如何将 Rmd 文档转换为 jupyter notebook

How can I convert a Rmd document to a jupyter notebook

我想将 rmarkdown .Rmd 文档转换为 jupyter notebook .ipynb。 我发现从 jupyter 转换为 rmd 很容易使用,如 reference page 中所述,但出于某种原因 (...) Rstudio 团队没有采用其他方式。

例如我想转换

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

查了一下,灵丹妙药好像是jupytext 它允许您在 markdownrmarkdownpythonipynb

之间进行转换

这实际上可以让您拥有一个非常整洁的工作流程

  1. 编写一个简单的 R 脚本,script.R,将 you can spin 写入 Rmd 文档
  2. 使用knitr::spin('script.R', knit = FALSE)将其转换为Rmd
  3. 使用jupytext --to notebook script.Rmd创建script.ipynb
  4. 共享或执行笔记本

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。它还使用单元格元数据来控制代码块在 Jupyter Lab 中的输入和输出显示,并导出 HTML 报告。

要使用此工具,您可以从 pipconda-forge 安装 sos-rmarkdown,然后 运行 使用命令

转换器
sos convert input.Rmd output.ipynb

或使用选项--execute执行转换后的笔记本

sos convert input.Rmd output.ipynb --execute

免责声明:我是 sos-rmarkdown.

的作者

这是另一种方法。

这里描述了详细的答案(将 .rmd 转换为 .ipynb):https://gist.github.com/ramnathv/10012123

TL;DR

使用第 3 方 Python 包 notedownsed 命令如下:

1) 安装一个 3rd-party python 包来为我们做转换

$ pip install notedown

2) 使用安装包将*.Rmd 文件(或*.md)转换为*.ipynb 和运行 终端命令:

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