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

你能用管道把一个print()传送到ggplot而不把它包装在`()`

  •  1
  • stackinator Brenton Wiernik  · 技术社区  · 7 年前
    library(tidyverse)
    (
      e <- ggplot(mpg, aes(cty, hwy)) + 
        geom_point()
    ) %>% 
      print()
    

    有吗 怎么做?”这意味着打印存储的ggplot对象。我经常需要将绘图作为对象存储,但也希望看到它们。这个 () e 最后,但我也不喜欢那样。像这样的东西太酷了。看看区别就知道了。

    library(tidyverse)
    f <- mtcars %>% 
      select(cyl) %>% 
      as_tibble() %>% 
      print()  # redundant, just proving a point
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Nicolas2    7 年前

    如果只是为了与管道的使用保持一致,您可以尝试使用ggplot2的包ggformula来访问ggplot2的功能,而不使用ggplot2的语法:

    library(ggformula)
    g <- gf_point(cty ~ hwy, data=mpg) %>% print()
    
        2
  •  1
  •   moodymudskipper    7 年前

    你可以用这个来做 package ggfun :

    # devtools::install_github("moodymudskipper/ggfun")
    library(tidyverse)
    library(ggfun)
    
    ggplot(mpg, aes(cty, hwy)) + 
      geom_point() +
      print