如果使用source()运行R包格, [英] R package lattice won't plot if run using source()
问题描述
我开始使用格
图形包,但我偶然发现一个问题。我希望有人能帮助我。
我想用相应的函数绘制一个直方图。
以下是文件 foo.r
:
library(lattice)
data < - data.frame(c(1 :2),c(2:3))
colnames(data)<-c(RT,Type)
pdf(/ tmp / baz.pdf )
直方图(〜RT | factor(Type),data = data)
dev.off()
当我使用 R --vanilla <
然而,如果我使用第二个文件 bar.r $ foo.r
c $ c> with
来源(bar)
并运行 R - vanilla < bar.r
这段代码会产生一个错误的pdf文件。
现在我发现 source(bar,echo = TRUE)
解决了这个问题。这里发生了什么?这是一个错误还是我错过了什么?
我使用R版本2.13.1(2011-07-08)with lattice_0.19-30
它位于 R 的常见问题 - 您需要 print()
您打电话的格子功能:
lockquote
7.22为什么格子/格子图形不起作用?
$ b p>最可能的原因是你忘记告诉R显示
图。像xyplot()这样的格子函数会创建一个图形对象,但
不显示它(对于ggplot2图形和S-Plus中的格子
图形也是如此)。图形对象的print()方法产生
的实际显示。当您在
命令行交互使用这些函数时,结果会自动打印,但是在自己的函数中的source()或
中,您将需要一个显式的print()语句。
I started using the lattice
graphic package but I stumbled into a problem. I hope somebody can help me out.
I want to plot a histogram using the corresponding function.
Here is the file 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()
When I run this code using R --vanilla < foo.r
it works all fine.
However, if I use a second file bar.r
with
source("bar")
and run R --vanilla < bar.r
the code produces an erroneous pdf file.
Now I found out that source("bar", echo=TRUE)
solves the problem. What is going on here? Is this a bug or am I missing something?
I'm using R version 2.13.1 (2011-07-08) with lattice_0.19-30
It is in the FAQ for R -- you need print()
around the lattice function you call:
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包格,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!