Package: catren
Thecatrenpython packageUsingcatrenyou can create and renderR markdown filesinyour terminal oryour favorite Python environment (e.g.PyCharmorVisual Studio Code).Thecatrenpython package consists of 4 functions:catrmd, which concatenates input files to output anR Markdown(Rmd) file.render, which callsrmarkdown::renderto create various output files (e.g. HTML documents).catren, which combines the functionality ofcatrmd andrender.rmdtor, which usesknitr::purlto convert an Rmd into an R script.All of the above function work for PythonandR code, exceptrmdtorwhich skips Python chunks.To run Rmd files that include R and Python code, you will need thereticulateR package.The functions that begin withr(renderandrmdtor) rely on thermarkdownandknitr,
R packages written byYihui Xie.
Thecatrmdfunction has no dependencies beyond Python,
all other functions (render,catren, andrmdtor) require R.Installationpipinstallcatrenor clone therepo, e.g.git clone https://github.com/marskar/catrenand either use locally, e.g.python catren.py header.yml README.md plot.py notes.txtor install using setup.py, e.g.python setup.py install.Basic usage: terminalCreating an R markdown file withcatrmdin the terminalThecatrmdfunctions works likenbuild. Provide all of the source files as arguments, e.g.catrmdheader.ymlintro.mdletters.Rnotes.txtplot.pyThe default output filename forcatrmdiscat.Rmd. By default, input and output files are located in the current directory ('./').You can provide a more descriptive filename for the unrendered Rmd (-u) and set different input  (-i) and output  (-o) filepaths:catrmdheader.ymlintro.mdletters.Rnotes.txtplot.py--unrenderedraw.Rmd--output_pathrmarkdown/# Orcatrmdheader.ymlintro.mdletters.Rnotes.txtplot.py-uraw.Rmd-ormarkdown/If want to later output an R notebook usingRStudio, your YAML header should includehtml_notebookas an output type (Hint: press ctrl/cmd+Shift+K).---title:"Untitled"output:html_notebook---Therenderfunction from thermarkdownR package allows you to specify the output type on the fly with theoutput_formatargument.Basic usage: python environment# You can import any or all of the functions from the catren package.# You can also import each function individuallyfromcatrenimportcatrmdfromcatrenimportrenderfromcatrenimportcatrenfromcatrenimportrmdtor# The above imports all 4 functions# This can also be done with either of the two lines below.fromcatrenimportnbuild,nbexec,nbless,catrmdfromcatrenimport*# Another alternative is to import the package and use it as a namespace.importcatrenascr# Use individually# To make an Rmd file, use catrmdcatrmd(["header.yml","intro.md","letters.R","plot.py","notes.txt"],output_path="rmarkdown/")# catrmd will also work with Python scripts.catrmd(["header.yml","README.md","plot.py","notes.txt"],output_path="notebooks/")# Or to run both catrmd and render at once, use catrencatren["README.md","plot.py","notes.txt"],nbexec_path="notebooks/")# Use nbless as a namespacecr.catrmd(["header.yml","intro.md","letters.R","plot.py","notes.txt"],output_path="rmarkdown/")You can also run thecatrenfunctions in an R environment using thereticulateR package.Missing a dependency?If you installed Python and R withAnaconda, you should already have all of the dependencies (pythonandr-essentials).If not, or if you haveMinicondainstalled, runcondainstall-ycrr-essentialsToo many file names to type out?You can use thelscommand to assign all of the relevant names in the current directory to a variable and pass this variable as an argument tonbconvert.py.To preserve the order and differentiate files that should be incorporated into the notebook, I recommend left padding your file names with zeros (e.g. 01_intro.md, 02_figure1.py).Consider the example below:touch{01..09}.pyname_list=`ls0*.py`pythoncatrmd.py`echo$name_list`In a python environment, I recommendos.listdirto obtain a list of all files:fromosimportlistdirfromos.pathimportisfile,joinonlyfiles=[fforfinlistdir(mypath)ifisfile(join(mypath,f))]