Search code examples
rshinyr-markdownlearnr

Create an open ended question with learnr:tutorial in r


I want to allow students to input their student ID at the beginning of every quiz they take. There are no correct answers as long as they input a 7 digit number. This is what I have right now but the function does not run when the defined answers are sot one single text. How can I accept all possible entries to a question?

library(gtools)
library(learnr)

id <- permutations(10, 7, c(1,2,3,4,5,6,7,8,9,0))
question_text(
  "Enter your student ID",
    answer(id, correct = TRUE),
  allow_retry = TRUE,
  trim = TRUE
)

EDIT

I ended up using the classis learnr question_text:

```{r student_id, echo=FALSE}

id_matrix  <- permutations(10, 7, c(1,2,3,4,5,6,7,8,9,0))
id <- apply(id_matrix,1,function(x) paste0(x,collapse = ''))

do.call(question_text, c(
  list("Enter your student ID:"),
  lapply(id, answer, correct = TRUE),
  list(
    incorrect = "Your student ID is a 7 digit number on your Husky One Card",
    allow_retry = TRUE,
    trim = TRUE
  )
))
```

I checked for 3 digit numbers and it works. The new problem is that it takes a very long time to run the document (it has been 25 mins so far). Any suggestions for making this faster?


Solution

  • You could use shinyFeedback and a server chunck to input the student's ID:

    ```{r, echo=FALSE}
    library(shinyFeedback)
    useShinyFeedback()
    textInput("id", "Enter your ID")
    verbatimTextOutput("value")
    ```
    
    ```{r, context="server"}
    observeEvent(input$id, {
        
        if (nchar(input$id) != 7 & !is.na(as.numeric(input$id))) {
          showFeedbackWarning(
            inputId = "id",
            text = "Enter 7 digits"
          )  
        } else {
          hideFeedback("id")
        }
        
      })
    
    

    enter image description here