How can I generate .rmd files with param values set programmatically

Viewed 82

I have been using this solution to generate r-markdown reports from an interactive (Shiny) app.

Rather than generate and download a html file, I'm looking generate the corresponding .rmd file, since I want to be able to rerun the report with fixed settings at a later point in time.

To provide an example, using the template provided in the link, and for n=40, I want to generate a file containing the following code:

---
title: "Dynamic report"
output: html_document
params:
  n: 40
---

```{r}
# The `params` object is available in the document.
params$n
```

In other words, a file in which the placeholder 'NA' from the template is replaced by the actual used value of 40.

Short of manipulating the yaml in the file directly, is there a way to set new parameters in a rmd template and generate the resulting .rmd file?

1 Answers

An extremely simple version to help with your parameters Rmarkdown question. This requires Rstudio. Instead of regularly knitting your document, you knit with parameters...

enter image description here

Now we get a GUI interface where we can specify the value we want, in this case we made it "numeric"

enter image description here

---
title: My Document
output: html_document
params:
    input: numeric
---


`r params$input`

Which renders and includes our interactive parameter inside the Rmarkdown HTML rendered document. resources I used for research: resource 1 and resource 2. You can do wayy more with parameters, but this was a simple answer to help provide you with an answer to your question.

enter image description here

Related