| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/jupyter.R |
| 3 |
\name{convert_ipynb} |
| 4 |
\alias{convert_ipynb} |
| 5 |
\title{Convert a Jupyter/IPython notebook to an R Markdown document} |
| 6 |
\usage{ |
| 7 |
convert_ipynb(input, output = xfun::with_ext(input, "Rmd")) |
| 8 |
} |
| 9 |
\arguments{ |
| 10 |
\item{input}{Path to the input \file{.ipynb} file.} |
| 11 |
|
| 12 |
\item{output}{The output file path.} |
| 13 |
} |
| 14 |
\value{ |
| 15 |
The output file path (invisibly). |
| 16 |
} |
| 17 |
\description{ |
| 18 |
Read a Jupyter/IPython notebook file (\file{.ipynb}) via |
| 19 |
\code{jsonlite::fromJSON()}, convert its code cells to R Markdown code |
| 20 |
chunks, preserve Markdown cells, and write out the results to an Rmd file. |
| 21 |
} |
| 22 |
\details{ |
| 23 |
This simple converter may have some rough edges, depending on how many |
| 24 |
IPython-specific features are used in a notebook. For example, line magics |
| 25 |
are not automatically converted (warnings will be issued if line magics are |
| 26 |
detected), but you may consider using or writing R functions to replace them |
| 27 |
in R Markdown (e.g., the \command{\%load} magic may be replaced by |
| 28 |
\code{reticulate::source_python()}). Cell magics will be converted to code |
| 29 |
chunks with the (\pkg{knitr}) language engine names being the magic names. |
| 30 |
For example, the cell magic \command{\%\%js} is converted to \verb{```{js}} |
| 31 |
in R Markdown. This does not always work because not all IPython cell magics |
| 32 |
have their counterparts in \pkg{knitr}'s language engines, but common cell |
| 33 |
magics like \command{\%\%bash}, \command{\%\%sh}, \command{\%\%js}, |
| 34 |
\command{\%\%perl}, \command{\%\%python}, and \command{\%\%ruby} should work. |
| 35 |
} |
| 36 |
\examples{ |
| 37 |
# this is not a real ipynb file, but illustrates what convert_ipynb() does |
| 38 |
nb_data <- list( |
| 39 |
cells = list( |
| 40 |
list(cell_type = 'markdown', source = 'Hi **Markdown**!'), |
| 41 |
list(cell_type = 'code', source = 'print("Hi R Markdown!")') |
| 42 |
), |
| 43 |
metadata = list( |
| 44 |
kernelspec = list(language = 'python') |
| 45 |
) |
| 46 |
) |
| 47 |
nb_file = tempfile(fileext = '.ipynb') |
| 48 |
jsonlite::write_json(nb_data, nb_file, auto_unbox = TRUE, pretty = TRUE) |
| 49 |
xfun::file_string(nb_file) # show file content |
| 50 |
|
| 51 |
# convert to R Markdown |
| 52 |
nb_rmd = rmarkdown:::convert_ipynb(nb_file) |
| 53 |
xfun::file_string(nb_rmd) |
| 54 |
} |