Skip to main content

Jupyter from/to R markdown notebooks

Project description

From Jupyter notebooks to R markdown, and back

Pypi Pypi Build Status codecov.io pylint Score pyversions Binder

This is a utility that allows to open and run R markdown notebooks in Jupyter, and save Jupyter notebooks as R markdown. You will be interested in this if

  • you want to version your notebooks and occasionally have to merge versions
  • you want to use RStudio's advanced rendering of notebooks to PDF, HTML or HTML slides
  • or, you have a collection of markdown or R markdown notebooks and you want to open them in Jupyter

Note that if you prefer to save notebooks as python scripts, this is also possible. In that case, have a look at the nbsrc package.

What is R markdown?

R markdown (extension .Rmd) is a source only format for notebooks. As the name states, R markdown was designed in the R community, and is the reference notebook format there. The format actually supports [many languages](https://yihui .name/knitr/demo/engines/).

R markdown is almost like plain markdown. There are only two differences:

  • R markdown has a specific syntax for active code cells: language, and optional cell options are enclosed into a pair of curly brackets:

    ```{python}
    
    
  • a YAML header, that describes the notebook title, author, and desired output (HTML, slides, PDF...).

Look at nbrmd/tests/ioslides.Rmd for a sample R markdown file (that, actually, only includes python cells).

Why R markdown and not filtered .ipynb under version control?

The common practice for having Jupyter notebooks under version control is to remove outputs with a pre-commit hook. That works well and this will indeed get you a clean commit history.

However, you may run into trouble when you try to merge two .ipynb notebooks in a simple text editor. Merging text notebooks, like the .Rmd ones that this package provides, is much simpler.

How do I open R markdown notebooks in Jupyter?

The nbrmd package offers a ContentsManager for Jupyter that recognizes .Rmd files as notebooks. To use it,

  • generate a jupyter config, if you don't have one yet, with jupyter notebook --generate-config
  • edit the config and include this:
c.NotebookApp.contents_manager_class = 'nbrmd.RmdFileContentsManager'

Then, make sure you have the nbrmd package installed, and re-start jupyter, i.e. run

pip install nbrmd
jupyter notebook

Now you can open your .Rmd files as notebooks in Jupyter, and save your jupyter notebooks in R markdown format (see below).

Rmd notebook in jupyter Rmd notebook as text

Can I save my Jupyter notebook as both R markdown and ipynb?

Yes. That's even the recommended setting for the notebooks you want to set under version control.

You need to choose whever to configure this per notebook, or globally.

Per-notebook configuration

The R markdown content manager includes a pre-save hook that will keep up-to date versions of your notebook under the file extensions specified in the nbrmd_formats metadata. Edit the notebook metadata in Jupyter and select the desired formats, like this:

{
  "kernelspec": {
    "name": "python3",
    (...)
  },
  "language_info": {
    (...)
  },
  "nbrmd_formats": "ipynb,Rmd"
}

Global configuration

If you want every notebook to be saved as both .Rmd and .ipynb files, then change your jupyter config to

c.NotebookApp.contents_manager_class = 'nbrmd.RmdFileContentsManager'
c.ContentsManager.default_nbrmd_formats = 'ipynb,Rmd'

If you prefer to update just .Rmd, change the above accordingly (you will still be able to open regular .ipynb notebooks).

Recommendations for version control

I recommend that you set nbrmd_formats to "ipynb,Rmd", either in the default configuration, or in the notebook metadata (see above).

When you save your notebook, two files are generated, with .Rmd and .ipynb extensions. Then, when you reopen either one or the other,

  • cell input are taken from the source only format, here .Rmd file
  • cell outputs are taken from .ipynb file.

This way, you can set the .Rmd file under version control, and still have the commodity of having cell output stored in the .ipynb file. When the .Rmd file is updated outside of Jupyter, then you simply reload the notebook, and benefit of the updates.

:warning: Be careful not to open twice a notebook with two distinct extensions! You should shutdown the notebooks with the extension you are not currently editing (list your open notebooks with the running tab in Jupyter).

How do I use the converter?

The package also provides a nbrmd script that converts Jupyter notebooks to R markdown notebooks, and vice-versa.

Use it as:

nbrmd jupyter.ipynb         # this prints the Rmarkdown alternative
nbrmd jupyter.ipynb -i      # this creates a jupyter.Rmd file
nbrmd jupyter.Rmd   -i      # and this, a jupyter.ipynb file
nbrmd jupyter.Rmd   -i -p   # update the jupyter.ipynb file and preserve outputs that correspond to unchanged inputs

Alternatively, the nbrmd package provides a nbconvert rmarkdown exporter that you can use with

nbconvert jupyter.ipynb --to rmarkdown

And if I convert twice?

Round trip conversion of R markdown is identity.
Round trip conversion of Jupyter notebooks preserves the source, not outputs.

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

nbrmd-0.4.2.tar.gz (24.3 kB view details)

Uploaded Source

Built Distribution

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

nbrmd-0.4.2-py3-none-any.whl (32.9 kB view details)

Uploaded Python 3

File details

Details for the file nbrmd-0.4.2.tar.gz.

File metadata

  • Download URL: nbrmd-0.4.2.tar.gz
  • Upload date:
  • Size: 24.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for nbrmd-0.4.2.tar.gz
Algorithm Hash digest
SHA256 85703db7a93afe5faaeac355671b8bd618e0eb2f6ec126d6b03fa070283e884d
MD5 7eb2ffd347df405f7692e957e9c48c55
BLAKE2b-256 f7fc515ff7c1097ef1bc00900ffa9af144e6eb098f36baead8103de0ab0f5299

See more details on using hashes here.

File details

Details for the file nbrmd-0.4.2-py3-none-any.whl.

File metadata

File hashes

Hashes for nbrmd-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c8594b05bf5e206ba0b24bc1eb176a689ea2bfa596295f9c2b3828b27987ec87
MD5 138cd0f98ea02096ce78ee2bb90093e8
BLAKE2b-256 584c0a6d68b8b7ef980af427cffb9dda1502551fcd89a93a09ae360ecf60443a

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