Skip to main content

Rmarkdown to SoS Notebook converter

Project description

Anaconda-Server Badge PyPI version Build Status

sos-rmarkdown: an RMarkdown to SoS Notebook converter

The RMarkdown format is a markdown format with embedded R expressions and code blocks, and is extremely popular for R users. SoS Notebook is an extension to Jupyter Notebook that allows the use of multiple kernels in one notebook, and enables RMarkdown-like features such as markdown text with inline expressions and code blocks in multiple languages. sos-rmarkdown is an extension module to SoS Workflow System and Polyglot Notebook and provides an almost lossless converter from Rmarkdown files to SoS notebooks using the sos file conversion mechanism.

Installation

sos-rmarkdown and related tools (sos, sos-notebook, sos-r, markdown-kernel, papermill, sos-papermill) can be installed with command

pip install sos-rmarkdown

or

conda install -c conda-forge sos-rmarkdown

if you are using a conda environment. Note that

  1. The conda installation of sos-r will install the conda version of R (e.g. r-base).

  2. You will need to install additional kernels and language modules such as sos-python and sos-bash if the Rmarkdown documents use these languages.

Basic Usage

You can convert a .Rmd file to a Jupyter notebook with command

sos convert input.Rmd output.ipynb

and optionally execute the resulting notebook with option --execute

sos convert input.Rmd output.ipynb --execute

The resulting notebook could be converted to a HTML format using any of the jupyter or SoS Notebook templates using commands such as

sos convert output.ipynb output.html --template sos-report-toc-v2

These steps could be combined with a Rmd -> HTML converter using the following command if you would only like to execute a Rmarkdown document with SoS Notebook, not knitr, and generate a SoS-style report:

sos convert input.rmd output.html --execute --template sos-report-toc-v2

Note

  • If the .Rmd file does not have inline expressions and does not have code blocks using Python and other languages, and --execute option is not used, the resulting notebook will be a notebook with an R kernel.

  • The --execute option essentially uses sos-papermill to execute the notebook with an SoS kernel. You can execute the notebook directly with command papermill --engine sos if you would like to use advanced features of papermill.

Features

Although there are already a number of Rmd to Jupyter converters such as notedown, RMD-to-Jupyter, ipymd, and rmd2jupyter, they lack support for some of the Rmakdown features due to limitations of the Jupyter notebook platform. SoS Notebook, especially its Jupyter Lab extension addresses most of the limitations and offers an almost perfect conversion from R markdown to Jupyter notebook.

Markdown text with inline expressions

Rmarkdown supports inline expressions, which are R expressions embedded in markdown texts. Jupyter cannot handle embedded expressions in its markdown cells because markdown cells are handled in its frontend and do not interact with the computing kernel. SoS Notebook addresses this problem with the use of a markdown kernel, which is essentially a markdown kernel that can be expanded with language-specific inline expressions.

For example, the following Rmarkdown text

I counted `r sum(c(1,2,3))` blue cars on the highway.

is converted to a markdown cell that is evaluated in a R kernel as follows

image

Code blocks in multiple languages

Code blocks in Rmarkdown supports multiple languages such as Python, Julia, and Stata. A Jupyter notebook with an ir kernel can only evaluate R scripts, and the use of magics such as %%python is rather limiting. SoS Notebook supports the use of multiple kernels in one notebook and can accommodate code blocks in multiple languages.

For example, code blocks such as

```{python}
def f(x):
  return x + 2
f(2)
```

and

```{r engine="python"}
def f(x):
  return x + 2
f(2)
```

are converted to cells with approprivate kernels such as

image

Note that SoS Notebook supports the execution of multiple kernels and allows data exchange between live kernels. This is differnt from Rmarkdown's multi-language approach and might lead to different results. For example, whereas multiple Python3 cells are always executed by the same Python kernel (unless multiple Python kernels are used with a %use magic or the Python kernel is restarted with a %shutdown -r magic), RMarkdown's Python code blocks can be executed together or independently (with or without reticulate).

Hiding input and/or output of code blocks

Rmarkdown's code blocks accept options such as echo=FALSE and include=FALSE which controls the output of input and/or output of code blocks. There were no corresponding features for classic Jupyter Notebook but Jupyter Lab supports hiding of input and/or output of cells using cell metadata. The sos-rmarkdown converter understands these options and converts code blocks such as,

```{r echo=FALSE}
arr <- rnorm(5)
cat(arr)
```

with appropriate open/colapse status

image

In addition, whereas the default templates from jupyter nbconvert does not respect the collasping status of cells and renders input and output of all cells, SoS Notebook provides templates that supports these features. For example, template sos-report-toc-v2 outputs all cells but hides collapsed inputs and outputs by default. The hidden content could be displayed by selecting a dropdown box to the top right corner of the document. Please refer to SoS Notebook templates for a list of templates.

Conversion from SoS Notebook to RMarkdown?

sos-rmarkdown is designed to convert Rmarkdown documents to Jupyter notebooks. It aims to provide a lossless converter from RMarkdown to SoS Notebook so please submit a ticket if certain features of RMarkdown are not properly converted.

sos-rmarkdown does retain all global and code block options of RMarkdown documents as global and cell meta data in the resulting notebooks, but a SoS Notebook to RMarkdown converter is not planned. Please submit a PR if you are interested in adding this feature.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sos_rmarkdown-0.1.6.tar.gz (13.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sos_rmarkdown-0.1.6-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file sos_rmarkdown-0.1.6.tar.gz.

File metadata

  • Download URL: sos_rmarkdown-0.1.6.tar.gz
  • Upload date:
  • Size: 13.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for sos_rmarkdown-0.1.6.tar.gz
Algorithm Hash digest
SHA256 186f492f577b5a197c2388452df7fa7136aea0b979d4d3fd7572d1b48b6b2f40
MD5 44e3b43d50ed40d2d60b3c0a786d7165
BLAKE2b-256 617925540aefceecba18e354e2dfa3908060c5440ad1ea0879d5fa0ea68bf6d9

See more details on using hashes here.

File details

Details for the file sos_rmarkdown-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: sos_rmarkdown-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for sos_rmarkdown-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 4300992980b853968403fa0cdb081a7673b23e4426b0742b5a025efb8ca94438
MD5 75a9beb58458feec79a9289d1e3126dd
BLAKE2b-256 ab8c6240514695b9647b7a9bfcd36f98898e49168022f73c0cbba1cbc5f32e15

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page