从数据框列中随机抽取测验问题的错误答案,而不是手工做

Randomly take wrong answers of a quiz question from dataframe column, instead of doing by hand

我尝试使用 learnr 包制作一种学习工具。我们有一个问题和答案,其中一个是正确的:

我不想每次都手动输入错误答案,而是想从数据框中随机获取错误答案:

df1 <- tibble(letters = LETTERS, Number = 1:26)

如何从第 Number 列中随机获取答案 1、2 和 4 的单元格值。正确答案是答案 Nr.3。

在第一步中我尝试使用 deparse(substitute(df1[1,2])) 而不是 1,但失败了。

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

```{r setup, include=FALSE}
library(learnr)
library(tidyverse)
knitr::opts_chunk$set(echo = FALSE)
df1 <- tibble(letters = LETTERS, Number = 1:26)
```


Question 1:

```{r quiz}
# This works not
quiz(
  question("Which number has E?",
    answer(deparse(substitute(df1[1,2]))),
    answer(deparse(substitute(df1[4,2]))),
    answer(deparse(substitute(df1[5,2])), correct = TRUE),
    answer(deparse(substitute(df1[8,2])))
  ),
  # This one works
  question("Which number has E?",
    answer("1"),
    answer("4"),
    answer("5", correct = TRUE),
    answer("8")
  )
)
```

输出:

我们需要转换为characterclass

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

```{r setup, include=FALSE}
library(learnr)
library(dplyr)    
knitr::opts_chunk$set(echo = FALSE)
df1 <- tibble(letters = LETTERS, Number = 1:26)
```


Question 1:

```{r quiz}
# This works not
quiz(
 question("Which number has E?",
       answer(as.character(df1[[2]][1])),
       answer(as.character(df1[4,2])),
       answer(as.character(df1[5,2]), correct = TRUE),
       answer(as.character(df1[8,2]))         
  ),
  # This one works
  question("Which number has E?",
       answer("1"),
       answer("4"),
       answer("5", correct = TRUE),
       answer("8")
  )
 )
 ```

-输出