2016-05-12 7 views
1

私はコマンドラインから動作するコードがあります。 RStudioからピース単位で実行すると、正常に動作します。コマンドラインから実行するとグラフィックデバイスが開きますが、空白のままです。コマンドラインからコードを起動しました:グラフィックデバイスの空白

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() 

グラフ「gioele」を表示して、ユーザーに情報を提供したいとします。その後、ユーザは挿入するコマンドを選択し(inputFromUser)、ウィンドウを閉じる必要があります。 アイデアはありますか?

+0

多分[ggsave](http://docs.ggplot2.org/0.9.2.1/ggsave.html)を見てください。なぜあなたはプロット(gg2)を使用していますか、印刷されていないのでしょうか(gg2)? – zx8754

+1

私は可能な組み合わせをたくさん試しました。私はplot(gg2)、print(gg2)、gg2のみを試しました(この場合は無視され、ウィンドウはポップアップしません)。私はggsaveを見て、私はあなたに知らせるでしょう。ありがとう! –

答えて

0

私は問題を理解しました。これは、readLines()とグラフィックデバイスの間の競合です。言い換えると、グラフをプロットしているデバイスを正しく開きますが、readLines(...)はグラフィックデバイスを空白にします(私はそれが技術的にどのように可能かわかりません)。

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() 
関連する問題