在一个干净的会话中,逐行验证R脚本从头到尾的成功执行,没有错误。

huangapple go评论91阅读模式
英文:

Validating successful execution of R scripts from top to bottom in a clean session without errors

问题

I teach multiple R Courses where students have to submit an analysis they did on a specific dataset. As a first, (small) step towards reproducibility, I want to make sure the script they are submitting ran at least once on their local computer with the following constraints. It ran:

  1. in a new session
  2. from top to bottom
  3. without any errors.

Until now, I've used RMarkdown / Quarto to "enforce" this. Rendering a R/Qmd file creates a new session for the code, which is executed from top to bottom, and by default errors halt the rendering process. The resulting output file (typically PDF or HTML) gives me confidence that the above requirements are met. (Of course, there are relatively easy ways to trick me, but these require malice, which I do not assume).

What I don't like about this approach is that the students are forced to work with R/Qmds rather than R-scripts. This generates additional overhead (e.g. when debugging) and sources of errors (issues related to RMarkdown / Quarto, rather than to the code).

Another approach would be to use targets, which is a great package but also generates much overhead that would completely overwhelm the students (who are R and programming novices).

Can anyone recommend a different, simple approach to my problem?

英文:

I teach multiple R Courses where students have to submit an analysis they did on a specific dataset. As a first, (small) step towards reproducibility, I want to make sure the script they are submitting ran at least once on their local computer with the following constraints. It ran:

  1. in a new session
  2. from top to bottom
  3. without any errors.

Until now, I've used RMarkdown / Quarto to "enforce" this. Rendering a R/Qmd file creates a new session for the code, which is executed from top to bottom, and by default errors halt the rendering process. The resulting output file (typically PDF or HTML) gives me confidence that the above requirements are met. (Of course, there are relatively easy ways to trick me, but these require malice, which I do not assume).

What I don't like about this approach is that the students are forced to work with R/Qmds rather than R-scripts. This generates additional overhead (e.g. when debugging) and sources of errors (issues related to RMarkdown / Quarto, rather than to the code).

Another approach would be to use targets, which is a great package but also generates much overhead that would completely overwhelm the students (who are R and programming novices).

Can anyone recommend a different, simple approach to my problem?

答案1

得分: 1

潜在解决方案之一是学生使用 reprex 来呈现他们的提交。

优点:

  • 在干净的会话中运行他们的代码
  • 与代码一起打印警告/错误信息
  • 您可以复制/粘贴他们的代码以及输出,然后在您自己的计算机上直接运行以确认他们的提交有效
  • 相对容易使用:
    • 安装该包会安装一个 RStudio 插件
    • 您可以将您的代码/整个脚本复制到剪贴板,然后运行 reprex::reprex(wd = "." , venue = "html") 以获得一个呈现的 HTML 文件和一个 Markdown 文件,保存在您的 '当前工作目录' 以供提交
    • 您可以为 R 脚本进行 reprex,例如 reprex(input = "my_reprex.R")
  • 如果学生在 Stack Overflow 寻求帮助时,他们将知道如何创建可重现的最小示例,使我们更容易运行他们的代码,并确保他们包括他们正在使用的库,消除拼写错误等
  • 依我看,文档 非常好;如果您对 R 有基本的了解,它们易于理解和实施
  • 您可以将 sessionInfo() 添加到 reprex 中(session_info = TRUE)以帮助您支持学生解决问题/故障/错误等

缺点:

  • HTML/Markdown 提交文件可编辑(可能会引发作弊尝试,尽管在您的计算机上运行他们的提交并获得不同的校验和将会发现这一点)
  • 如果学生使用 RStudio Cloud 或类似的工具,步骤略有不同(文档中有解释,绝对不是不可逾越的问题)
  • reprex 文档 很好,但初学者可能会在某些概念上感到困难(您可能需要更详细地解释一些概念)

总的来说,我认为如果学生阅读文档并且您提供清晰的指导和对最终提交文件的期望,这是一个不错的选择。

英文:

One potential solution is the students use reprex to render their submissions.

Pros:

  • runs their code in a clean session
  • prints warnings/errors along with the code
  • you can copy/paste their code, along with their output, and run it on your own computer 'as is' to confirm their submission is valid
  • relatively straightforward to use:
    • installing the package installs an RStudio plugin
    • you can copy your code / the whole script to the clipboard then run reprex::reprex(wd = ".", venue = "html") to get a rendered html file and a markdown file in your 'current working directory' for submission
    • you can reprex an R script, e.g. reprex(input = "my_reprex.R")
  • if/when students come to stackoverflow for help, they'll know how to reprex their minimal reproducible example, making their code easier for us to run and ensuring they include the libraries they're using, eliminating typos, etc
  • in my opinion, the docs are very good; they are easy to understand and implement if you have a basic understanding of R
  • you can add the sessionInfo() to the reprex (session_info = TRUE) to help you support your students with troubleshooting/issues/bugs/etc

Cons:

  • html/markdown submission files can be edited (may invite attempts to cheat, although running their submission on your computer and getting a different checksum would catch this)
  • if students are using Rstudio cloud, or something similar, the steps are slightly different (explained in the docs, definitely not an insurmountable problem)
  • the reprex docs are very good, but 'first timers' / beginners may struggle with some of the concepts (you may need to explain some of the concepts in more detail)

Overall, I think it's a good option if the students read the docs and you provide clear instructions/expectations for the final submission file/s.

答案2

得分: 0

为什么不使用相同的方法,只是将他们的所有脚本都放在一个代码块中?通常我会给我的学生一个Quarto模板,这样头部和所有配置都已经完成。

英文:

Why not use the same method but just put all of their script in one code chunk? I typically give my students a Quarto template so that the header and all that is already configured as well.

答案3

得分: 0

另一个选项是使用函数 knitr::spin(),它可以从一个R脚本创建一个中间的RMarkdown文件,然后将其渲染成html,pdf(如果 knit = TRUE)。这个函数并不是很常见,可能因为 knitr 似乎没有官方网站(这里有一些关于 rdocumentation.orgrmarkdown-cookbook 的文档)。这里是来自 ?knitr::spin() 的一个片段:

spin                   包:knitr                   R文档

将山羊毛纺成羊毛

描述:

     该函数接受一个特殊格式的R脚本,并将其转换为一份可读的编程文档。默认情况下,普通文本(文档)应该在roxygen注释(‘#’)之后编写,代码块选项应该在‘#+’或‘#-’或‘# ----’之后编写,或者使用这些组合之一,将‘#’替换为‘--’。

...

详细信息:

     显然,山羊毛是原始的R脚本,羊毛是已准备好进行编织的可读编程文档。
英文:

Another option would be to use the function knitr::spin(), which creates an intermediate RMarkdown file from an RScript and renders this to html, pdf (if knit = TRUE). This function is not well known, maybe since knitr does not seem to have an official website (here is some documentation on rdocumentation.org and in the rmarkdown-cookbook). And here's a snippet from ?knitr::spin()

spin                   package:knitr                   R Documentation

Spin goat's hair into wool

Description:

     This function takes a specially formatted R script and converts it
     to a literate programming document. By default normal text
     (documentation) should be written after the roxygen comment (‘#'’)
     and code chunk options are written after ‘#+’ or ‘#-’ or ‘# ----’
     or any of these combinations replacing ‘#’ with ‘--’.

...

Details:

     Obviously the goat's hair is the original R script, and the wool
     is the literate programming document (ready to be knitted).

huangapple
  • 本文由 发表于 2023年6月26日 20:17:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76556608.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定