Here's a solution... But be warned, it works for me on Linux, might work on a Mac, and I doubt it works on Windows...
Create two Rcpp functions:
library(Rcpp)
cppFunction('void redir(){FILE* F=freopen("/tmp/capture.txt","w+",stdout);}')
cppFunction('void resetredir(){FILE* F=freopen("/dev/tty","w+",stdout);}')
The first will send everything to that file. The second will reset it. The problem is that interactively after the first you'll not be able to see anything. So beware...
So initially system2 sends to the console:
> system2("echo", "hello")
hello
But wrapping a call in redir/resetredir sends it to the file:
> redir(); system2("echo","hello world this time") ; resetredir()
> # prompt returns!
Now we have:
$ cat /tmp/capture.txt
hello world this time
and if this is the output from some other package you'll have to read it in with R's file I/O routines.
The dodgy bit is the use of /dev/tty in the reset code - I'm not sure it works on a Mac or Windows. If you don't care about resetting the stdout then skip it, and just make sure you know how to quite R without seeing what you type. I'm also unsure if this will work in RStudio which probably has a different concept of the console....
?system2. you would have seen that using the argumentstdout = Tcaptures the output as a string.my_lswill always be just "Hello". There's no point in trying capture.output., Either usereturn() around thesystem2` call or reverse the order of those commands inside the function.system2call is within a function in a package and I am not able to edit the function to setstdout = T, so I'm looking for a workaround.MyFoothen typeMyFoo, note no brackets, and you get the R code of the function, copy it and make your changes and save it as a new functionMyFoo2for examplecapture.outputgets "the console" output andsystem2outputs to "the C-level stdout' I reckon.