Comments (2)
The same just happened to me.
from rmd2jupyter.
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rmd2jupyter.