best practices for protecting stdout with rApache

30 views
Skip to first unread message

Murat Tasan

unread,
Dec 5, 2014, 2:51:35 PM12/5/14
to rap...@googlegroups.com
Hi all --- I'm new to using rApache, and have previously rolled my own CGI/R scripts when needed.
Unfortunately, some R packages contain functions that choose to output to stdout diagnostic information while running.
Since rApache uses stdout as part of the response, I'm curious about 'best practices' used by others to protect the stdout connection from such (undocumented) cases.

My default method for this is to sink() all stdout to stderr while building up a single response object, then briefly un-sink stdout, write the output, and re-sink stdout for any final cleanup.
The only negative part of that approach that I can see is the need to build that response object in-memory, rather than outputing the appropriate data online during computation.
Constantly sinking/unsinking for incremental pieces of the output lead to rather unpleasant code, however.

One could wrap calls in capture.output(), but that also tends to obfuscate the code.

What other approaches, if any, have y'all come up with?

Cheers!

-m

Gergely Daróczi

unread,
Dec 5, 2014, 3:35:05 PM12/5/14
to rap...@googlegroups.com
In one of my rApache-based web APIs I use pander::evals for evaluation, which captures the messages and stdout besides the results of an R expression, so it's up to you what to use from the returning list. Quick example:

> pander::evals('chisq.test(mtcars$am, mtcars$gear)')
[[1]]
$src
[1] "chisq.test(mtcars$am, mtcars$gear)"

$result

Pearson's Chi-squared test

data:  mtcars$am and mtcars$gear
X-squared = 20.9447, df = 2, p-value = 2.831e-05


$output
[1] ""                                                
[2] "\tPearson's Chi-squared test"                    
[3] ""                                                
[4] "data:  mtcars$am and mtcars$gear"                
[5] "X-squared = 20.9447, df = 2, p-value = 2.831e-05"
[6] ""                                                

$type
[1] "htest"

$msg
$msg$messages
NULL

$msg$warnings
[1] "Chi-squared approximation may be incorrect"

$msg$errors
NULL


$stdout
NULL

attr(,"class")
[1] "evals"


Best,
Gergely

--
You received this message because you are subscribed to the Google Groups "rapache" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rapache+u...@googlegroups.com.
To post to this group, send email to rap...@googlegroups.com.
Visit this group at http://groups.google.com/group/rapache.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages