Access parameters in bookdown chapters

Hi there,

I'm working on the report using {bookdown} package. I'm using "Knit and Merge" rendering approach to knit each .Rmd in fresh session. The problem, I'm facing, is how to use parameters that I set in yaml header of index.Rmd, in the all chapters?

Hi @Dobrokhotov1989,

You should be able to declare your parameters at the top-level of the YAML header in your index.Rmd file (or your _output.yaml file, if used), such as:

title: "Whatever"
output: bookdown::gitbook
  foo: "bar"

And in any of the downstream chapters/files

# Chapter 1

This is my custom parameter: `r params$foo`!.

I've thought it should be so. However, setting params in the index.Rmd works only for the rendering book with the "merge and knit" approach. Probably because with "knit and merge" each .Rmd knitted in a fresh session the params from the index header also get washed away.

I've just tried to put params in _output.yml, but I can't make it work in both rendering approach. In minimal book example I've added params to _output.yml and call it in index.Rmd. Build is stopped with error.

This is _output.yml with 3 last lines added by me + I changed link title from "A Minimal ..." to "My Minimal..." just to confirm that file is actually accessed during the build.

  css: style.css
      before: |
        <li><a href="./">My Minimal Book Example</a></li>
      after: |
        <li><a href="" target="blank">Published with bookdown</a></li>
    download: ["pdf", "epub"]
    in_header: preamble.tex
  latex_engine: xelatex
  citation_package: natbib
  keep_tex: yes
bookdown::epub_book: default
  refresh_rds: false
  refresh_imagej: true

Then in index.Rmd I've added simple chunk just after # About header (w/o backslash at the last line):

```{r include=TRUE, echo=TRUE}

When same params setted in the header of index.Rmd it works perfectly.

As a workaround, I've created a .R file with all the params that I initially had in yaml header and then source this file at the first chunk in each .Rmd (put it in template, of course). The result is the same as with yaml header in "merge and knit" rendering but need a couple extra steps.
Hope it can be somehow fixed in the future releases of {bookdown}.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.