0

I am trying to knit an parameterized R markdown file using two child .Rmd files. For some reason with the second child .Rmd file, I get the following error

 Error in readLines(if (is.character(input2)) { : 
  object 'input2' not found 

I don't have a deep understanding of what is happening for within knitr::knit() which I suspect is giving the error. Any insight would be appreciated!


The premise is that I am trying to create a dynamic markdown file where the child document cat_var2.Rmd creates a table and plot for each categorical variable selected in a data set. The params$data object can be any type of data set containing factor or character type columns.

I've included the code below:

parent.Rmd

---
title: "parent document"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r mydata, include=FALSE}
my_data <- iris
```

```{r cat_log, include=FALSE}
cat_log <- TRUE #default
if (length(names(dplyr::select_if(my_data, funs(is.character(.) | is.factor(.))))) < 1) {
  cat_log <- FALSE
}
```

`r if (cat_log) {"# Categorical Variables"}`
```{r catgorical_vars-md, include=FALSE, message=FALSE}
out_catgorical <- NULL

for (v in names(dplyr::select_if(my_data, funs(is.character(.) | is.factor(.))))) {
  out_catgorical <- c(out_catgorical, knitr::knit_expand('child2.Rmd'))
}
```
`r if (cat_log) paste(knit(text=out_catgorical), collapse = '\n')`

child2.Rmd

## Variable: {{v}}

```{r table-{{v}}, echo=FALSE}
var_cat <- '{{v}}'

my_table <- table(my_data[var_cat]) #make a table
```
EJJ
  • 1,474
  • 10
  • 17
  • Your error is saying that `input2` is not defined within your environment. Is this defined somewhere else? Also, please try and remove all unnecessary code and provide only a [minimal example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). This makes it much easier to work out where the problems may be – Michael Harper Dec 05 '18 at 16:59
  • Try to replace `knit(text=out_catgorical)` with `knit_child(text=out_catgorical)`. And yes, please do make your example minimal. I don't think dplyr or ggplot2 is relevant. – Yihui Xie Dec 05 '18 at 17:15
  • I've removed the dplyr and ggplot2. @MichaelHarper `input2` is not being defined anywhere. – EJJ Dec 05 '18 at 17:42
  • @YihuiXie I've tried using `knit_child()` and changed last line in the child doc to `r paste(out_catgorical, collapse = '\n')` previously but ran into issues where RStudio would time out and never finish the rendering. I then decided to use a method I found [here](https://gist.github.com/rmoff/a043676a2f084b81a434) – EJJ Dec 05 '18 at 17:58

0 Answers0