Run ipynb with R through the command line

Hi all - how do I run a jupyter notebook that has an R script in it through the command line? And better still, how do I assign parameters that will be read as variables into the script?

Thanks!

A general approach using only jupyter first-party means would be:

  • get parameters in:
    • write your notebook to optionally ingest the parameters via “normal means”, if available:
      • read a config file
      • read environment variable
      • or read a config file from a variable
    • use notebooks as strings
      • write your notebooks with some placeholders
      • read the notebook as a string
      • replace the strings
      • write out the strings
    • use notebooks as data, and inject cells
      • read the some-r-notebook.tmpl.ipynb with your preferred R mechanism
      • inject some code cells
      • write out the json
  • use nbconvert
jupyter nbconvert --execute some-r-notebook.ipynb

There are rafts of other solutions, e.g papermill, ploomber, but all bring a lot of opinions (e.g. custom metadata) that can make a notebook less portable.

2 Likes