Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to pass a list as parameter in quarto to later loop through that list

I want to pass a list as parameter so that I can loop through that list and print something. Here is a simple example:

---
title: "`r params$report_title`"
author: "`r params$name`"
format: html
params:
  report_title: "main title"
  name: "author name"
  p_list: list("A", "B")
---

Printing each element of the list...

```{r}
#| echo: true
for(p in params$p_list){
  print(p)
}
```

Printing the list...
```{r}
#| echo: true
print(params$p_list)
```

Output for first chunk

[1] "list(\"A\", \"B\")"

for second chunk

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[1] "list(\"A\", \"B\")"

How to untangle this list so that I can print A and B separately? thanks in advance.

>Solution :

Here params$p_list is a string and we turn this into an R list using eval(parse(text = params$p_list)).

---
title: "`r params$report_title`"
author: "`r params$name`"
format: html
params:
  report_title: "main title"
  name: "author name"
  p_list: list("A", "B")
---

Printing each element of the list...

```{r}
#| echo: true
for(p in eval(parse(text = params$p_list))){
  print(p)
}
```

Printing the list...
```{r}
#| echo: true
print(eval(parse(text = params$p_list)))
```

list as parameters


See the Q/A Evaluate expression given as a string for more potential options to solve your case.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading