Is there a way to specify a nested group of params in RMarkdown YAML? If not, should there be?

I would quite like to do something like the following in an RMarkdown YAML header:

---
params:
  weightings:
    a: 11
    b: 12
    ...
---

so that I could then do this in the body of the document:

weightings <- unlist(params$weightings)

rather than

weightings <- c(params$weightings$a, params$weightings$b, ...)

Of course that YAML approach above doesn't work for params, because when you indent under params you are expected to be providing labels and values for the Shiny parameters interface.
Does anyone else think that such a nesting feature for params would be useful? Or is there a way to do this currently, that I haven't thought of?

My best approach currently would be something like:

---
params:
  weighting_a: 11
  weighting_b: 12
  ...
---
weightings <- unlist(subset(params, grepl("^weighting", names(params))))

This is a bit hidden as usually params are designed for only one parameter in YAML but you could do it this way :

params:
  weightings:
    value:
      a: 11
      b: 12

This is because when subfields are passed to the main param key, it will trigger shiny inputs. See

So the value fields must be expected.

Another hidden trick is to use yaml package feature !exprto pass R code

params:
  weightings: !expr list(a = 11, b = 12)

That would be how you would pass the parameter in rmarkdown::render(... , params = list( weightings = list(a = 11, b = 12)))

This should solve your issue

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.

Some good answers to a very narrow problem, thank you @cderv.
I think I prefer the first method, because my aim would be to enable the user to set the weightings one by one in the Shiny interface to "Knit with params". If I just want to set the weightings in a more hard-coded, less user-friendly manner, I would use the list approach.

1 Like

This topic was automatically closed after 11 days. 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.