I use ProjectTemplate and Knitr to produce reports. Most of the analysis is stored in the src directory, whilst the report contains the presentation R markdown.
I would like the main text to include only the results of the analysis, and the document appendix to contain some code chunks from the analysis. The only way I have found to achieve this is as follows:
First, run the actual analysis in the main body of the document:
```{r runanalysis, warning=FALSE, message=FALSE}
# run the analysis code to generate the objects
source('../src/rf-model-caret.R')
```
Secondly, in the appendix, two knitr chunks are needed. The first reads in the actual code (and executes it). The second displays the code.
```{r analysis, eval=TRUE, echo=FALSE}
knitr::read_chunk('../src/rf-model-caret.R')
```
```{r analysis2, ref.label="analysis", eval=FALSE, echo=TRUE}
```
This works but seems very inefficient because:
The analysis has to be run twice - firstly in the source in the main document, and again in the appendix just to produce the code.
reading a knitr chunk and then referencing it again immediately to display the code
Is there a better way to achieve the goal of executing external source in the main document and printing the code in the appendix?
You may try this:
In the main body:
```{r runanalysis, code=readLines('../src/rf-model-caret.R'), echo=FALSE, eval=TRUE}
```
In the appendix:
```{r runanalysis, code=readLines('../src/rf-model-caret.R'), echo=TRUE, eval=FALSE}
```
Related
I am writing a document with fairly resource intensive R code. I want to prevent execution of one block of R code in knitr which is giving me document timeout error in Overleaf.
In R studio, this can be done using eval = FALSE. I want to recreate this in knitr. So far, the only way I have found is to suppress errors using <<setup, include=FALSE, cache=FALSE>>= muffleError <- function(x,options) {} but it only works on the entire document.
I specifically want to prevent evaluation but show the R code.
Is this what you want to do, or have I misunderstood? The eval = FALSE is in one code chunk and the second chunk still plots.
---
title: "A Test Knit"
output: html_document
---
## Show code but don't run
```{r, eval = FALSE}
summary(cars)
```
## Run and render plot
```{r}
plot(pressure)
```
I use ProjectTemplate and Knitr to produce reports. Most of the analysis is stored in the src directory, whilst the report contains the presentation R markdown.
I would like the main text to include only the results of the analysis, and the document appendix to contain some code chunks from the analysis. The only way I have found to achieve this is as follows:
First, run the actual analysis in the main body of the document:
```{r runanalysis, warning=FALSE, message=FALSE}
# run the analysis code to generate the objects
source('../src/rf-model-caret.R')
```
Secondly, in the appendix, two knitr chunks are needed. The first reads in the actual code (and executes it). The second displays the code.
```{r analysis, eval=TRUE, echo=FALSE}
knitr::read_chunk('../src/rf-model-caret.R')
```
```{r analysis2, ref.label="analysis", eval=FALSE, echo=TRUE}
```
This works but seems very inefficient because:
The analysis has to be run twice - firstly in the source in the main document, and again in the appendix just to produce the code.
reading a knitr chunk and then referencing it again immediately to display the code
Is there a better way to achieve the goal of executing external source in the main document and printing the code in the appendix?
You may try this:
In the main body:
```{r runanalysis, code=readLines('../src/rf-model-caret.R'), echo=FALSE, eval=TRUE}
```
In the appendix:
```{r runanalysis, code=readLines('../src/rf-model-caret.R'), echo=TRUE, eval=FALSE}
```
I am using Rmarkdown and Knitr using Rstudio.
The following both print script and output to html.
```{r}
summary(cars)
```
However the following will only print the output, that is embedded plot.
```{r, echo=FALSE}
plot(cars)
```
I have situation different than above, I want to present the script but should not run in html as this will take very long time (hours if not days) to run. So I just did was put comment sign.
```{r}
#summary(cars)
```
But I need a better way to do this - Is there any better way presenting script without running it.
eval = FALSE
Checkout The R Markdown Cheat Sheet http://blog.rstudio.org/2014/08/01/the-r-markdown-cheat-sheet/
It summarizes the options for code chunks
I am using knitr and pandoc to write reports into word (we need to be able to circulate for comments using track changes etc).
It is working very well so far, but I have found that the plots are all coming out with captions at the bottom, and I don't want captions. While I could just delete them in the word doc, if I can stop them showing in the code it would be better.
So for the following code in markdown:
Test test test
```{r}
summary(cars)
```
You can also embed plots, for example:
```{r fig.width=7, fig.height=6}
plot(cars)
```
I then run the following code in R:
library("knitr")
# Stackoverflow table test 1.html
knit2html("captiontest.rmd")
FILE <- "captiontest"
system(paste0("pandoc -o ", FILE, ".docx ", FILE, ".md"))
And the graph, in the word document, has the caption "plot of chunk unnamed-chunk-2"
I know I can change this caption, e.g. {r fig.width=7, fig.height=6, fig.cap='hello'}, but I thought that fig.cap=NULL would make it hidden. Instead it seems to make the whole plot disappear.
Are plots required to have a caption - do I just have to go through each word doc and delete them manually? Or is there a way to hide them?
Kind of a dirty trick, but:
You can set fig.cap="" on the chunk in question:
Test test test
```{r}
summary(cars)
```
You can also embed plots, for example:
```{r fig.width=7, fig.height=6, fig.cap=""}
plot(cars)
```
Or, you can set fig.cap="" for all chunks at once in an initializing chunk at the beginning of your Rmd document:
Test test test
```{r options-chunk}
opts_chunk$set(fig.cap="")
```
```{r}
summary(cars)
```
You can also embed plots, for example:
```{r fig.width=7, fig.height=6}
plot(cars)
```
I'm using knitr to embed R code within a markdown (.Rmd) file. I'm converting the file to a PDF file with Pandoc. The contents of my .Rmd file looks like this:
Report
========================================================
This is my report. Some analysis is included in LINK TO CHUNK NAMED MY.ANALYSIS HERE
```{r my.analysis, echo=FALSE}
summary(cars)
```
Where it says LINK TO CHUNK NAMED MY.ANALYSIS HERE, am I able provide a link to the code chunk named my.analysis in the outputted PDF?
I believe the OP asked a similar, but slightly different question here: figure captions, references using knitr and markdown to html
Do this:
Report
========================================================
This is my report. Some analysis is included in \href{http://stackoverflow.com/q/16445247/1000343}{LINK TO CHUNK NAMED MY.ANALYSIS HERE}
```{r my.analysis, echo=FALSE}
summary(cars)
```
And then covert the markdown file (not Rmd) to the PDF.
This also works using the devel version of reports:
```{r setup, include=FALSE}
# set global chunk options
opts_chunk$set(cache=TRUE)
library(reports); library(knitr);
```
Report
========================================================
This is my report. Some analysis is included in `r HR("http://stackoverflow.com/q/16445247/1000343", "LINK TO CHUNK NAMED MY.ANALYSIS HERE")`
```{r my.analysis, echo=FALSE}
summary(cars)
```
You then convert the html file to pdf.