R考试使用dplyr打包了奇怪的行为

时间:2018-11-21 14:47:48

标签: r dplyr r-exams

在加载dplyr库时,我注意到R考试包中的行为异常。下面的示例仅在我明确调用dplyr命名空间时才有效,如注释中所示。请注意,该错误仅发生在新的会话中,即您需要重新启动R才能看到我看到的内容。您需要将以下内容放在文件exam.Rmd中,然后调用

library(exams)
library(dplyr)
exams2html("exam.Rmd")  # in pwd

# this is exam.Rmd
```{r datagen,echo=FALSE,results='hide',warning=FALSE,message=FALSE}
df = data.frame(i = 1:4, y = 1:4, group = paste0("g",rep(1:2,2)))
# works:
b2 = diff(dplyr::filter(df,group!="g1")$y)
b3 = diff(dplyr::filter(df,group!="g2")$y)
# messes up the complete exercise:
# b2 = diff(filter(df,group!="g1")$y)
# b3 = diff(filter(df,group!="g2")$y)
nq = 2
questions <- solutions <- explanations <- rep(list(""), nq)
type <- rep(list("num"),nq)

questions[[1]] = "What is the value of $b_2$ rounded to 3 digits?"
questions[[2]] = "What is the value of $b_3$ rounded to 3 digits?"
solutions[[1]] = b2
solutions[[2]] = b3
explanations[[1]] = paste("You have you substract the conditional mean of group 2 from the reference group 1. gives:",b2)
explanations[[2]] = paste("You have you substract the conditional mean of group 3 from the reference group 1",b3)
```


Question
========
You are given the following dataset on two variables `y` and `group`. 

```{r showdata,echo=FALSE}
# kable(df,row.names = FALSE,align = "c")
df
```

some text with math

$y_i = b_0 + b_2 g_{2,i}  + b_3 g_{3,i} + e_i$

```{r questionlist, echo = FALSE, results = "asis"}
answerlist(unlist(questions), markup = "markdown")
```

Solution
========

```{r sollist, echo = FALSE, results = "asis"}
answerlist(unlist(explanations), markup = "markdown")
```

Meta-information
================
extype: cloze
exsolution: `r paste(solutions,collapse = "|")`
exclozetype: `r paste(type, collapse = "|")`
exname: Dummy Manual computation
extol: 0.001

2 个答案:

答案 0 :(得分:4)

感谢提出此问题,并致@hrbrmstr解释问题的一部分。但是,仍然缺少一部分解释:

  • 当然,问题的根源是statsdplyr都导出不同的filter()函数。并且取决于各种因素,首先发现哪个功能。
  • 在交互式会话中,以正确的顺序加载软件包就足够了,其中stats被自动加载,随后dplyr被自动加载。因此,它起作用了:
    library("knitr")
    library("dplyr")
    knit("exam.Rmd")
  • 花了我一会儿时间才知道有什么不同:
    library("exams")
    library("dplyr")
    exams2html("exam.Rmd")
  • 事实证明,在后一个代码块中,knit()exams2html()调用,因此NAMESPACE包中的exams会更改搜索路径,因为它会完全导入整个stats软件包。因此,除非在加载了stats::filter()的环境(如dplyr::filter()中对代码进行评估,否则dplyr会在.GlobalEnv之前找到。 (有关更多详细信息,请参见@hrbrmstr的答案)

由于没有迫切的理由要求exams包导入整个stats包,因此我将NAMESPACE更改为仅选择性地导入所需的功能(不包括filter()函数)。请从R-Forge安装开发版本:

install.packages("exams", repos = "http://R-Forge.R-project.org")

然后只需将dplyr::...包括在.Rmd中或调用library("dplyr")之前,就可以不使用exams2html()来编译.Rmd。两者现在都应该按预期工作。

答案 1 :(得分:1)

使用您的exams.Rmd,这是我要在其中输入cmd输入的源窗格:

enter image description here

(我添加了quiet=FALSE,所以我可以看到发生了什么事。)

这是输入cmd后的控制台输出:

enter image description here

这是输出:

enter image description here

如果您一直从全文阅读knit上的帮助:

  • envir要在其中评估代码块的环境,例如parent.frame()new.env()globalenv()

与您所做的相比,parent.frame()globalenv()是必需的(您似乎并不完全了解环境)。您从TRUE调用中获得了exists(),因为默认情况下 inheritsTRUE函数中为exists,并且告诉该函数“ [搜索]环境的封闭框架” (来自exists的帮助。

而且,您应该深切关注源代码和分类错误。您正在使用一种编程语言和开放源代码软件,很正确的说library(dplyr)在Rmd中不起作用,这是由于“ great” 软件包中的一些糟糕的代码选择所致,您不想指出,因为您不想看源代码。

结束,因为我无法为您做更多。我只是希望其他人能从中受益。