R: RMarkdown: Include R File in chunk without evaluating it [duplicate] - r

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

Display Block of R Code in Knitr With Evaluation Turned Off

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)
```

Executing external source in knitr and printing the external code chunk

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}
```

Proper R Markdown Code Organization

I have been reading about R Markdown (here, here, and here) and using it to create solid reports. I would like to try to use what little code I am running to do some ad hoc analyses and turn them into more scalable data reports.
My question is rather broad: Is there a proper way to organize your code around an R Markdown project? Say, have one script that generates all of the data structures?
For example: Let's say that I have the cars data set and I have brought in commercial data on the manufacturer. What if I wanted to attach the manufacturer to the current cars data set, and then produce a separate summary table for each company using a manipulated data set cars.by.name as well as plot a certain sample using cars.import?
EDIT: Right now I have two files open. One is an R Script file that has all of the data manipulation: subsetting and re-categorizing values. And the other is the R Markdown file where I am building out text to accompany the various tables and plots of interest. When I call an object from the R Script file--like:
```{r}
table(cars.by.name$make)
```
I get an error saying Error in summary(cars.by.name$make) : object 'cars.by.name' not found
EDIT 2: I found this older thread to be helpful. Link
---
title: "Untitled"
author: "Jeb"
date: "August 4, 2015"
output: html_document
---
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r}
table(cars.by.name$make)
```
```{r}
summary(cars)
summary(cars.by.name)
```
```{r}
table(cars.by.name)
```
You can also embed plots, for example:
```{r, echo=FALSE}
plot(cars)
plot(cars.import)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
There is a solution for this sort of problem, explained here.
Basically, if you have an .R file containing your code, there is no need to repeat the code in the .Rmd file, but you can include the code from .R file. For this to work, the chunks of code should be named in the .R file, and then can be included by name in the .Rmd file.
test.R:
## ---- chunk-1 ----
table(cars.by.name$make)
test.Rmd
Just once on top of the .Rmd file:
```{r echo=FALSE, cache= F}
knitr::read_chunk('test.R')
```
For every chunk you're including (replace chunk-1 with the label of that specific chunk in your .R file):
```{r chunk-1}
```
Note that it should be left empty (as is) and in run-time your code from .R will be brought over here and run.
Often times, I have many reports that need to run the same code with slightly different parameters. Calling all my "stats" functions separately, generating the results and then just referencing is what I typically do. The way to do this is as follows:
---
title: "Untitled"
author: "Author"
date: "August 4, 2015"
output: html_document
---
```{r, echo=FALSE, message=FALSE}
directoryPath <- "rawPath" ##Something like /Users/userid/RDataFile
fullPath <- file.path(directoryPath,"myROutputFile.RData")
load(fullPath)
```
Some Text, headers whatever
```{r}
summary(myStructure$value1) #Where myStructure was saved to the .RData file
```
You can save an RData file by using the save.image() command.
Hope that helps!

preventing a chunk run in rmarkdown

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

reference knitr chunk in R markdown

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.

Resources