1

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")
  )
)
```

输出: 在此处输入图像描述

4

1 回答 1

1

我们需要转换为character

---
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")
  )
 )
 ```

-输出

在此处输入图像描述

于 2022-01-29T18:24:56.013 回答