r - tidyeval 函数和 `View()` 的问题
问题描述
代码块 #1 和 #2 相同,除了第 14 行。代码块 #1 使用print()
调用,代码块 #2 使用View()
调用。代码块 #1 工作正常。代码块 #2 给出错误"Error in FUN(X[[i]], ...) : object 'cal.date' not found"
。为什么?
1
library(tidyverse)
set.seed(1)
graph.data <- tibble(cal.date = as.Date(40100:40129, origin = "1899-12-30"),
random_num = rnorm(30, 8, 5))
child_function <- function(df, variable, hor.line = 6) {
variable <- enquo(variable)
df <- df %>% mutate(mutation = 2 * !!variable, horizontal.line = hor.line)
}
parent_function <- function(df, date, variable, hor.line = 6) {
date <- enquo(date)
variable <- enquo(variable)
df <- df %>% child_function(!!variable, hor.line) %>% print() # LINE 14
p <- ggplot(df, aes(!!date, mutation)) +
geom_point() +
geom_hline(aes(yintercept = hor.line))
p
}
parent_function(graph.data, date = cal.date, variable = random_num, hor.line = 8)
2
library(tidyverse)
set.seed(1)
graph.data <- tibble(cal.date = as.Date(40100:40129, origin = "1899-12-30"),
random_num = rnorm(30, 8, 5))
child_function <- function(df, variable, hor.line = 6) {
variable <- enquo(variable)
df <- df %>% mutate(mutation = 2 * !!variable, horizontal.line = hor.line)
}
parent_function <- function(df, date, variable, hor.line = 6) {
date <- enquo(date)
variable <- enquo(variable)
df <- df %>% child_function(!!variable, hor.line) %>% View() # LINE 14
p <- ggplot(df, aes(!!date, mutation)) +
geom_point() +
geom_hline(aes(yintercept = hor.line))
p
}
parent_function(graph.data, date = cal.date, variable = random_num, hor.line = 8)
解决方案
View()
是一个副作用函数,不返回任何东西。
%T>%
从包装中使用,magrittr
而不是%>%
用于您的第二种情况。
View()
结束管道,这样你就会想要一个T pipe
。我想你可以像这样更清楚地看到它
df %>% child_function(!!variable, hor.line) %>% View() -> df
对比
df %>% child_function(!!variable, hor.line) %T>% View() -> df
推荐阅读
- spring-boot - 我们可以在没有 Spring Boot 的情况下实现 Spring 批处理应用程序吗?
- python - 以指定格式返回日期时间
- bash - Cron 作业从服务器获取文件,如果文件未更改,我应该在脚本中添加什么以使其在 15 分钟内再次检查?
- r - 具有来自不同数据帧的多个条件的 mutate()
- css - 如何让自定义 CSS 成为 Shiny 中的主导 CSS?
- hana - 带有输出变量的存储过程示例
- azure - 拥有的 Azure 订阅未显示在 Azure DevOps Pipeline 中
- javascript - 如何获取点击的html链接的ID?
- python - 通过搜索相邻列来选择一列的内容
- spring-boot - Spring Boot Azure Functions 项目不适用于 Kotlin