2

I have the following code with 4 correct answers. I want the students to input all 4 of them. Instead of defining 24 permutations of the answers, I want 4 field boxes that would only accept an answer once.

question_text(
  "Input all paths:",
    answer("ABEF", correct = TRUE),
    answer("ABCDG", correct = TRUE),
    answer("ABCDEF",correct = TRUE),
    answer("ABDEF", correct = TRUE),
    incorrect = "Direction from top to bottom of the plate",
  allow_retry = TRUE,
  trim = TRUE
)

EDIT

I tried this approach but I do not think I can set the answer as anything other than a single text:

library(gtools)
pat <- permutations(4, 4, c("ABEF","ABCDG","ABCDEF","ABDEF"))
question_text(
  "Input all possible rupture paths:",
    answer(pat, correct = TRUE),
  allow_retry = TRUE,
  trim = TRUE
)

Even if I set pat <- c("ABEF","ABCDG","ABCDEF","ABDEF") it does not run successfully. How can define multiple answers at the same time without writing them out.

ismirsehregal
  • 30,045
  • 5
  • 31
  • 78
Maral Dorri
  • 468
  • 5
  • 17

1 Answers1

1

I'm not sure about your desired output - however, please check the following.

Referring to:

How can define multiple answers at the same time without writing them out.

You can use lapply to create the answers and do.call to pass the different arguments to question_text:

library(learnr)

do.call(question_text, c(
  list("Input all paths:"),
  lapply(c("ABEF", "ABCDG", "ABCDEF", "ABDEF"), answer, correct = TRUE),
  list(
    incorrect = "Direction from top to bottom of the plate",
    allow_retry = TRUE,
    trim = TRUE
  )
))

as *.Rmd file:

---
title: "Tutorial"
output: learnr::tutorial
runtime: shiny_prerendered
---

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


```{r two-plus-two, exercise=FALSE}
do.call(question_text, c(
  list("Input all paths:"),
  lapply(c("ABEF", "ABCDG", "ABCDEF", "ABDEF"), answer, correct = TRUE),
  list(
    incorrect = "Direction from top to bottom of the plate",
    allow_retry = TRUE,
    trim = TRUE
  )
))
```

result

Regarding:

I want 4 field boxes that would only accept an answer once

Edit: Added an event handler to access to the answers provided by the user.

---
title: "Tutorial"
output: learnr::tutorial
runtime: shiny_prerendered
---

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

questions <-
  mapply(
    FUN = question_text,
    lapply(c("ABEF", "ABCDG", "ABCDEF", "ABDEF"), answer, correct = TRUE),
    text = paste("Question", 1:4),
    incorrect = paste("Incorrect", 1:4),
    MoreArgs = list(allow_retry = TRUE,
                    trim = TRUE),
    SIMPLIFY = FALSE
  )
```

```{r q1, echo = FALSE}
do.call(quiz, c(list(caption = "Quiz 1"), questions))
```

```{r context="server-start"}
event_register_handler("question_submission", function(session, event, data) {
  # names(data):
  # "label"    "question" "answer"   "correct"
  message("event: question_submission: ", data$answer)
})
```

result2

ismirsehregal
  • 30,045
  • 5
  • 31
  • 78
  • Thank you this is exactly what I needed to do! Is there a way to add a message if the previously entered answer was entered again "Answer already accepted" or something like that? – Maral Dorri Sep 17 '20 at 01:15
  • @MaralDorri this would require to render questions based on the result of the previous question. I'm currently not aware of a solution for that. Recently there was an [event handler system added](https://github.com/rstudio/learnr/pull/398) to learnr (dev version: `devtools::install_github("rstudio/learnr")`) so you can access the answers - please see my edit regarding this. Furthermore, you can [pass variables between exercises](https://github.com/rstudio/learnr/issues/72#issuecomment-658218762) which might be of interest. – ismirsehregal Sep 17 '20 at 08:36
  • I am not sure if I am missing something but after running the document i gett the error `Error in event_register_handler("question_submission", function(session, : could not find function "event_register_handler" Calls: ... do.call -> -> -> eval -> eval Execution halted` – Maral Dorri Sep 17 '20 at 15:01
  • You need to install the dev version: `devtools::install_github("rstudio/learnr")`. It's not on CRAN yet. – ismirsehregal Sep 17 '20 at 15:04
  • The first solution that you provide, allows me to enter any of the four solutions `c("ABEF", "ABCDG", "ABCDEF", "ABDEF")` in the text box. The second question does provide four boxes and show, but it will only accept the entered answers in the order that is given in the code. For example if a student puts the first possible answer `ABEF` in the second box, the quiz grades it as incorrect. The objective that i am trying to achieve is that the students can input the possible answers in any order, just cannot repeat the same answer for more than one box. – Maral Dorri Sep 25 '20 at 20:20