In R, is it possible to redirect console output to a variable?

In R, I'm wondering if it's possible to temporarily redirect the output of the console to a variable?

p.s. There are a few examples on the web on how to use sink() to redirect the output into a filename, but none that I could find showing how to redirect into a variable.

p.p.s. The reason this is useful, in practice, is that I need to print out a portion of the default console output from some of the built in functions in R.

Answers


I believe results <- capture.output(...) is what you need (i.e. using the default file=NULL argument). results <- sink(); ...; sink() should work as well, but as ?capture.output says, capture.output() is:

Related to ‘sink’ in the same way that ‘with’ is related to ‘attach’.

... which suggests that capture.output() will generally be better since it is more contained (i.e. you don't have to remember to terminate the sink()).


Need Your Help

Calling A Custom API From Table's Insert Function Windows Azure

api azure azure-mobile-services

I'm using windows azure in order to manage my application's data.

C# Active Directory User Set Dial-In enabled

c# .net active-directory user

So I'm creating a AD User with the UserPrincipal Class but I can't set the Allow Dial-In Setting through that.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.