如果使用 source() 运行,R 包点阵将不会绘制 [英] R package lattice won't plot if run using source()

查看:25
本文介绍了如果使用 source() 运行,R 包点阵将不会绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用 lattice 图形包,但我偶然发现了一个问题.我希望有人可以帮助我.我想使用相应的函数绘制直方图.

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.

这是文件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 一切正常.

When I run this code using R --vanilla < foo.r it works all fine.

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

However, if I use a second file bar.r with

source("bar")

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

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?

我使用的是带有lattice_0.19-30 的R 版本2.13.1 (2011-07-08)

I'm using R version 2.13.1 (2011-07-08) with lattice_0.19-30

推荐答案

R 的常见问题 -- 您需要 print() 围绕您调用的点阵函数:

It is in the FAQ for R -- you need print() around the lattice function you call:

7.22 为什么点阵/格子图形不起作用?

7.22 Why do lattice/trellis graphics not work?

最可能的原因是你忘记告诉 R 显示图形.xyplot() 等晶格函数创建图形对象,但不显示它(ggplot2 图形也是如此,TrellisS-Plus 中的图形).图形对象的 print() 方法产生实际显示.当您以交互方式使用这些功能时命令行,结果自动打印,但在 source() 或在您自己的函数中,您将需要一个显式的 print() 语句.

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屋!

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