
Contents
実行環境
••
•
•
psql
準備その 1 : Cygwin シェルから Windows 版 R の起動
Cygwin次に、シェルからパイプで
$ echo '1 2' | d:/works/r/current/bin/x64/rscript -e 'read.table("stdin")' During startup - Warning message: Setting LC_CTYPE=ja_JP.UTF-8 failed V1 V2 1 1 2 # suppress the warning about locale export LC_CTYPE="C"

関連で、パイプから
Octave
$ echo '1 2 3 4' | d:/works/r/current/bin/x64/rscript -e 'barplot(unlist(read.table("stdin")[1,]))' # open PDF $ cygstart rplots.pdf

準備その 2 : Cygwin シェルから R のプロットウィンドウにグラフ描画
コマンドラインから、PDF$ d:/works/r/current/bin/x64/rscript -e 'x11(); plot(iris); scan("stdin")'

ただし難あり。R

この方法だと、前項のようにパイプでデータを渡しても上手くいかず ↓ 一瞬で
$ echo '1 2' | d:/works/r/current/bin/x64/rscript -e 'x11(); plot(read.table("stdin")); scan("stdin")' Read 0 items numeric(0)
scan("stdin")
devAskNew() dev.hold() getGraphicsEvent() Sys.sleep(Inf)
確認 … データを渡しつつ対話型シェルに入るのは、R では厳しい
gnuplot、Octaveで簡単に出来た「とりあえずデータを渡してざっくりグラフ化、そのまま対話型シェルに入って微調整」という、最も使いそうなパターンが以下、参考までに出来ない場面の例をいくつか。単純にパイプを、Rscript
$ echo '1 2' | d:/works/R/current/bin/x64/rFatal error: you must specify '--save', '--no-save' or '--vanilla'
で、例えば
$ echo '1 2' | d:/works/R/current/bin/x64/r --vanilla R version 3.3.0 (2016-05-03) -- "Supposedly Educational" Copyright (C) 2016 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 (64-bit) ... Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > 1 2Error: unexpected numeric constant in "1 2" Execution halted

パイプでなく
$ d:/works/R/current/bin/x64/r -e "d=read.table('tmp4')" R version 3.3.0 (2016-05-03) -- "Supposedly Educational" Copyright (C) 2016 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 (64-bit) ... Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > d=read.table('tmp4') > $

R
psql から R にデータを渡し、グラフ出力
psql-- set command to psql variable # \setenv r 'd:/works/r/current/bin/x64/rscript -e' -- set output format # \f ' ' \\\t on \\\pset format unaligned -- query and pipe to R # select x, y, sin(r) / r from generate_series(-8, 8, 0.4) as x, generate_series(-8, 8, 0.4) as y, cast(sqrt(x^2 + y^2) + 1e-9 as float) as r \g | $r 'head(read.table("stdin"))'

上でクエリしたのは、前回の
-- set output format # \f ' ' \\\t on \\\pset format unaligned -- query again and pipe to R # \g | $r 'attach(read.table("stdin")); x=unique(V1); persp(x, unique(V2), matrix(V3, length(x)), phi=25, theta=35);' -- open PDF # \! cygstart rplots.pdf

R

注意点。PDF
# \g | $r '...' Error in (function (file = if (onefile) "Rplots.pdf" else "Rplot%03d.pdf", : cannot open file 'Rplots.pdf' Calls: persp -> persp.default -> plot.new ->Execution halted

psql から R のプロットウィンドウにグラフを出す(限定的)
前項のようにファイル出力では面倒な時、使える範囲は限られて問題もありますが一応、psql-- set command to psql variable # \setenv r 'd:/works/r/current/bin/x64/rscript -e' -- set output format # \f ' ' \\\t on \\\pset format unaligned # \g tmp4 \! $r "d=read.table('tmp4'); x11(); plot(d); scan('stdin')"

ただしCygwin
また、上の
-- set output format # \f ' ' \\\t on \\\pset format unaligned # \g tmp4 \! $r "d=read.table('tmp4'); x11(); x=unique(d[,1]); persp(x, unique(d[,2]), matrix(d[,3], length(x)), phi=25, theta=35); scan('stdin')"

↓ なぜか

↓ 同様のコードをインタラクティブシェルで実行すると、普通。R
# \! d:/works/R/current/bin/x64/r --no-save -q > d=read.table('tmp4') > x11() > x=unique(d[,1]) > persp(x, unique(d[,2]), matrix(d[,3], length(x)), phi=25, theta=35)

そもそも
それと対照的に