Setting document title in Rmarkdown from parameters

后端 未结 3 689
星月不相逢
星月不相逢 2020-12-02 08:33

I\'ve got an Rmarkdown template that works well, and I\'ve parameterized it so I can generate variants of the same report from different data sources. However, I\'d like to

3条回答
  •  不知归路
    2020-12-02 08:48

    Try to use a second YAML metadata block, and put the parameterized metadata in there.

    I got the following code to work as expected (i.e., producing a document title from the list of params):

    ---
    output: html_document
    params: 
        set_title: "My Title!"
    ---
    
    ---
    title: `r params$set_title`
    ---
    

    The RMarkdown documentation notes that YAML metadata blocks are combined by Pandoc. Use the first block to define the parameter set, and the second one to use the parameters as metadata. Knitr will execute the R code to interpret the parameters in the second block.Then Pandoc will merge the metadata blocks together.

    Update (2017):

    This can be accomplished in a single block, like so:

    ---
    output: html_document
    params: 
        set_title: "My Title!"
    title: "`r params$set_title`"
    ---
    

    This works because the title comes after the params definition. I put quotes around the in-line R code to prevent "Scanner errors".

提交回复
热议问题