我有一个RMarkdown脚本,如果我手动运行块,一次运行一个或者使用“Run All”,它可以正常工作。但是当我尝试使用knitr
生成HTML或PDF时,我得到了一个错误:Error in select(responses, starts_with("Q1 ") & !contains("None")) %>% : could not find function "%>%"
实际的整行内容如下:cols <- select(responses, starts_with("Q1 ") & !contains("None") ) %>% colnames()
我正在处理一项调查中的数据,其中很多问题都是“选择尽可能多的应用”类型的问题,并且有一个开放式的“以上都不是”选项。此时,我正在抽出我想要的列(所有的Q1响应,但不是Q10或Q11响应,而不是开放式响应),所以我可以使用pivot_longer()
并总结响应。我得到一个所需的确切列名列表,然后计算值。
但是当我尝试使用knitr()
时,它在%>%
上犹豫了。
processing file: 02_Survey2020_report.Rmd
|.... | 6%
ordinary text without R code
|......... | 12%
label: setup (with options)
List of 1
$ include: logi FALSE
|............. | 19%
ordinary text without R code
|.................. | 25%
label: demographics gender
Quitting from lines 28-46 (02_Survey2020_report.Rmd)
Error in select(responses, starts_with("Q1 ") & !contains("None")) %>% :
could not find function "%>%"
Calls: <Anonymous> ... handle -> withCallingHandlers -> withVisible -> eval -> eval
Execution halted
一个简化的可重复的例子得到了同样的结果,我运行了下面的程序,得到了我所期望的结果,一个整洁的表格,上面有每个答案被选中的次数:
example <- data.frame("id" = c(009,008,007,006,005,004,003,002,001,010), "Q3_Red" = c("","","","Red","","","","Red","Red","Red"), "Q3_Blue" = c("","","","","","Blue","Blue","Blue","",""),
"Q3_Green" = c("","Green","Green","","","","","Green","",""), "Q3_Purple" = c("","Purple","","","Purple","","Purple","","Purple","Purple"),
"Q3_None of the above" = c(009,008,"Verbose explanation that I don't want to count." ,006,005,004,003,002,"Another verbose entry.",010)
)
cols <- select(example, starts_with("Q3") & !contains("None") ) %>% colnames()
example %>%
pivot_longer(cols = all_of(cols),
values_to = "response") %>%
filter(response != "") %>%
count(response)
但是当我使用ctrlshiftk输出文档时,我得到了同样的错误:
processing file: 00a_reproducible_examples.Rmd
Quitting from lines 9-25 (00a_reproducible_examples.Rmd)
Error in select(example, starts_with("Q3") & !contains("None")) %>% colnames() :
could not find function "%>%"
Calls: <Anonymous> ... handle -> withCallingHandlers -> withVisible -> eval -> eval
Execution halted
为什么编织者在烟斗前犹豫不决?
1条答案
按热度按时间j0pj023g1#
最近我遇到了类似的问题。2不确定是否有更复杂的解决方案,但是在每个代码块中加载库对我来说都很有效。3要显示代码的结果,请添加message = FALSE。
示例: