best practices for protecting stdout with rApache

Skip to first unread message

Murat Tasan

Dec 5, 2014, 2:51:35 PM12/5/14
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?



Gergely Daróczi

Dec 5, 2014, 3:35:05 PM12/5/14
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] "chisq.test(mtcars$am, mtcars$gear)"


Pearson's Chi-squared test

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

[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] ""                                                

[1] "htest"


[1] "Chi-squared approximation may be incorrect"



[1] "evals"


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
To post to this group, send email to
Visit this group at
For more options, visit

Reply all
Reply to author
0 new messages