Your code doesn't look so bad, but you can simplify it a bit by using textConnection :
sink(tt <- textConnection("results","w"),split=TRUE) print(11:15) ## [1] 11 12 13 14 15 sink() results ## [1] "[1] 11 12 13 14 15" close(tt) ## clean up
The only thing you need to pay attention to is that if you do not close the connection, the results will have locked bindings (see ?textConnection ), which will mean that you cannot, for example. Assign it a new value.
The output character of the symbol has locked bindings (see "lockBinding") until "close" is called in the connection.
Alternatively, you do not need to include several statements in the function to get them in capture.output() - you can use curly braces {} to make several statements in the same output ...
results <- capture.output(split=TRUE,{ print("hello") print("goodbye") })
source share