Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

convert_ipynb

Convert a Jupyter/IPython notebook to an R Markdown document


Description

Read a Jupyter/IPython notebook file (‘.ipynb’) via jsonlite::fromJSON(), convert its code cells to R Markdown code chunks, preserve Markdown cells, and write out the results to an Rmd file.

Usage

convert_ipynb(input, output = xfun::with_ext(input, "Rmd"))

Arguments

input

Path to the input ‘.ipynb’ file.

output

The output file path.

Details

This simple converter may have some rough edges, depending on how many IPython-specific features are used in a notebook. For example, line magics are not automatically converted (warnings will be issued if line magics are detected), but you may consider using or writing R functions to replace them in R Markdown (e.g., the %load magic may be replaced by reticulate::source_python()). Cell magics will be converted to code chunks with the (knitr) language engine names being the magic names. For example, the cell magic %%js is converted to ```{js} in R Markdown. This does not always work because not all IPython cell magics have their counterparts in knitr's language engines, but common cell magics like %%bash, %%sh, %%js, %%perl, %%python, and %%ruby should work.

Value

The output file path (invisibly).

Examples

# this is not a real ipynb file, but illustrates what convert_ipynb() does
nb_data <- list(
  cells = list(
    list(cell_type = 'markdown', source = 'Hi **Markdown**!'),
    list(cell_type = 'code', source = 'print("Hi R Markdown!")')
  ),
  metadata = list(
    kernelspec = list(language = 'python')
  )
)
nb_file = tempfile(fileext = '.ipynb')
jsonlite::write_json(nb_data, nb_file, auto_unbox = TRUE, pretty = TRUE)
xfun::file_string(nb_file)  # show file content

# convert to R Markdown
nb_rmd = rmarkdown:::convert_ipynb(nb_file)
xfun::file_string(nb_rmd)

rmarkdown

Dynamic Documents for R

v2.10
GPL-3
Authors
JJ Allaire [aut], Yihui Xie [aut, cre] (<https://orcid.org/0000-0003-0645-5666>), Jonathan McPherson [aut], Javier Luraschi [aut], Kevin Ushey [aut], Aron Atkins [aut], Hadley Wickham [aut], Joe Cheng [aut], Winston Chang [aut], Richard Iannone [aut] (<https://orcid.org/0000-0003-3925-190X>), Andrew Dunning [ctb] (<https://orcid.org/0000-0003-0464-5036>), Atsushi Yasumoto [ctb, cph] (<https://orcid.org/0000-0002-8335-495X>, Number sections Lua filter), Barret Schloerke [ctb], Carson Sievert [ctb] (<https://orcid.org/0000-0002-4958-2844>), Christophe Dervieux [ctb], Devon Ryan [ctb] (<https://orcid.org/0000-0002-8549-0971>), Frederik Aust [ctb] (<https://orcid.org/0000-0003-4900-788X>), Jeff Allen [ctb], JooYoung Seo [ctb] (<https://orcid.org/0000-0002-4064-6012>), Malcolm Barrett [ctb], Rob Hyndman [ctb], Romain Lesur [ctb], Roy Storey [ctb], Ruben Arslan [ctb], Sergio Oller [ctb], RStudio, PBC [cph], jQuery Foundation [cph] (jQuery library), jQuery contributors [ctb, cph] (jQuery library; authors listed in inst/rmd/h/jquery-AUTHORS.txt), jQuery UI contributors [ctb, cph] (jQuery UI library; authors listed in inst/rmd/h/jqueryui-AUTHORS.txt), Mark Otto [ctb] (Bootstrap library), Jacob Thornton [ctb] (Bootstrap library), Bootstrap contributors [ctb] (Bootstrap library), Twitter, Inc [cph] (Bootstrap library), Alexander Farkas [ctb, cph] (html5shiv library), Scott Jehl [ctb, cph] (Respond.js library), Ivan Sagalaev [ctb, cph] (highlight.js library), Greg Franko [ctb, cph] (tocify library), John MacFarlane [ctb, cph] (Pandoc templates), Google, Inc. [ctb, cph] (ioslides library), Dave Raggett [ctb] (slidy library), W3C [cph] (slidy library), Dave Gandy [ctb, cph] (Font-Awesome), Ben Sperry [ctb] (Ionicons), Drifty [cph] (Ionicons), Aidan Lister [ctb, cph] (jQuery StickyTabs), Benct Philip Jonsson [ctb, cph] (pagebreak Lua filter), Albert Krewinkel [ctb, cph] (pagebreak Lua filter)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.