Tim Tim - 1 year ago 70
R Question

dplyr + magrittr + qplot = no plot?

I want to use

(ggplot2) and then forward the data with

This works:

mtcars %>% qplot(mpg, cyl, data=.)

This produces an error:

mtcars %>% qplot(mpg, cyl, data=.) %>% summarise(mean(mpg))

And those produce only summary statistics:

mtcars %T>% qplot(mpg, cyl, data=.) %>% summarise(mean(mpg))
mtcars %>% {qplot(mpg, cyl, data=.); .} %>% summarise(mean(mpg))
mtcars %T>% {qplot(mpg, cyl, data=.)} %>% summarise(mean(mpg))

What is the problem? I already found this solution, but it does not help, as you see from the code attached.

Answer Source

All ggplot2 functions return an object that represents a plot - to see it you need to print it. That normally happens automatically when you're working in the console, but needs to explicit inside a function or a chain.

The most elegant solution I could come up with is this:


echo <- function(x) {
mtcars %>% 
  {echo(qplot(mpg, cyl, data = .))} %>% 

It seems like there should be a better way.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download