statquant
statquant

Reputation: 14370

How can I convert a Rmd document to a jupyter notebook

I would like to convert a rmarkdown .Rmd document to a jupyter notebook .ipynb. I found that converting from jupyter to rmd is easy using as described in reference page but for some reason (...) the Rstudio team did not do the other way.

For instance I would like to convert

---
title: "Untitled"
author: "statquant"
date: "03/09/2019"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
summary(cars)
```

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}
plot(pressure)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Upvotes: 22

Views: 17896

Answers (3)

statquant
statquant

Reputation: 14370

After checking, the silver bullet seems to be jupytext It allows you to convert from or to markdown, rmarkdown, python, ipynb, etc.

This can actually allow you a pretty neat workflow

  1. write a simple R script, script.R, that you can spin into a Rmd document
  2. use knitr::spin('script.R', knit = FALSE) to transform it to Rmd
  3. use jupytext --to notebook script.Rmd to create script.ipynb
  4. share or execute the notebook

Upvotes: 36

Aziz Alto
Aziz Alto

Reputation: 20341

Here is another way.

The detailed answer (to convert .rmd to .ipynb) is described here: https://gist.github.com/ramnathv/10012123

TL;DR

Use a 3rd-party Python package notedown with sed command as follows:

1) Install a 3rd-party python package which does the conversion for us

$ pip install notedown

2) Use the installed package to convert from your *.Rmd file (or *.md) to *.ipynb and run the terminal command:

$ notedown example.Rmd | sed '/%%r/d' > example.ipynb

Upvotes: 3

user2283347
user2283347

Reputation: 779

sos-rmarkdown provides yet another Rmarkdown to Jupyter notebook converter. The unique features include its support for inline expressions using markdown-kernel, use of multiple kernels in one notebook (using a SoS kernel) to accommodate code blocks in multiple languages, and the ability to execute generated notebook using sos-papermill. It also uses cell meta data to control the display of input and output of code blocks in Jupyter Lab and exported HTML reports.

To use this tool, you can install sos-rmarkdown from pip or conda-forge, then run the converter with command

sos convert input.Rmd output.ipynb

or use option --execute to execute the converted notebook

sos convert input.Rmd output.ipynb --execute

Disclaimer: I am the author of sos-rmarkdown.

Upvotes: 4

Related Questions