Is there a way to use the choices of a rmarkdown parameter as a vector inside the report?

Viewed 34

I have a rmarkdown report, and below is the header

title: ""
author: ""
date: "`r Sys.Date()`"
output:
  html_document:
    keep_md: yes
    theme: united
    df_print: kable
editor_options: 
  markdown: 
    wrap: 72
params:
  TP:
    label: "Time Point"
    input: select
    choices: [3, 10, 22]
  Integrated:
    label: "Integrated"
    value: TRUE

Inside the report, I need to extract the choices of the params$TP as a vector as follows.

if (params$Integrated == FALSE){
    data=read.csv(paste0("DataATTimePoint", params$TP, ".csv")) } else {
    data=lapply(params$TP$choices, function(x) read.csv(paste0("DataATTimePoint", x,".csv")))
    data=do.call("rbind",data}

But it’s not the right way because (writing params$TP$choices is not correct). What is the right way to do this?

1 Answers

I think what you are after is knitting with multiple parameters, and do something based on the values selected. The following will access and print the parameters selected, if the document is Integrated. The key is to add multiple: yes in the YAML header, and to give a default value in the value field.

To get all the choices, knitr::knit_params is the helper function that reads YAML headers. It takes a text input of the document to be converted, in this case, if within an interactive environment, can be obtained via rstudioapi::getActiveDocumentContext().

---
title: ""
output: html_document
params:
  TP:
    label: "Time Point"
    input: select
    multiple: yes
    choices: [3, 10, 22]
    value: [3, 10]
  Integrated:
    label: "Integrated"
    value: TRUE
---

```{r}
if(params$Integrated) {
    params$TP
}
```
Grab all possible choices from within the document
```{r}
knitr::knit_params(rstudioapi::getActiveDocumentContext()$contents)$TP$choices
```

enter image description here

Related