格子图在函数内部不起作用

SAN*_*SAN 3 printing r lattice

我有这个R代码,它可以很好地工作,如果我在R控制台中运行它或者作为一个RScript,我会获得带有该图的位图

library(DBI);
library(RMySQL);
library(brew);
library(lattice);
con <- dbConnect(MySQL(),server credentials)
x <- dbGetQuery(con,"SELECT name, distance FROM distances")
bitmap("/tmp/dist_6078.bmp")
dotplot(x$distance~x$name, col='red', xlab='name', ylab='distance', main='Distance plot')
dev.off()
Run Code Online (Sandbox Code Playgroud)

问题是,如果我将所有内容都包含在<%和%>之间并使用brew库,我会得到一张空白图片.如果我使用基本的R图,一切正常,只有当我使用格子时问题.

SAN*_*SAN 7

来自R FAQ 7.22

诸如xyplot()之类的格子函数创建了一个图形对象,但不显示它(ggplot2图形和S-Plus中的Trellis图形也是如此).图形对象的print()方法产生实际显示

工作代码

library(DBI);
library(RMySQL);
library(brew);
library(lattice);
con <- dbConnect(MySQL(),server credentials)
x <- dbGetQuery(con,"SELECT name, distance FROM distances")
bitmap("/tmp/dist_6078.bmp")
plot_obj <- dotplot(x$distance~x$name, col='red', xlab='name', ylab='distance', main='Distance plot')
print(plot_obj)
dev.off()
Run Code Online (Sandbox Code Playgroud)