Is it possible to include or display an .r script in the output of .rmd file?
Important - just want to display the .r file!
Tried source(filename.r); source does not display it.
Any ideas?
**knitr Global Options**
```{r echo=TRUE}
knitr::opts_chunk$set(tidy=FALSE, fig.path='figures/')
```
**Load Libraries**
```{r echo=TRUE}
library(dplyr)
```
```{r echo=TRUE, include=TRUE}
source("external.R")
# the complete source code of the .r file should be displayed here
# possible?
```
What would be the use-case for such a requirement?
Creating .Rmd helps with documentation. In fact all my documentation is created using .Rmd.
There are .R scripts which take a long time to run (processing large data). In such a case working with .Rmd is not practical. Prefer to work with .R scripts.
If the source code of the .R can be "included & displayed" in the .Rmd would be wonderful for documentation purpose.
For this particular case, there is a simple solution. That is, you can assign source code to the chunk option code, then knitr will just take your source code as if it were written in the code chunk, e.g.
```{r, code = readLines('external.R')}
```
Alternatively and equivalently, you can use the file option:
```{r, file = 'external.R'}
```
Related
I just found this awesome technique to put the code used in the .Rmd file in the appendix (of that same file).
However, I am using R Sweave and not R Markdown and I would like to know if there exists a similar way to put all the code at the end in a unique chunk. The code to do that in Markdown does not work in Sweave. I precise that, unlike this post, I do not have a separate .R file where the calculations are made. Everything is done in the .Rnw file.
Does anybody know how to do it?
Edit : a reproducible example
\documentclass[11pt, twocolumn]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\begin{document}
\SweaveOpts{concordance=TRUE}
<<reg2, echo=FALSE, print=FALSE>>=
head(mtcars)
#
<<reg3, echo=FALSE, print=FALSE>>=
head(iris)
#
\section*{Appendix}
% the place where I could like to put the whole code
\end{document}
This chunk works to include the code:
<<echo=FALSE, eval=TRUE>>=
filename <- tempfile(fileext=".R")
Stangle("test.Rnw", output = filename, quiet = TRUE)
cat(readLines(filename), sep = "\n")
#
When I include that in your example file, I see this:
I think it's possible to modify the format a bit; see ?Rtangle for some details. Similar things are possible with knitr, but it's more flexible. I suspect the best method would be similar to the one you found for RMarkdown.
I want to use knitr to format an R markdown file, lets call it Main.rmd. Some code in Main.rmd relies on helper functions in a second file, lets call it Functions.rmd.
When I first run Functions.rmd and then Main.rmd, the code in Main.rmd runs fine. When I first run Functions.rmd and then try to knit Main.rmd, I receive an evaluation:
Error "Object 'myfunction' not found
How can I fix this without combining Main.rmd and Functions.rmd into a single document, which I would like to avoid doing?
Edit: I've added a toy example below. There are very useful suggestions so far for how to call the functions in Functions.rmd from Main.rmd, but they all require converting Functions.rmd to a .R file. However, for my current purpose, it is important that Functions.rmd can also be read as a standalone markdown document.
First, Main.rmd:
---
title: "Main_test"
author: "Matt Nolan"
date: "25/06/2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Background.
This is the main body of text and code used to display results of analyses, some of which are created by calling functions in Functions.Rmd.
```{r cars}
myexamplefunction(1,2)
```
And, here is Functions.rmd:
---
title: "Functions_test"
author: "Matt Nolan"
date: "25/06/2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Background
This is a document containing functions used in the document "Main_test".
Because it contains functions and formatted text to explain the functions for an interested reader, it should be usable as a standalone markdown document.
For example, this is a function that adds two numbers.
```{r cars}
myexamplefunction <- function(a, b) {a + b}
```
30Jun2018 Update: R Markdown does not support combining Rmd files
Matt's 25Jun2018 update clarifies the question, asking how to embed one Rmd document in another Rmd document. Per the R Markdown website, R Markdown requires a single Rmd file. It does not currently support the embedding of one Rmd file within another Rmd document.
That said, with the bookdown package, you could structure the Rmd files as chapters in a book, where each Rmd file is a chapter in the book. For details, see Bookdown: Authoring Books with R Markdown 1.4 - Two Rendering Approaches, the Getting Started page, and the Bookdown Demo github repository for an example book built in bookdown.
25Jun2018 Update: Printing the code in an Appendix
Per the comments from the OP, the reason for including the functions in an Rmd file instead of an R file was to obtain a formatted printout of the code in an Appendix. This is possible with the technique I originally posted plus a few changes.
Use named chunks to put the code in an appendix, and use the arguments echo=TRUE and eval=FALSE to avoid executing it multiple times.
Execute code from the Appendix in main flow of the document by way of the ref.label= argument, and keep the code from printing in the main document with the echo=FALSE argument.
In addition to using the source() function, one must print each function in another chunk in the appendix in order to obtain formatted print of each function.
An updated version of my example Rmd file is listed below.
---
title: "TestIncludedFiles"
author: "Len Greski"
date: "June 24, 2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Background
A question was posted on [Stackoverflow](https://stackoverflow.com/questions/51013924/calling-functions-in-a-second-file-when-compiling-rmd-files-with-knitr) about how to include functions from one Rmd file while knitting another.
If the second file contains R functions to be accessed in the second Rmd file, they're best included as R files rather than Rmd. In this example we'll include three files of functions from the Johns Hopkins University *R Programming* course: `pollutantmean()`, `corr()`, and `complete()`. We'll execute them in a subsequent code block.
After an update to the original post where the original poster noted that he included the functions in an Rmd file in order to provide a formatted printout of the code in the report as an appendix, I've modified this example to account for this additional requirement.
```{r ref.label="sourceCode",echo=FALSE}
# execute sourceCode chunk from appendix
```
## Executing the sourced files
Now that the required R functions have been sourced, we'll execute them.
```{r runCode, echo=TRUE}
pollutantmean("specdata","nitrate",70:72)
complete("specdata",1:10)
corr("specdata",threshold=500)
```
# Appendix
```{r sourceCode,echo=FALSE,eval=FALSE}
# use source() function to source the functions we want to execute
source("./rprogramming/oneLine_pollutantmean.r")
source("./rprogramming/oneLine_complete.r")
source("./rprogramming/oneLine_corr.r")
```
The following is an inventory of the functions used in this Rmd file.
```{r }
pollutantmean
complete
corr
```
...and the output for the Appendix section of the document (with redactions to avoid publishing answers to a class programming assignment).
Original Answer
If the second Rmd file only contains functions, you're better off saving them as an R file and using source() to include them in Main.Rmd. For example:
date: "June 24, 2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Background
A question was posted on [Stackoverflow](https://stackoverflow.com/questions/51013924/calling-functions-in-a-second-file-when-compiling-rmd-files-with-knitr) about how to include functions from one Rmd file while knitting another.
If the second file contains R functions to be accessed in the second Rmd file, they're best included as R files rather than Rmd. In this example we'll include three files of functions from the Johns Hopkins University *R Programming* course: `pollutantmean()`, `corr()`, and `complete()`. We'll execute them in a subsequent code block.
```{r sourceCode,echo=TRUE}
# use source() function to source the functions we want to execute
source("./rprogramming/pollutantmean.r")
source("./rprogramming/complete.r")
source("./rprogramming/corr.r")
```
## Executing the sourced files
Now that the required R functions have been sourced, we'll execute them.
```{r runCode, echo=TRUE}
pollutantmean("specdata","nitrate",70:72)
complete("specdata",1:10)
corr("specdata",threshold=500)
```
...produces the following output:
DISCLOSURE: This answer includes techniques that I previously posted as a blog article in 2016, ToothGrowth Assignment: Accessing R Code from an Appendix in Knitr.
There are a few questions about this already, but they are either unclear or provide solutions that don't work, perhaps because they are outdated:
Proper R Markdown Code Organization
How to source R Markdown file like `source('myfile.r')`?
http://yihui.name/knitr/demo/externalization/
Modularized code structure for large projects
R Markdown/Notebook is nice, but the way it's presented, there is typically a single file that has all the text and all the code chunks. I often have projects where such a single file structure is not a good setup. Instead, I use a single .R master file that loads the other .R files in order. I'd like to replicate this structure using R Notebook i.e. such that I have a single .Rmd file that I call the code from multiple .R files from.
The nice thing about working with a project this way is that it allows for the nice normal workflow with RStudio using the .R files but also the neat output from R Notebook/Markdown without duplicating the code.
Minimal example
This is simplified to make the example as small as possible. Two .R files and one master .Rmd file.
start.R
# libs --------------------------------------------------------------------
library(pacman)
p_load(dplyr, ggplot2)
#normally load a lot of packages here
# data --------------------------------------------------------------------
d = iris
#use iris for example, but normally would load data from file
# data manipulation tasks -------------------------------------------------
#some code here to extract useful info from the data
setosa = dplyr::filter(d, Species == "setosa")
plot.R
#setosa only
ggplot(setosa, aes(Sepal.Length)) +
geom_density()
#all together
ggplot(d, aes(Sepal.Length, color = Species)) +
geom_density()
And then the notebook file:
notebook.Rmd:
---
title: "R Notebook"
output:
html_document: default
html_notebook: default
---
First we load some packages and data and do slight transformation:
```{r start}
#a command here to load the code from start.R and display it
```
```{r plot}
#a command here to load the code from plot.R and display it
```
Desired output
The desired output is that which one gets from manually copying over the code from start.R and plot.R into the code chunks in notebook.Rmd. This looks like this (some missing due to lack of screen space):
Things I've tried
source
This loads the code, but does not display it. It just displays the source command:
knitr::read_chunk
This command was mentioned here, but actually it does the same as source as far as I can tell: it loads the code but displays nothing.
How do I get the desired output?
The solution is to use knitr's chunk option code. According to knitr docs:
code: (NULL; character) if provided, it will override the code in the
current chunk; this allows us to programmatically insert code into the
current chunk; e.g. a chunk option code =
capture.output(dump('fivenum', '')) will use the source code of the
function fivenum to replace the current chunk
No example is provided, however. It sounds like one has to feed it a character vector, so let's try readLines:
```{r start, code=readLines("start.R")}
```
```{r plot, code=readLines("start.R")}
```
This produces the desired output and thus allows for a modularized project structure.
Feeding it a file directly does not work (i.e. code="start.R"), but would be a nice enhancement.
For interoperability with R Notebooks, you can use knitr's read_chunk method as described above. In a notebook, you must call read_chunk in the setup chunk; since you can run notebook chunks in any order, this ensures that the external code will always be available.
Here's a minimal example of using read_chunk to bring code from an external R script into a notebook:
example.Rmd
```{r setup}
knitr::read_chunk("example.R")
```
```{r chunk}
```
example.R
## ---- chunk
1 + 1
When you execute the empty chunk in the notebook, code from the external file will be inserted, and the results displayed inline, as though the chunk contained that code.
As per my comment above, I use the here library to work with projects in folders:
```{ r setup, echo=FALSE, message=FALSE, warning=FALSE, results='asis'}
library(here)
insert <- function(filename){
readLines(here::here("massive_report_folder", filename))
}
```
and then each chunk looks like
```{ r setup, echo=FALSE, message=FALSE, warning=FALSE,
results='asis', code=insert("extra_file.R")}
```
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!
Let's say I have a project directory called testknit (and I do, see github for MRE), and inside this I have several subdirectories, including scripts where I keep .R and .rmd files.
In RStudio, I create a project and select this testknit directory so that when I open the project, the working directory is mypath/testknit.
Inside testknit/scripts I have a master.R file. If I want to source a file called testsource1.R, which is also in testknit/scripts, I can run source("scripts/testsource1.R") from within master.R.
library(knitr)
getwd()
# [1] "mypath/testknit"
source("scripts/testsource1.R")
So far so good.
But let's say I also want to knit a .rmd file called test.rmd that is located in testknit/scripts. I can run knit("scripts/test.rmd") from master.R.
My test.rmd file does the following:
```{r setup}
library(knitr)
opts_knit$set(root.dir='../')
```
```{r option1}
source("scripts/testsource2.R")
```
```{r option2}
source("testsource2.R")
```
Since test.rmd exists within testknit/scripts, I specify opts_knit$set(root.dir='../') in the first chunk so knitr knows that my root directory is really one level up.
When I open test.rmd in RStudio and click knit HTML, predictably, the option1 chunk works and the option2 chunk does not.
But when I try to knit test.rmd by running knit("scripts/test.rmd") from master.R instead of knitting from within the .rmd file, neither chunk option works. Both return an error that there is no file by that name.
What am I doing wrong? Why can't R find testsource2.R when knitting the .rmd file from the master .R?
See github link above for reproducible example.
Update:
As I noted below in the comments, I tried adding wd <- getwd() just before opts_knit$set and changed (root.dir='../') to (root.dir=wd). So when I run knit("scripts/test.rmd") from master.R, the option2 chunk runs because the wd I added gets set to mypath/testknit/scripts. But if i open the .rmd file and run all chunks, wd is set to the root directory, mypath/testknit, and the option1 chunk runs.
I need the working directory to remain the project root. This does not seem like an elegant solution to me, but changing:
```{r setup}
library(knitr)
opts_knit$set(root.dir='../')
```
to
```{r setup}
library(knitr)
wd <- ifelse(basename(getwd())=="scripts",
gsub("/scripts", "", getwd()),
getwd())
opts_knit$set(root.dir=wd)
```
lets me run all chunks when in the .rmd file or knit("scripts/test.rmd") from master.R. It works, but it feels like I am taking the wrong approach.
#Yihui: Perhaps you can make ../ an absolute path using normalizePath('../'). A relative working directory can be confusing to reason about (at least my head hurts after I read too many levels of relative paths :). BTW, when you Knit HTML in RStudio, RStudio first changes the working directory to the input Rmd file.
Me: yes! using just opts_knit$set(root.dir=normalizePath('../')) works for knitting the .rmd file from master.R and knitting to html or running all chunks from within the .rmd. I updated the github example. test-b.rmd now shows this. thanks!