简体   繁体   English

代码从命令行启动:图形设备空白

[英]Code launched from command-line: graphic device blank

I have a code that has to work from command-line. 我有一个代码必须从命令行工作。 If I run it piece by piece from RStudio it works properly. 如果我从RStudio一块一块地运行它,它可以正常工作。 When I run it from command-line, it does open the graphic device, but it remain blank. 当我从命令行运行它时,它会打开图形设备,但它仍然是空白的。

require(ggplot2)
#ds <- head(SOM_dist_tot)
num <- 6
ds <- c(1.00566799, 0.81354614, 0.36507594, 0.15541231, 0.13957369, 0.06986632)

vett <- as.data.frame(ds)
pdf("ggplot_test.pdf")
  gioele <-ggplot(vett, aes(x= as.numeric(rownames(vett)), y =vett$ds))+
           geom_point(size=2) + xlab("X") + ylab("Y")+
           ggtitle("...")+
           theme(axis.text.x = element_text(angle=-45, hjust=0, vjust=1), 
                 plot.margin = unit(c(1, 1, 1, 1), "cm"), 
                 plot.title = element_text(size = 20, face = "bold", colour = "black", vjust = -1))
  #print(gioele)
  plot(gioele)
dev.off()

print(gioele)

inputFromUser <- as.numeric(readLines(file("stdin"),1))

dev.off()

I want to show the graph "gioele", such to give informations to the user. 我想显示图形“gioele”,例如向用户提供信息。 Then the user choose a command to insert ( inputFromUser ), and then the window has to close. 然后用户选择要插入的命令( inputFromUser ),然后窗口必须关闭。 Do you have any ideas? 你有什么想法?

After a lot of pain I have understood the problem: it is a conflict between readLines() and the graphic device. 经过很多痛苦我已经理解了这个问题:它是readLines()和图形设备之间的冲突。 Inother words, it correctly open the device plotting the graph, but then the readLines(...) make the graphic device blank (I don't know how it is technically possible). 换句话说,它正确地打开了绘制图形的设备,但随后readLines(...)使图形设备变为空白(我不知道它在技术上是如何可能的)。

I have found the solution removing the readLines() and using a more correct readline() . 我找到了删除readLines()并使用更正确的readline()的解决方案。

require(ggplot2)
#ds <- head(SOM_dist_tot)
num <- 6
ds <- c(1.00566799, 0.81354614, 0.36507594, 0.15541231, 0.13957369, 0.06986632)


pdf("plot_test.pdf")
  plot(ds[1:num], type="p", xlab="X", ylab="Y", col="red")
  title(main="...", sub = "...", line=-1.2, cex.sub = 0.75, font.sub = 3, col.sub = "red")
dev.off()
#cat("Faccio il plot")
# plot(ds[1:num], type="p", xlab="X", ylab="Y", col="red")
# title(main="...", sub = "...", line=-1.2, cex.sub = 0.75, font.sub = 3, col.sub = "red")


vett <- as.data.frame(ds)
pdf("ggplot_test.pdf")
  gg2 <-ggplot(vett, aes(x= as.numeric(rownames(vett)), y =vett$ds))+
    geom_point(size=2) + xlab("X") + ylab("Y")+
    ggtitle("...")+
    theme(axis.text.x = element_text(angle=-45, hjust=0, vjust=1), 
          plot.margin = unit(c(1, 1, 1, 1), "cm"), 
          plot.title = element_text(size = 20, face = "bold", colour = "black", vjust = -1))
  #print(gg2)
  plot(gg2)
dev.off()

print(gg2)

#inputFromUser <- as.numeric(readLines(file("stdin"),1))
inputFromUser <- as.numeric(readline())

dev.off()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM