Skip navigation

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 via nbconverting 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

  1. 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.

  2. 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


5 Trackbacks/Pingbacks

  1. By Knit directly to jupyter notebooks from RStudio – Cyber Security on 10 Jan 2017 at 11:58 am

    […] 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 → […]

  2. By Knit directly to jupyter notebooks from RStudio - Use-R!Use-R! on 10 Jan 2017 at 2:10 pm

    […] 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 […]

  3. […] article was first published on R – rud.is, and kindly contributed to […]

  4. […] 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: […]

  5. […] 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: […]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.