knitr, , (.. ) .
, 4 .
YAML . , .
---
title: My thesis
params:
dataset: ABC
---
blah blah.
```{r child='01-introduction.rmd'}
```
```{r child='02-mathsy-maths.rmd'}
```
R .
---
title: My thesis
---
blah blah.
```{r set-params, include=FALSE}
params <- list(dataset = "ABC")
```
```{r child='01-introduction.rmd'}
```
```{r child='02-mathsy-maths.rmd'}
```
. .
knitr::knit_params(), .
---
title: My thesis
---
blah blah.
```{r def-assign-params, include=FALSE}
assign_params <- function(file) {
text <- readLines(file)
knit_params <- knitr::knit_params(text)
params <<- purrr::map(knit_params, "value")
}
```
```{r, include=FALSE}
assign_params('01-introduction.rmd')
```
```{r child='01-introduction.rmd'}
```
```{r child='02-mathsy-maths.rmd'}
```
(hacky)
hook use.params chunk: . use.params=TRUE, .
, params .
---
title: "Main document"
---
```{r hook-def, include=FALSE}
params_cache <- new.env(parent = emptyenv())
knitr::knit_hooks$set(use.params = function(before, options, envir) {
if (before && options$use.params) {
if (exists("params", envir = envir)) {
params_cache$params <- envir$params
}
text <- readLines(knitr::current_input(dir = TRUE))
knit_params <- knitr::knit_params(text)
envir$params <- purrr::map(knit_params, "value")
}
if (!before && options$use.params) {
if (exists("params", envir = params_cache)) {
envir$params <- params_cache$params
rm("params", envir = params_cache)
} else {
rm("params", envir = envir)
}
}
})
```
blah blah.
```{r child='01-introduction.rmd', use.params=TRUE}
```
```{r child='02-mathsy-maths.rmd', use.params=TRUE}
```