Coder Social home page Coder Social logo

Overwrites the original file about rmd2jupyter HOT 2 OPEN

drtoche avatar drtoche commented on June 9, 2024
Overwrites the original file

from rmd2jupyter.

Comments (2)

ricoperdiz avatar ricoperdiz commented on June 9, 2024

The same just happened to me.

from rmd2jupyter.

mariusbommert avatar mariusbommert commented on June 9, 2024

It seems to be beneficial to add a parameter for the name of the output file similar to the following code:

rmd2jupyter <- function(x, out, force = FALSE) {
  if(!file.exists(out) || force) {
    con <- file(x)
    x <- readLines(con, warn = FALSE)
    close(con)
    ## strip yaml
    if (grepl("^---", x[1])) {
      yaml_end <- grep("^---", x)[2]
      x <- x[(yaml_end + 1L):length(x)]
    }
    chunks <- grep("^```", x)
    if (length(chunks) == 0L) {
      lns <- c(1L, length(x))
      chunks <- matrix(lns, ncol = 2, byrow = TRUE)
      chunks <- data.frame(chunks)
      names(chunks) <- c("start", "end")
      chunks$cell_type <- "markdown"
    } else {
      lns <- sort(c(
        1L, length(x), chunks,
        chunks[seq(1, length(chunks), 2)] - 1L,
        chunks[seq(2, length(chunks), 2)] + 1L
      ))
      lns <- lns[lns > 0 & lns <= length(x)]
      if (chunks[length(chunks)] == length(x)) {
        lns <- lns[-length(lns)]
      }
      if (chunks[1L] == 1L) {
        lns <- lns[-1]
      }
      chunks <- matrix(lns, ncol = 2, byrow = TRUE)
      chunks <- data.frame(chunks)
      names(chunks) <- c("start", "end")
      codes <- grep("^```", x)
      codes <- codes[seq(1, length(codes), 2)]
      chunks$cell_type <- ifelse(chunks$start %in% codes, "code", "markdown")
      x <- gsub("^```.*", "", x)
    }
    for (i in seq_len(nrow(chunks))) {
      s <- paste0(x[(chunks$start[i]):(chunks$end[i])], "\n")
      ## trim top and bottom blank lines
      while (s[1] == "\n" & length(s) > 2L) {
        s <- s[-1]
      }
      while (s[length(s)] == "\n" & length(s) > 2L) {
        s <- s[-length(s)]
      }
      chunks$source[i] <- I(list(s))
    }
    cells <- Map(format_cell, chunks$cell_type, chunks$source)
    x <- jsonlite::prettify(format_cells(cells))
    x <- gsub("count\": \"\"", "count\": null", x)
    x <- gsub("metadata\": \"\"", "metadata\": {}", x)
    x <- gsub("outputs\": \"\"", "outputs\": []", x)
    cat(x, file = out)
    message(paste("file saved as", out))
  } else {
    warning(paste0("File ", out, " already exists. Use force = TRUE to force writing.")) 
  }
}

As out something like "file.ipynb" can be used and there is a warning if the file already exists. Setting force = TRUE overwrites the existing file.

from rmd2jupyter.

Related Issues (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.