2012-10-25 10 views
17

Rスクリプトを実行し、png()を使用してプロットを保存します。 X11は、Unixサーバではサポートされていないと私は、次のエラーメッセージが出ます:X11なしのサーバーでRスクリプトを実行する方法

Error in X11(paste("png::", filename, sep = ""), g$width, g$height, pointsize, : 
    unable to start device PNG 
Calls: png 
In addition: Warning message: 
In png("test.png", width = 900, height = 400) : 
    unable to open connection to X11 display '' 
Execution halted 

環境変数の設定解決していないRスクリプトで

Sys.setenv("DISPLAY"=":0.0") 

または

Sys.setenv("DISPLAY"=":0") 

を問題。

このコード例は、上記のエラー生成:

library(ggplot2) 
library(grid) 

# Some data 
df = data.frame(x = c(1.2,5,3,5.3,2,9,4,6,8,0), y = c(1.5,6,2.5,5.1,2.3,8.7,4.1,6,8,0)) 


# Base plot 
p <- ggplot(df, aes(x,y)) + geom_point() + 
    scale_x_continuous(limits = c(0, 8), expand = c(0,0)) + 
    scale_y_continuous(limits = c(0, 8), expand = c(0,0)) + 
    geom_smooth(method="lm", se=FALSE, formula=y~x, aes(colour="2"), show_guide=TRUE, fill=NA, size=1.2) + 
    geom_vline(xintercept = 3) + geom_vline(xintercept = 7) + 
    opts(plot.margin = unit(c(1,1,4,1), "lines")) 

# Create the text Grobs 
Text1 = textGrob("Part 1") 
Text2 = textGrob("Part 2") 
Text3 = textGrob("Part 3") 

# Add the annotations 
# Segment 1 
p1 = p + 
    annotation_custom(grob = linesGrob(), xmin = 0, xmax = 0, ymin = 
-1, ymax = -.75) + 
    annotation_custom(grob = linesGrob(), xmin = 0, xmax = 3, ymin = 
-1, ymax = -1) + 
    annotation_custom(grob = linesGrob(), xmin = 3, xmax = 3, ymin = 
-1, ymax = -.75) + 
    annotation_custom(grob = Text1, xmin = 0, xmax = 3, ymin = -1.25, 
ymax = -1.25) 

# Segment 2 
p1 = p1 + 
    annotation_custom(grob = linesGrob(), xmin = 3, xmax = 7, ymin = 
-1, ymax = -1) + 
    annotation_custom(grob = linesGrob(), xmin = 7, xmax = 7, ymin = 
-1, ymax = -.75) + 
    annotation_custom(grob = Text2, xmin = 3, xmax = 7, ymin = -1.25, 
ymax = -1.25) 

# Segment 3 
p1 = p1 + 
    annotation_custom(grob = linesGrob(), xmin = 7, xmax = 8, ymin = 
-1, ymax = -1) + 
    annotation_custom(grob = linesGrob(), xmin = 8, xmax = 8, ymin = 
-1, ymax = -.75) + 
    annotation_custom(grob = Text3, xmin = 7, xmax = 8, ymin = -1.25, 
ymax = -1.25) 

png("test.png", width=900, height=400) 

# Code to override clipping 
gt <- ggplot_gtable(ggplot_build(p1)) 
gt$layout$clip[gt$layout$name=="panel"] <- "off" 
grid.draw(gt) 

dev.off() 

capabilities()の結果は次のとおりです。

jpeg  png  tiff tcltk  X11  aqua http/ftp sockets 
    FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE  TRUE 

    libxml  fifo cledit iconv  NLS profmem cairo 
    TRUE  TRUE FALSE  TRUE  TRUE FALSE FALSE 

私は太陽のグリッド・エンジンを経由してスクリプトを実行しています。

+0

もX11をサポートするようにコンパイルRました私のために働いたoptions(bitmapType='cairo') を試してみてください?サーバ上で 'capabilities()'をチェックしてください。 IIRCではXの代わりにXの仮想フレームバッファを使用できますが、これは動作することができますが、X仮想フレームバッファパッケージ/ソフトウェアがサーバにインストールされていることを前提としています(LinuxではFedoraにxorg-x11-server-Xvfbをインストールできますこの目的)。それが失敗した場合は、サーバーでサポートされているグラフィックスデバイスを見つけ、それをプロットしてファイルを取得し、ローカルシステムのPNGに変換してください。 –

+1

サーバーにどのように接続していますか?私は 'ssh'にX11トンネリングをオンまたはオフにするオプションがあると思います。 –

+0

JPEG、PNG、TIFF tcltk X11アクアHTTP/FTPソケット FALSE FALSE FALSE TRUE FALSE FALSE TRUE TRUE のlibxml用FIFO cledit RのiconvのNLS profmemカイロ TRUE TRUE FALSE TRUE TRUE FALSE FALSE –

答えて

3

これまでに私はこれまでに答えましたが、X11がそれを忘れてしまうことを期待するプログラムを作ることはできませんが、仮想フレームバッファを使ってX11が存在するとふりまとうことができます。

詳細については、this older SO questionを参照してください。

20

あなたはR 3.0を使用している場合、それは

+1

どこに配置しましたか? png()呼び出しの前または後、または他の場所? –

+0

png()コールの前に配置し、すべてのpng()コールで十分です。遅れて申し訳ありません – svural

+1

こんにちは、これは私のために働いた唯一のソリューションです。 – lourencoj

関連する問題