我已经使用书本创建了“书”。我希望能够添加交互式测验,而无需闪亮等。
是否可以将R /考试(http://www.R-exams.org/)与书本一起使用?我主要对HTML输出感兴趣; PDF输出有奖金,但几乎不是必需的。该网页提供了承诺:
基于(潜在地)动态练习模板,可以为各种系统创建大量的个性化考试/测验/测试:以及创建自定义输出的可能性(PDF,HTML,Docx等) 。
运动类型包括多项选择题或单项选择题,数字或文本答案或这些的组合。格式化可以在Markdown或LaTeX中完成,并可以使用R生成动态内容,例如随机数,图形,数据集或混排的文本块。
听起来不错。有谁知道是否可以将考试与书本一起使用(即使只是某些功能)?
[如果可能:如何?有指针吗?
如果不可能:有人知道做类似事情的方法吗?
R /考试确实是可扩展的,利用其构建块相对容易。所有exams2xyz()
接口基础上的主力函数称为xexams()
。它分为四个步骤:
sweave
:将练习文件复制到一个临时目录,然后通过R运行,默认情况下使用xweave()
,它为utils::Sweave()
(对于Rnw文件)和knitr::knit()
(对于Rmd文件)提供了统一的便捷界面)。read
:默认情况下使用read_exercise()
将生成的编织文件读入R。对于每个练习,都会产生一个question
,questionlist
,solution
,solutionlist
,metainfo
和supplements
的列表。所有元素都始终存在,但可能为空,例如,当练习中没有提供解决方案环境或没有补充文件时。transform
:默认情况下为空,但可用于将上面的练习列表元素转换为所需的格式,例如HTML。write
:默认情况下为空,但可用于为考试的每个n
复制内容写出结果。当您在R / Markdown(Rmd)文件中编写练习时,可以轻松地通过xexams()
运行它们以获取它们的一些随机版本。例如,让我们考虑衍生练习的数字(num
)和单选(schoice
)版本,请参见:deriv,deriv2。使用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。 Dale Barr和Lisa DeBruine的webex包是一种用于生成此类输出的基于R的轻量级框架。
在webex
中,您可以通过fitb()
为具有可选公差的数字解(在R /考试中为num
)或对于字符解决方案(在R /考试中为string
)创建空白互动。同样,您可以通过mcq()
为单选题(R /考试中的schoice
)创建下拉菜单交互。 ((注:关于选择问题的行话是不统一的:R /考试称为单选的也称为多项选择。在这种情况下,R /考试称为多选的通常使用多重答案。 )
此刻,webex
不支持单选按钮来代替下拉菜单。此外,多项选择题(也称为多项答案)的复选框不可用。
[下面,我说明如何在schoice
中嵌入简单的num
,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
和webex.js
。
[处理包含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>"