stackinator
stackinator

Reputation: 5819

Can you pipe a `print()` to ggplot without wrapping it in `()`

library(tidyverse)
(
  e <- ggplot(mpg, aes(cty, hwy)) + 
    geom_point()
) %>% 
  print()

Is there any "prettier" way to do this? 'This' meaning print a stored ggplot object. I often have to store plots as an object, yet also want to see them. The () wrap really makes things ugly. Seems contrary to core tidyverse principles. I know I could simply call out e at the end, but I don't like that either. Something like this is so much cooler. Just look at the difference.

library(tidyverse)
f <- mtcars %>% 
  select(cyl) %>% 
  as_tibble() %>% 
  print()  # redundant, just proving a point

Upvotes: 1

Views: 348

Answers (2)

moodymudskipper
moodymudskipper

Reputation: 47340

You can do it with the package ggfun :

# devtools::install_github("moodymudskipper/ggfun")
library(tidyverse)
library(ggfun)

ggplot(mpg, aes(cty, hwy)) + 
  geom_point() +
  print

Upvotes: 1

Nicolas2
Nicolas2

Reputation: 2210

If it is only a matter of consistency with the use of the pipe, you could try the package ggformula that gives access to the features of ggplot2 without the syntax of ggplot2 :

library(ggformula)
g <- gf_point(cty ~ hwy, data=mpg) %>% print()

Upvotes: 1

Related Questions