I would like to create an Rmarkdown document (pdf or html) that has some chunks "executed" conditionally. The particular case I have in mind is that I might want a more verbose and documented version of the output for internal review by colleagues, and a shorter version for external consumers. I may not want or need to show data manipulation steps to a client, but just key graphs and tables. I also do not want to make two separate documents or have to manually indicate what to show or not.
Is there a way to set a switch at the beginning of the Rmd that indicates, e.g., verbose=T that will run all chunks or verbose=F that toggles echo=F (or include=F)?
Thank you.
knitr options can be stated as R expressions. Per the "output" documentation on the knitr webpage:
Note all options in knitr can take values from R expressions, which brings the feature of conditional evaluation introduced in the main manual. In short, eval=dothis means the real value of eval is taken from a variable named dothis in the global environment; by manipulating this variable, we can turn on/off the evaluation of a batch of chunks.
In other words if you write some chunks like:
```{r label}
doNextChunk <- as.logical(rbinom(1,1,.5))
```
```{r conditional, eval = doNextChunk}
"hello world!"
```
opts_chunk$set() is what you are after. Anything "set" will be the default for subsequent chunks (unless overwritten on a chunk-by-chunk basis)
```{r setup}
library(knitr)
opts_chunk$set(eval = TRUE, include= TRUE)
````
You can then alter as you see fit.
Related
I am working on a data set using both my laptop and a cloud facility. I want to compute some "computation-heavy" code chunks only when working on the cloud.
So far, I have chosen a not very elegant way to do so. I have added ##OPT## prefixes to segments which I want to execute only when in the cloud. I then simply remove these prefixes and run the script when in the cloud.
Now my question: is there a way that I can select in the beginning of the script once whether to execute these segments or not and then skip these segments if the argument is set to "false"? I have tried with if conditions but this is very cumbersome.
If you use an R notebook in RStudio, you can include the different code in different code chunks in the document. Code chunks are defined like:
```{r}
"hello world!"
```
Doing this allows you to quite easily run only the chunks that you want to run. Additionally, if you wish to run all of the chunks, you can do so.
Any given chunk possess an option called eval which dictates whether or not they should be run. This can take a value from an expression so you can essentially do something like:
```{r label}
is_cloud <- FALSE #or TRUE
```
```{r conditional, eval = is_cloud}
"hello world!"
```
and the chunk will be executed only if is_cloud is TRUE.
To further explain the comment of docendo discimus, just define a parameter at the beginning of your script:
execpart <- TRUE #and change to FALSE if you don't want to execute
Then wrap the whole part of your script which should only be executed situationally in:
if(execpart){
## your script
}
You could even define multiple parameters for different parts of your script at the beginning. That would give you the option to set up the execution of your script with a few quick changes.
Note that if looks for TRUE/FALSE, so you do not need to specify (execpart == TRUE) in your if-condition.
In RStudio R notebooks is is convenient to combine the nice suggestion in another answer with using "params" in the notebook header. E.g.:
---
title: params example
output: html_notebook
params:
in_cloud: yes
---
This one always runs:
```{r}
print("hello always")
```
This one only runs when in_cloud is TRUE:
```{r, eval=params$in_cloud}
print("hello, cloud")
```
Conditional blocks also work for other engines (like bash):
```{bash, eval=params$in_cloud}
echo "hello, cloud, from bash"
```
Using the chunk option eval=FALSE one can suppress chunk evaluation in an RMarkdown file or R Notebook when it is knit. Is there a way to make this apply during interactive running of the document in RStudio (i.e., making "run all chunks" skip certain chunks)?
I've got some chunks in the beginning of my analysis that take a while to run, which the later sections don't depend on. I want to be able to source the important parts of the code so I can continue writing the downstream stuff, without having to do it manually chunk-by-chunk so I can avoid the parts I don't need in my workspace for further writing.
I've set up the rmarkdown document with logical parameters meant to change which parts of the code need to get run - I meant these as control flags for when the code is actually finished and getting used, but I was hoping I could use those same parameters to exclude chunks from running in interactive mode (i.e., something like eval=params$run_part1).
Setting knitr::opts_chunk and knitr::opts_hooks only help you when knitting, not in interactive mode, so while I could be wrong I'm going to tentatively say you can't control that behavior with dynamic chunk options (yet).
As a workaround you could use interactive() and if blocks so that the code is only run when knitted. It would also mesh well with your logical parameters, despite the pain of having to be in a bracket block.
---
title: "R Notebook"
output:
html_document: default
html_notebook: default
---
```{r}
if (!interactive()) {
print("long running code")
}
```
```{r}
print(2)
```
```{r}
print(3)
```
Pressing "Run All Chunks Above":
Knitting:
I am preparing a .Rmd document that begins with an executive summary. I would like to include some inline R code to present a few of the key results up front; however, those results are calculated as part of the body later in the document.
Is there any way to present a result in the rendered document out of order/sequence with the actual calculation?
You could use chunk reuse in knitr (see http://yihui.name/knitr/demo/reference/). Here you would put your chunks to analyze first but not create the output, then output the summary, then the details. Here is some quick markdown knitr code to show this:
```{r chunk1, echo=FALSE, results='hide'}
x <- rnorm(1)
x
```
the value of x is `r x`.
```{r chunk2, ref.label='chunk1', echo=TRUE, results='markup', eval=2}
```
Note that the code will be evaluated twice unless you take steps to prevent this (the eval=2 in my example).
Another option would be to create 2 child documents, the first runs your main code and creates the output, the second creates the summary. Then in your parent document you include the summary first, then the detail part. I think that you will need to run knitr on these by hand so that you do it in the correct order, the automatic child document tools would probably run in the wrong order.
I am using knit()and markdownToHTML() to automatically generate reports.
The issue is that I am not outputting plots when using these commands. However, when I use RStudio's Knit HTML button, the plots get generated. When I then use my own knit/markdown function, it suddenly outputs the plot. When I switch to another document and knit that one, the old plot appears.
Example:
```{r figA, result='asis', echo=TRUE, dpi=300, out.width="600px",
fig=TRUE, fig.align='center', fig.path="figure/"}
plot(1:10)
```
Using commands:
knit(rmd, md, quiet=TRUE)
markdownToHTML(md, html, stylesheet=style)
So I guess there are 2 questions, depending on how you want to approach it:
What magic is going on in Rstudio's Knit HTML?
How can I produce/include without depending on RStudio's Knit HTML button?
The only issue I see here is that this doesn't work when you have the chunk options {...} spanning two lines. If it's all on one line, it works fine. Am I missing something?
See how this is not allowed under knitr in the documentation:
Chunk options must be written in one line; no line breaks are allowed inside chunk options;
RStudio must handle linebreaks in a non-standard way.
This is really embarrassing, I really thought I read the documentation carefully:
include: (TRUE; logical) whether to include the chunk output in the
final output document; if include=FALSE, nothing will be written into
the output document, but the code is still evaluated and plot files
are generated if there are any plots in the chunk, so you can manually
insert figures; note this is the only chunk option that is not cached,
i.e., changing it will not invalidate the cache
Simply adding {..., include=TRUE} did the trick. I would say it would be a pretty sensible default though.
Can anyone help me in relation to passing (or setting global) par graphical parameters across chunks in knitr.
I have a large piece of R code with a loop that generates a plot at each iteration of the loop and prints each plot on the same page of a PDF (via par(mfrow=c(5,4)).
If I break up my code into knitr chunks to make it more manageable, the graphical parameters are lost each time I leave the chunk and I lose the ability to output several plots on the same PDF page.
Is there a way to pass graphical parameters across chunks or to set the parameters as global so that all chunks can access them.
You can set the global.par knitr-option to TRUE like this:
```{r setup, include=FALSE}
opts_knit$set(global.par = TRUE)
```
Then set par in the next chunk and plot something:
```{r}
par(mfrow=c(5,4))
plot(something)
```
The same par-options will now be passed to the following chunks (until changed again).