Rscript在true if语句中产生空图

时间:2014-08-05 18:09:13

标签: r

如果我在下面运行下面的Rscript,我会得到一个没有任何内容的损坏的空白PDF。但是,如果我注释掉if语句,我会得到一个PDF。即使if语句没有被注释掉,我仍然在stdout中看到null device 1

ifiddes@ifiddes-desktop ~ $ Rscript test.R
[1] 3
null device 
         1 

以下是if语句处于活动状态的示例脚本

#!/usr/bin/env Rscript

library(lattice)

d <- matrix(c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9), nrow=3, ncol=3)

myPanel <- function(x, y, z, ...) {
    panel.levelplot(x, y, z, ...)
    panel.text(x, y, 100 * round(exp(-z),4))
}

if ( dim(d)[1] > 0 ) {

pdf()
print(dim(d)[1], stdout())
levelplot(as.matrix(-log(d)), main="", xlab="Read bases", ylab="Reference bases", panel = myPanel, col.regions=colorRampPalette(c("white","red"))(256))

dev.off()

}

1 个答案:

答案 0 :(得分:1)

使用lattice时,您必须明确print您的设备图。

if ( dim(d)[1] > 0 ) {
  pdf()
  print(dim(d)[1], stdout())
  myplot <- levelplot(as.matrix(-log(d)), main="", xlab="Read bases", ylab="Reference bases", panel = myPanel, col.regions=colorRampPalette(c("white","red"))(256))
  print(myplot) # Use print() to save your plot.
  dev.off()
}