r - 为什么 knitr 会在其他工作正常的管道上窒息?
问题描述
如果我手动运行块(一次一个或使用“全部运行”),我有一个 RMarkdown 脚本可以正常工作。但是当我尝试使用它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
为什么knitr对管道犹豫不决?
解决方案
推荐阅读
- performance - 避免循环中的条件语句
- c++ - 为什么MingW编译错误?
- r - 合并到最近的索引日期
- python - tensorflow 1.10 中的`tf.group(tf.local_variables_initializer(), tf.global_variables_initializer())` 有替代方法吗?
- java - 当从 Java Repository 发送“关键字”时,如何让 PSQL 认为它是一个变量
- python - 如何减少数百万张图片的文件大小?
- javascript - 存储到MYSQL数据库时如何删除时区?
- java - 从对话框更新 TableLayout Tablerows
- javascript - innerHTML 返回 undefined - JavaScript
- python-2.7 - 在树莓派上使用 python 2.7 搜索从永远不会完成的 shell 进程返回的表达式