<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/knit-directly-to-jupyter-notebooks-from-rstudio/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rud.is/b/2017/01/10/knit-directly-to-jupyter-notebooks-from-rstudio/"> R – rud.is</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Did you know that you can completely replace the “knitting” engine in R Markdown documents? Well, you can!</p>
<p>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.</p>
<p>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 <code>ipynb</code> and <code>Rmd</code> formats.</p>
<p>The <a target="_blank" rel="nofollow" href="https://github.com/aaren/notedown"><code>notedown</code></a> module and command-line tool does just that. mI came across that after seeing <a target="_blank" rel="nofollow" href="https://github.com/lecy/RMD-to-Jupyter">this <code>notedown</code> example</a>. 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 <code>ipynb</code> file and a notebook preview <code>html</code> file from an Rmd you’re working on.</p>
<p>We can exploit the fact that you can specify a <code>knit:</code> 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 <code>pandoc</code> processing. Your function has to do all the heavy lifting.</p>
<p>To that end, I modified my (github only for now) <a target="_blank" rel="nofollow" href="https://github.com/hrbrmstr/markdowntemplates"><code>markdowntemplates</code></a> package and added a <code>to_jupyter()</code> 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 <code>notedown</code> installed properly, adding <code>knit: markdowntemplates::to_jupyter</code> to the YAML header of (in theory) any Rmd document and knitting it via RStudio will result in</p>
<ul>
<li>an <code>ipynb</code> file being generated</li>
<li>an <code>html</code> file generated via <code>nbconvert</code>ing the notebook file, and </li>
<li>said HTML file being rendered in your system’s default browser</li>
</ul>
<p>You can take <a target="_blank" rel="nofollow" href="https://rud.is/dl/notedown/notedown.Rmd">this test Rmd</a>:</p>
<pre><code>---
knit: markdowntemplates::to_jupyter
---
## Notedown Test

Let's try a python block

```{r engine=&quot;python&quot;}
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()
```</code></pre>
<p>and, after doing <code>devtools::install_github(&quot;hrbrmstr/markdowntemplates&quot;)</code> and ensuring you have <code>notedown</code> working, knit it in RStudio to generate <a target="_blank" rel="nofollow" href="https://rud.is/dl/notedown/notedown.ipynb">the <code>ipynb</code> file</a> and render an HTML file:</p>
<p><iframe height="1250px" width="100%" src="https://rud.is/dl/notedown/notedown.html"></iframe></p>
<p>Note the python block is a fully functioning notebook cell. I haven’t tried other <em>magic</em> language cells, but they should work according to the <code>notedown</code> docs.</p>
<p>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 <a target="_blank" rel="nofollow" href="https://github.com/hrbrmstr/markdowntemplates/issues">file an issue</a>. Also drop any feature requests to the same location.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://rud.is/b/2017/01/10/knit-directly-to-jupyter-notebooks-from-rstudio/"> R – rud.is</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/Wr_KQHvHICw"></a></div>