Did you know that you can completely replace the “knitting” engine in R Markdown documents? Well, you can!
Why would you want to do this? Well, in the case of this post, to commit the unpardonable sin of creating a clunky jupyter notebook from a pristine Rmd file.
I’m definitely not “a fan” of “notebook-style” interactive data science workflows (apologies to RStudio, but I don’t even like their take on the interactive notebook). However, if you work with folks who are more productive in jupyter-style environments, it can be handy to be able to move back and forth between the ipynb
and Rmd
formats.
The notedown
module and command-line tool does just that. I came across that after seeing this notedown
example. There’s a script there to do the conversion but it’s very Windows-specific and it’s a pretty manual endeavour if all you want to do is quickly generate both an ipynb
file and a notebook preview html
file from an Rmd you’re working on.
We can exploit the fact that you can specify a knit:
parameter in the Rmd YAML header. Said parameter can be inline code or be a reference to a function in a package. When you use the “Knit” command from RStudio (button or key-cmd-shortcut) this parameter will cause the Rmd file to be passed to that function and bypass all pandoc
processing. Your function has to do all the heavy lifting.
To that end, I modified my (github only for now) markdowntemplates
package and added a to_jupyter()
function. Provided you have jupyter setup correctly (despite what the python folk say said task is not always as easy as they’d like you to believe) and notedown
installed properly, adding knit: markdowntemplates::to_jupyter
to the YAML header of (in theory) any Rmd document and knitting it via RStudio will result in
- an
ipynb
file being generated - an
html
file generated vianbconvert
ing the notebook file, and - said HTML file being rendered in your system’s default browser
You can take this test Rmd:
---
knit: markdowntemplates::to_jupyter
---
## Notedown Test
Let's try a python block
```{r engine="python"}
def test(x):
return x * x
test(2)
```
And a ggplot test
```{r}
suppressPackageStartupMessages(library(ggplot2))
```
We'll use an old friend
```{r}
head(mtcars)
```
and plot it:
```{r}
ggplot(mtcars, aes(wt, mpg)) + geom_point() + ggthemes::theme_fivethirtyeight()
```
and, after doing devtools::install_github("hrbrmstr/markdowntemplates")
and ensuring you have notedown
working, knit it in RStudio to generate the ipynb
file and render an HTML file:
Note the python block is a fully functioning notebook cell. I haven’t tried other magic language cells, but they should work according to the notedown
docs.
I’ve only performed light testing (on a MacBook Pro with jupyter running under python 3.x) and I’m sure there will be issues (it’s python, it’s jupyter and this is dark alchemy bridging those two universes), so when you run into errors, please file an issue. Also drop any feature requests to the same location.
3 Comments
I’d love to see a post on what you dislike about notebooks, specifically RStudio notebooks. While I don’t care to work in Jupyter very often, I do get a kick out of RStudio’s take on them. Getting your opinions and concerns would be helpful.
I’m trying to convert my Rmd file to ipynb using your procedure, but get this error. Any thoughts on a quick fix?
Error in knit:markdowntemplates::to_jupyter : NA/NaN argument
If you could head over to https://github.com/hrbrmstr/markdowntemplates and file an issue with more details I can likely assist. It’s a better place for package problems.
5 Trackbacks/Pingbacks
[…] Did you know that you can completely replace the “knitting” engine in R Markdown documents? Well, you can! Why would you want to do this? Well, in the case of this post, to commit the unpardonable sin of creating a clunky jupyter notebook from a pristine Rmd file. I’m definitely not “a fan” of “notebook-style”… Continue reading → […]
[…] leave a comment for the author, please follow the link and comment on their blog: R – rud.is.R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data […]
[…] article was first published on R – rud.is, and kindly contributed to […]
[…] had to take a quick peek at markdowntemplates? due to a question from a blog reader about the Jupyter notebook generation functionality. While I was in the code I added two new bits to the knit: […]
[…] to a question from a blog reader about the Jupyter notebook generation functionality. While I was in the code I added two new bits to the knit: […]