dplyr + magrittr + qplot = no plot? [英] dplyr + magrittr + qplot = no plot?

查看:114
本文介绍了dplyr + magrittr + qplot = no plot?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 qplot (ggplot2),然后使用 magrittr 转发数据:

I want to use qplot (ggplot2) and then forward the data with magrittr:

这样做:

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

这会产生错误:

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.

推荐答案

所有ggplot2函数返回一个表示情节的对象 - 查看需要打印的对象。这通常会在您在控制台中工作时自动发生,但需要在功能或链接中显式。

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:

library("ggplot2")
library("magrittr")
library("dplyr")

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

似乎应该有一个更好的方法。

It seems like there should be a better way.

这篇关于dplyr + magrittr + qplot = no plot?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆