Reputation: 5883
This seems like a very basic question, but somehow I cannot find the solution anywhere. I am trying to pass variables to R markdown child documents.
In the parent document, I have this chunk:
```{r}
var1 = "test-var1!"
cat(knit_child("child.Rmd"), sep = "\n")
```
In the child document, if I use ls()
, I can see var1
is in the environment. However, if I try to use var1
, I get a knit error:
Error in str(var1) : object 'var1' not found
Calls: <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval -> str
I tried knitting in RStudio and on the command line.
Is there a way to use objects in the child document?
Upvotes: 1
Views: 2017
Reputation: 5272
I have created a MWE with two defaults files form RStudio
and I cannot reproduce your error, though I guess the ouput is not the one you are looking for.
Parent.Rmd file:
---
title: "Parent"
author: "Clement Walter"
date: "27/11/2017"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
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}
var = 1
cat(knitr::knit_child("Child.Rmd"), sep = "\n")
```
Child.Rmd file:
## R Markdown
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}
ls()
var + 1
```
Upvotes: 3