With RMarkdown is there a way to use knitr syntax to call external R code from a Jupyter notebook? - r

I am using RMarkdown to write a journal article. For various reasons I'd prefer to have the R analysis script in a separate Jupyter notebook. Is there a nice way to call R code from MyAnalysis.ipynb in MyArticle.Rmd?
I know I can use knitr syntax to have the .Rmd file read and execute chunks of R code from a .R file like so. And that you can use knitr::purl to call code chunks from one rmarkdown doc in another like so.
But I would like to be able to "purl" the code from the .ipynb file. Is there any way to do this?

Related

Source RMarkdown chunks

Please consider the following.
For me, the beauty of writing nearly all analyses in a RMarkdown file instead of in R scripts it that RMarkdown offers the possibility to write a report of the analysis while performing/coding it.
Sometimes, specific code snippets are re-used for different outputs. For example: a table created in a RMarkdown code chunk could be used in a Shiny app as well. Currently, I copy/paste the respective code from the RMarkdown into the Shiny app code.
However, when this table would be created in a R script, we can use source("table_script.R"). In this way no copy/paste is needed and both the RMarkdown and the Shiny app can make efficient use of this table. However, this (writing separate source-able R scripts) is exactly what I try to avoid when writing an RMarkdown, because otherwise the code chunks in the RMarkdown would have little other use than sourcing a couple of R scripts.
Question
Is there any way to source() (named) RMarkdown chunks?
Thanks in advance!

How to use libraries across an r notebook?

I wish to use libraries across multiple .Rmd files in an r notebook without having to reload the library each time.
An example: I have loaded the library kableExtra in the index.Rmd file but when I call it in another .Rmd file such as ExSum.Rmd I would get this error:
Error in Kable....: could not find funciton "kable" Calls:...
If I load the kableExtra library again this problem goes away. Is there a workaround?
R Markdown files are intended to be standalone, so you shouldn't do this. There are two workarounds that come close:
If you process your .Rmd files within the R console by running code like rmarkdown::render("file.Rmd") then any packages attached in the session will be available to the code in the .Rmd file.
You can put all the setup code (e.g. library(kableExtra)) into a file (called setup.R, for example), and source it into each document in the first code chunk using source('setup.R'). Every file will run the same setup, but you only need to type it once.
The second is the better approach.

rmarkdown running child chunks from inside RStudio

I've been moving some of the code for a report I'm writing to child .rmd files. I want to run these chunks by clicking on the green arrow (top right):
But this doesn't work in RStudio, is this a feature or a bug?
This has not been implemented in RStudio yet, and probably won't be for some time.
However, you can write your R code in a separate file, reference it in R Markdown chunks, and execute those chunks interactively in RStudio. The way to do this is with knitr's code externalization feature. You can read about how to use it in R Markdown notebooks here:
https://rmarkdown.rstudio.com/r_notebooks.html#executing_code (scroll down a bit to Executing External Chunks)
More on code externalization with knitr here:
https://yihui.name/knitr/demo/externalization/

Testing code chunks of a markdown script

I have a markdown script with several code chunks and providing this script together with my package.
I want to include unit testing for these code chunks or any way to make sure, my markdown script is always running.
Has anyone tried something before or can recommend a way of testing the markdown script?
I use a function called runAllChunks to run R code from an RMD file. I stole the function from knitr: run all chunks in an Rmarkdown document. That might be helpful in your situation.

knitr to produce pdf output file

I would like to be able to use knitr to produce a structured PDF file. I did not see anything unless the input file is Sweave format. This question may be too vague but I really like to know right from knitr, is it possible to create a structured PDF output file without using any Sweave or LaTeX?
Try using command knit2pdf if you haven't already.

Resources