代码之家  ›  专栏  ›  技术社区  ›  stackinator Brenton Wiernik

tidyeval函数和`View()的问题`

  •  1
  • stackinator Brenton Wiernik  · 技术社区  · 6 年前

    print() 调用和编码块#2使用 View() "Error in FUN(X[[i]], ...) : object 'cal.date' not found" . 为什么?

    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)
    
    1 回复  |  直到 6 年前
        1
  •  5
  •   Jrakru56    6 年前

    View() 归还任何东西

    使用 %T>% magrittr %>% 你的第二个案子。

    把管子端起来,这样你就可以 T pipe 相反我想你可以这样看得更清楚

     df %>% child_function(!!variable, hor.line) %>% View() -> df
    

    与。

     df %>% child_function(!!variable, hor.line) %T>% View() -> df