首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >tidyeval函数和“`View()”的问题

tidyeval函数和“`View()”的问题
EN

Stack Overflow用户
提问于 2018-11-12 20:12:56
回答 1查看 35关注 0票数 1

代码块#1和#2是相同的,除了第14行。代码块#1使用print()调用,代码块#2使用View()调用。代码块#1工作正常。代码块#2给出了错误"Error in FUN(X[[i]], ...) : object 'cal.date' not found"。为什么?

1

代码语言:javascript
复制
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

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-12 20:20:14

View()是一个副作用函数,不返回任何内容。

在第二种情况下,使用来自magrittr包的magrittr而不是%>%

View()结束管道,这样您就会想要一个T pipe。我想你可以这样看得更清楚

代码语言:javascript
复制
 df %>% child_function(!!variable, hor.line) %>% View() -> df

代码语言:javascript
复制
 df %>% child_function(!!variable, hor.line) %T>% View() -> df
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53269415

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档