如果使用source()运行,则R封装晶格将不会绘制

标签 r graphics plot lattice

我开始使用lattice图形包,但遇到一个问题。我希望有人能帮助我。
我想使用相应的函数绘制直方图。

这是文件foo.r:

library("lattice")

data <- data.frame(c(1:2),c(2:3))
colnames(data) <- c("RT", "Type")

pdf("/tmp/baz.pdf")
histogram( ~ RT | factor(Type), data = data)
dev.off()

当我使用R --vanilla < foo.r运行此代码时,一切正常。

但是,如果我使用第二个文件bar.r
source("bar")

并运行R --vanilla < bar.r,该代码会生成错误的pdf文件。
现在我发现source("bar", echo=TRUE)解决了这个问题。这里发生了什么?这是错误还是我错过了什么?

我正在将R版本2.13.1(2011-07-08)与lattice_0.19-30一起使用

最佳答案

它在FAQ for R中-您需要在调用的晶格函数周围添加print():

7.22 Why do lattice/trellis graphics not work?

The most likely reason is that you forgot to tell R to display the graph. Lattice functions such as xyplot() create a graph object, but do not display it (the same is true of ggplot2 graphics, and Trellis graphics in S-Plus). The print() method for the graph object produces the actual display. When you use these functions interactively at the command line, the result is automatically printed, but in source() or inside your own functions you will need an explicit print() statement.

关于如果使用source()运行,则R封装晶格将不会绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6783120/

相关文章:

r - 连接来自向量的相邻字符串

r - devtools::document 尝试将目录作为文件打开

delphi - 将 Jpeg 图像转换为 Bmp - 某些图像显示为蓝色

R Shiny 改变情节高度

r - 更改图例中的字体大小

math - 在 MATLAB 中将 3D 中的 2D 矢量演变绘制为色带

R : Percentile 90% with tapply

r - 如何使用 map,sapply 为 R 中的元素操作构建高效循环

math - 全局变换到局部变换?

java - 将值传递给小程序