一般说明
R/exams 确实是可扩展的,利用其构建块相对容易。所有底层的主力功能exams2xyz()
接口被称为xexams()
。它分四个步骤进行:
-
sweave
:练习文件被复制到临时目录,然后通过R运行,默认使用xweave()
它提供了一个统一的便捷接口utils::Sweave()
(对于 Rnw 文件)和knitr::knit()
(对于 Rmd 文件)。
-
read
:将生成的编织文件读入 R,默认情况下使用read_exercise()
。对于每个练习,都会生成一个列表question
, questionlist
, solution
, solutionlist
, metainfo
, and supplements
。所有元素始终存在,但可能为空,例如,当练习中没有提供解决方案环境或没有补充文件时。
-
transform
:默认情况下这是空的,但可用于将上面的练习列表元素转换为所需的格式,例如 HTML。
-
write
:默认情况下这是空的,但可以用来写出每个的结果n
考试的重复。
在 Markdown 中嵌入练习文本
当您在 R/Markdown (Rmd) 文件中编写练习时,您可以轻松地运行它们xexams()
获得它们的一些随机版本。作为示例,让我们考虑数字 (num
) 和单选 (schoice
) 导数练习的版本,请参阅:deriv http://www.R-exams.org/templates/deriv/, deriv2 http://www.R-exams.org/templates/deriv2/. Using 1
作为随机种子,数值练习有以下问题以及正确的解决方案和容差:
set.seed(1)
d1 <- xexams("deriv.Rmd")[[1]][[1]]
d1$question
## [1] "What is the derivative of $f(x) = x^{2} e^{2.3 x}$, evaluated at $x = 0.56$?"
d1$metainfo$solution
## [1] 6.68
d1$metainfo$tolerance
## [1] 0.01
原因是[[1]][[1]]
索引是,这是第一次(也是唯一一次)考试,第一次(也是唯一一次)练习。如果你生成,比如说,xexams(..., n = 3)
那么第一个索引可以是 1, 2, 3。同样,如果您愿意,您可以包含多个练习。
单选版本有
set.seed(1)
d2 <- xexams("deriv2.Rmd")[[1]][[1]]
d2$question
## [1] "What is the derivative of $f(x) = x^{2} e^{2.3 x}$, evaluated at $x = 0.66$?"
## [2] ""
d2$questionlist
## [1] "$8.01$" "$14.09$" "$10.59$" "$15.35$" "$6.02$"
d2$metainfo$solution
## [1] FALSE FALSE TRUE FALSE FALSE
这两者都可以很容易地作为静态文本集成到任何 R/Markdown 文档中。
将练习文本嵌入webex
为了将静态文本转换为 HTML 中的动态元素,例如读者可以输入数字的文本字段,然后将其与解决方案中的参考值进行比较,可以使用 JavaScript 等。一种用于生成此类输出的基于 R 的轻量级框架是webex https://CRAN.R-project.org/package=webex戴尔·巴尔 (Dale Barr) 和丽莎·德布鲁恩 (Lisa DeBruine) 设计的包装。
In webex
您可以通过以下方式创建填空交互fitb()
对于具有可选公差的数值解(num
在 R/考试中)或用于字符解决方案(string
在 R/考试中)。此外,您还可以通过以下方式创建下拉菜单交互mcq()
对于单选题(schoice
在 R/考试中)。(注:有关选择题的行话不统一:R/exams 所说的单项选择也称为多项选择。在这种情况下,多选题通常用于 R/exams 所说的多项选择。)
眼下,webex
不支持单选按钮作为下拉菜单的替代。此外,多项选择(也称为多项答案)问题的复选框不可用。
下面我简单说明一下如何嵌入schoice
, num
, and string
中的问题webex
。有关补充文件的更详细示例,请参阅下面的评论。还,cloze
也是可行的,但需要更多的工作。
---
title: "Web Exercises with R/exams & webex"
output: webex::webex_default
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = TRUE)
library("webex")
library("exams")
```
`r style_widgets("#DF536B", "#61D04F")`
## `schoice`
```{r swisscapital, echo = FALSE, results = "asis"}
x <- xexams("swisscapital.Rmd")[[1]][[1]]
names(x$questionlist) <- ifelse(x$metainfo$solution, "answer", "")
x <- c(
x$question,
"",
mcq(x$questionlist),
"",
hide("Correct solution"),
"",
x$solution,
"",
paste("*", x$solutionlist),
"",
unhide()
)
writeLines(x)
```
## `num`
```{r deriv, echo = FALSE, results = "asis"}
x <- xexams("deriv.Rmd")[[1]][[1]]
x <- c(
x$question,
"",
fitb(x$metainfo$solution, tol = x$metainfo$tol,
width = min(100, max(20, nchar(x$metainfo$solution)))),
"",
hide("Correct solution"),
"",
x$solution,
"",
unhide()
)
writeLines(x)
```
## `string`
```{r function, echo = FALSE, results = "asis"}
x <- xexams("function.Rmd")[[1]][[1]]
x <- c(
x$question,
"",
fitb(x$metainfo$solution, width = min(100, max(20, nchar(x$metainfo$solution)))),
"",
hide("Correct solution"),
"",
x$solution,
"",
unhide()
)
writeLines(x)
```
渲染这个rmarkdown::render()
为您提供一个如下面的屏幕截图所示的文件。当嵌入这个bookdown
你需要确保嵌入webex.css
and webex.js
从包装中。
进一步的变化
处理包含图像的练习时会涉及一些额外的工作,例如boxplots http://www.R-exams.org/templates/boxplots/。默认在xexams()
设置为 PDF 输出,但driver$sweave
可以调整以产生 PNG 输出。无论哪种情况,supplements
是补充文件的文件路径向量:
set.seed(1)
b1 <- xexams("boxplots.Rmd", driver = list(sweave = list(png = TRUE)))[[1]][[1]]
b1$question
## [1] "In the following figure the distributions of a variable"
## [2] "given by two samples (A and B) are represented by parallel boxplots."
## [3] "Which of the following statements are correct? _(Comment: The"
## [4] "statements are either about correct or clearly wrong.)_"
## [5] "\\"
## [6] "![](boxplot-1.png)"
## [7] ""
b1$supplements
## boxplot-1.png
## "/tmp/RtmpA07Hau/file11d77d212e69bf/exam1/exercise1/boxplot-1.png"
## attr(,"dir")
## [1] "/tmp/RtmpA07Hau/file11d77d212e69bf/exam1/exercise1"
此外,您还可以设置一个transform
已将 R/Markdown 转换为 HTML 的驱动程序(而不是bookdown
稍后再做)。这里我选择的是pandoc
作为转换器,使用 MathJax 来呈现数学内容(例如bookdown
也是如此)。使用base64 = TRUE
而不是FALSE
下面将使用 Base 64 编码将补充 PNG 图像直接嵌入到 HTML 代码中。
set.seed(1)
htmltrafo <- make_exercise_transform_html(converter = "pandoc-mathjax", base64 = FALSE)
b2 <- xexams("boxplots.Rmd", driver = list(sweave = list(png = TRUE), transform = htmltrafo))[[1]][[1]]
b2$question
## [1] "<p>In the following figure the distributions of a variable given by two samples (A and B) are represented by parallel boxplots. Which of the following statements are correct? <em>(Comment: The statements are either about correct or clearly wrong.)</em><br />"
## [2] "<img src=\"boxplot-1.png\" /></p>"