2

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?

Maral Dorri
  • 468
  • 5
  • 17

2 Answers2

1

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

Waldi
  • 39,242
  • 6
  • 30
  • 78
  • 1
    @Maral Dorri, before trying to answer the other questions you posted, I would appreciate to know whether the above solution worked. Thanks for your feedback. – Waldi Sep 16 '20 at 17:07
  • Thank you Waldi, I do get the box but I am not sure how to add a submit button so I can record the answer they input. I am currently recording the submitted answers with `new_recorder <- function(tutorial_id, tutorial_version, user_id, event, data) { cat(tutorial_id, " (", tutorial_version, "): ", user_id, ", ", event, ", ", data$label, ", ", data$answers, ", ", data$correct, "\n", sep = "", file = "save.txt", append = TRUE) } options(tutorial.event_recorder = new_recorder)` – Maral Dorri Sep 17 '20 at 01:07
  • 1
    You could call `new_recorder(...)` in the `else` section of `observeEvent`. – Waldi Sep 17 '20 at 08:56
  • Thank you for the response, but I am not sure how to have this append the entered digits. The new_recorder should record any event. But since there is no option to submit it is not recording the entered value for the student id – Maral Dorri Sep 17 '20 at 14:00
  • 1
    You're right, save.txt is going to be written each time 7 digits are reached without asking for submission. However, in the else section, as learnr works on shiny, it's probably possible to display a submit button : this is beyond the scope of this question but feasible. – Waldi Sep 17 '20 at 14:20
0

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
  )
))
```
Maral Dorri
  • 468
  • 5
  • 17