Parameters aren't passed with `rmarkdown::render()`

I am trying to create a parameterised Rmarkdown document with custom template that can be edited via a shiny app. When I run the document in Rstudio via the knit button, it works fine. When I run the document from console with rmarkdown::render() the parameters aren't passed through, and the defaults defined in the document are used.

Here is a minimal Rmarkdown document:

---
title: Test report
output:
  html_document: 
    template: report_template.html
params:
    text: "Foo"
---

Hello world!

With report_template.html defined as:

<!DOCTYPE html>
<head>
    <title>$title$</title>
</head>
<body>
    <h1>$title$</h1>
    You entered $params.text$
    $body$
</body>
</html>

When knitting via the knit button all works as it should and produces the result below as expected.

rmarkdown1

However when using rmarkdown::render("report.Rmd", output_file = "report.html", params = list(text = "Bar")) on the console, it produces the same result, and the parameter passed is ignored. I.e. I expect it to show

You entered Bar

But it still says "Foo".
rmarkdown2

What am I doing wrong? Why are the parameters not being passed into the document?

Running Windows 10, R 4.0.0, rmarkdown 2.1. Also tested on R 3.6.3 which made no difference.

The following works for me with your report_template.html.

---
title: Test report
output:
  html_document: 
    template: report_template.html
params:
  text: "Foo"
---
  
  Hello world!
  
  My text from `params` is `r params$text` .

With the knitr button and the call rmarkdown::render('therog1.Rmd',params=list(text='blabla'))
I get the following outputs on respectively the left and right side of the screenprint

image

I think that the $params.text$ in the html file inserts the pandoc variable with the same name while the 'r params$text' construct calculates the value from the parameter you supplied (in the Rmd yaml or in the params argument for the render) in the knitr step.

So it probably better to always use the 'r params$text' construct ?

Thank you Han, I can confirm that my document works as expected with the changes you suggested, and I have come up with a way to make that work.

I think my question still remains though? Surely this is not expected behaviour, that it does different things (with respect the parameters defined in the YAML header) when pressing the knit button vs running rmarkdown::render()?

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