Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overwrites the original file #1

Open
drtoche opened this issue Feb 10, 2022 · 2 comments
Open

Overwrites the original file #1

drtoche opened this issue Feb 10, 2022 · 2 comments

Comments

@drtoche
Copy link

drtoche commented Feb 10, 2022

This project was very promising. But unfortunately it overwrites the original file. I wasn't in the mood to check if the notebook looked good...

@ricoperdiz
Copy link

The same just happened to me.

@mariusbommert
Copy link

mariusbommert commented Feb 7, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants