Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.
like image 258
statquant Avatar asked Sep 03 '19 18:09

statquant


People also ask

Is R Markdown same as Jupyter Notebook?

Rmarkdown is based on markdown, a human readable markup language, Jupyter notebooks are based on JSON, a data interchange format common on the web. This means that Rmarkdown files can be easily edited using any text editor you like. The same is not true of Jupyter notebooks.

Is Markdown allowed in Jupyter Notebook?

Markdown in Jupyter Notebook A great benefit of Jupyter Notebook is that it allows you to combine both code (e.g. Python ) and Markdown in one document, so that you can easily document your workflows.

How do I make a Jupyter Notebook Markdown?

You can change the cell type of any cell in Jupyter Notebook using the Toolbar. The default cell type is Code. To use the Keyboard Shortcuts, hit the esc key. After that, you can change a cell to Markdown by hitting the m key, or you can change a cell to Code by hitting the y key.

What can R Markdown notebooks be converted into?

R Markdown can also compile R scripts to a notebook which includes commentary, source code, and script output. Notebooks can be compiled to any output format including HTML, PDF, and MS Word.


1 Answers

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
like image 98
statquant Avatar answered Oct 23 '22 17:10

statquant