Run R silently from command line, export results to JSON -
how might call r script shell (e.g. node.js exec) , export results json (e.g. node.js)?
the r code below works. reads data, fits model, converts parameter estimates json, , prints them stdout:
#!/usr/bin/rscript --quiet --slave install.packages("cut", repos="http://cran.rstudio.com/"); install.packages("hmisc", repos="http://cran.rstudio.com/"); install.packages("rjson", repos="http://cran.rstudio.com/"); library(rjson) library(reshape2); data = read.csv("/data/records.csv", header = true, sep=","); mylogit <- glm( y ~ x1 + x2 + x3, data=data, family="binomial"); params <- melt(mylogit$coefficients); json <- tojson(params); json
here's how i'd call node...
var exec = require('child_process').exec; exec('./model.r', function(err, stdout, stderr) { var params = json.parse(stdout); // fail! junk in stdout });
except r process won't stop printing stdout. i've tried --quiet --slave --silent
little not enough. here's what's sent stdout:
the downloaded binary packages in /var/folders/tq/frvmq0kx4m1gydw26pcxgm7w0000gn/t//rtmpyk7gmn/downloaded_packages downloaded binary packages in /var/folders/tq/frvmq0kx4m1gydw26pcxgm7w0000gn/t//rtmpyk7gmn/downloaded_packages [1] "{\"value\":[4.04458733165933,0.253895751245782,-0.1142272181932,0.153106007464742,-0.00289013062471735,-0.00282580664375527,0.0970325223603164,-0.0906967639834928,0.117150317941983,0.046131890754108,6.48538603593323e-06,6.70646151749708e-06,-0.221173770066275,-0.232262366060079,0.163331098409235]}"
what's best way use r scripts on command line?
running r --silent --slave cmd batch model.r
per post below still results in lot of extraneous text printed model.rout
:
those options stop r's own system messages printing, won't stop r function doing printing. otherwise you'll stop last line printing , won't json stdout!
those messages coming install.packages
, try:
install.packages(-whatever-, quiet=true)
which claims reduce amount of output. if reduces zero, job done.
if not, can redirect stdout sink
, or run things inside capture.output
.
Comments
Post a Comment