1

I'm using shiny to pass a large number of parameters to an rmarkdown document. This is successful, but my parameter declaration section just keeps getting longer and longer as more parameters are added.

Is there a way to simplify the declaration of parameters?

Example, I pass parameters A-Z to the rmarkdown document, right now I would write parameters in rmarkdown as:

 ---
    title: "TEST"
    params:
      A: NA
      B: NA
      C: NA
      And so on until Z
    output: word_document
    ---

This works for a small number of parameters, but I'm going to passing along 400 parameters, and feel like there should be an easier way handle a large quantity without writing line by line.

Desired output, something like:

---
    title: "TEST"
    params:
      lapply(LETTERS[1:26], function(x){paste(x,": NA")})
    output: word_document
    ---

When I search on stack or just a general net search, all examples of parameters seem to do it just one by one: https://rmarkdown.rstudio.com/lesson-6.html

Thanks!

Silentdevildoll
  • 1,187
  • 1
  • 6
  • 12
  • You can write code to generate the text required for the header. But you'll still have to list all the values *somewhere*. Otherwise I'm really not sure what you are after. If you are going to have a bunch of parameters, you're going to need to define them in order to use them somehow. It would be easier to offer specific advice with a proper [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – MrFlick Aug 07 '20 at 22:11
  • I'm sorry if my example wasn't clear. I added a desired output to help clarify. Essentially if we use A:Z as the example, how can we write the A:Z in a singular line, rather than dozens of lines? – Silentdevildoll Aug 07 '20 at 22:41

1 Answers1

2

Instead of passing single parameters to the Rmd you can pass them via a named list or vector. Try this:

Example Rmd

---
title: "test"
output: html_document
params:
  params_list: !r NULL
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

```{r}
print(params$params_list)
```

```{r}
print(params$params_list$A)
```

Render

rmarkdown::render("test.Rmd", params = list(params_list = setNames(lapply(LETTERS[1:4], function(x){paste(x,": NA")}), LETTERS[1:4])))
stefan
  • 90,330
  • 6
  • 25
  • 51