I would like to render multiple objects of the function quiz
of the library learnr
using a loop in one chunk. The R
is compiling the html-markdown
file without problems, but the rendering is not correct. It is rendering text and not a question as it is supposed to.
This is my reproducible code, currently not rendering correctly:
---
title: ""
output: learnr::tutorial
runtime: shiny_prerendered
description: ""
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(learnr)
```
```{r, q-part2, echo=FALSE, results='asis'}
words_lst <- structure(list(eng = c("bathroom", "bedroom", "dining room",
"door", "house", "kitchen", "garden", "neighbor", "office", "attic",
"living room"), nl = c("de badkamer", "de slaapkamer", "de eetkamer",
"de deur", "het huis", "de keuken", "de tuin", "de buurman",
"het kantoor", "de zolder", "de woonkamer")), row.names = c(NA,
-11L), class = "data.frame")
for(i in seq_along(words_lst)){
quiz(caption = paste0(i),
question_text(paste0("Translate: ", words_lst[i,1L]),
answer("C0rrect ", message = "Make sure you do not have a trailing space"),
answer(trimws(words_lst[i, 2L]), correct = TRUE),
incorrect = paste0("Gebruik: ", sub("\\s.*", "", words_lst[i,2L])),
allow_retry = TRUE,
trim = TRUE,
placeholder = "schrijf het juist antwoord"
))
}
```
I found a solution, It turns out that in one chunk it is not possible to render two or more functions quiz
.
My solution is to generate multiple question_text
in a loop, store them in a list
and then use eval
to call all the questions as arguments of quiz
. This is the solution:
```{r, q-part1, echo=FALSE, message=FALSE, results="asis"}
words_lst <- structure(list(eng = c("bathroom", "bedroom", "dining room",
"door", "house", "kitchen", "garden", "neighbor", "office", "attic",
"living room"), nl = c("de badkamer", "de slaapkamer", "de eetkamer",
"de deur", "het huis", "de keuken", "de tuin", "de buurman",
"het kantoor", "de zolder", "de woonkamer")), row.names = c(NA,
-11L), class = "data.frame")
for (j in 1:nrow(words_lst)) { list_w[[j]] <- question_text(
paste0("Translate: ", words_lst[j, 1L]),
answer("C0rrect ", message = "Make sure you do not have a trailing space"),
answer(trimws(words_lst[j, 2L]), correct = TRUE),
incorrect = paste0("Gebruik: ", sub("\\s.*", "", words_lst[j, 2L])),
allow_retry = TRUE,
trim = TRUE,
placeholder = "schrijf het juist antwoord" ) }
eval(parse(text = paste0( "quiz(", paste0("list_w[[", 1:nrow(words_lst), "]]", collapse = ', '), ")" )))
```