rmarkdown running child chunks from inside RStudio - r

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/

Related

Execute all R chunks of an external Rmd file from an R script

I have a .Rmd file called f1.Rmd (containing a mix of text and R chunks) and an R script called f2.R.
I would like to insert a set of R instructions in f2.R that would execute all the R chunks contained in f1.Rmd, in such a way that all variables created in f1.Rmd would be created in my current R session if I source f2.R
(similarly to what happens when clicking on "Run" -> "Run all chunks below from the Rstudio menu").
If you render the f1.Rmd file from your current environment, this should happen.
You can use rmarkdown::render() from the console or a .R script. This will create all the variables in your current environment. It will also have the side effect of making the document.
When you use the knit button in RStudio, this launches the render in a new r session as a background process.
See also the envir option for render.
See also this answer for other options. knitr: run all chunks in an Rmarkdown document
It also depends what your .Rmd is doing.

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!

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

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?

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.

Knit2html not replicating functionality of Knit HTML button in R Studio

I'm trying to write a Bash script in Ubuntu 10.04 that opens a Python file which exports a CSV, and then runs the following Rscript with the goal of exporting a HTML with plots from Dashboard.Rmd:
require(knitr)
setwd('/home/sensors/Desktop/')
knit2html('Dashboard.Rmd')
browseURL('Dashboard.html')
Dashboard.Rmd is an R markdown that calls read.csv on the csv from the first step, makes a data frame and creates plots, but that part's working fine. According to this, I figure that Rscript should replicate the action of pressing "Knit HTML" in R Studio. However, the html it creates is identical to the last time Knit HTML was pressed; i.e. even if the CSV is different, the html doesn't reflect the change.
I also tried using a separate line for knit and markdownToHTML with the same effect. It seems like it doesn't source the code from the Rmd when performing knit. It does update the html properly when I enter the commands from that Rscript into the console of R Studio with Dashboard.Rmd open. However I'm not sure how to translate that into a Bash script. I also tried knit2html with envir=new.env(), envir=R_GlobalEnv, and envir=parent.frame() with no luck. Any help would be appreciated!
So it turns out that this was an artifact of cache=TRUE -- the HTML file was not changed because everything was cached.

Resources