[R] Capturing R console output into a file (sink+savehistory ??)

Duncan Murdoch murdoch.duncan at gmail.com
Sat May 22 12:26:28 CEST 2010


Tal Galili wrote:
> After reading more, I understand I didn't formulate my last question
> correctly, so please allow me to rephrase:
>
> What I am looking for is a way to save the R console session output.
> That is, a command that would combine the results of using:
> ?sink   # And
> ?savehistory
>   

I think there's still some ambiguity in the question; I'll try to answer 
a few versions.

Do you want to run R normally, then afterwards save the console log?  
That depends on what front end you're using.  In the Windows GUI, you 
can do it with
Ctrl-A to select everything in the console, then menu items File | Save 
to file..., or just Ctrl-C to copy, and then paste it into Word.  
Similar operations would work on a Mac.

Do you want to run code in a way that writes it to a file without 
displaying it to the screen?  R CMD batch does that.

Do you want to run just a few commands like that? Then try

capture.output(source(stdin(), echo=TRUE))

This will accept commands from the console until it hits an EOF (Ctrl-Z 
on Windows, I don't know on other systems, but Ctrl-D is a good guess) 
and will return
the results in a character vector, which you could write to a file.

Do you want to run R normally with output on screen, but also logging 
everything to a file?  I don't know how to do that in the R GUI in 
Windows, but there are probably command line tools that could do it.

Duncan Murdoch

>   
> My motivation for this is that doing it will allow someone who is a blind
> user of R to be able to easily export his results to word so he could have
> word read him the text.
> I also imagine it might be useful for session login.
>
> Thanks,
> Tal
>
>
>
>
> ----------------Contact
> Details:-------------------------------------------------------
> Contact me: Tal.Galili at gmail.com |  972-52-7275845
> Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
> www.r-statistics.com (English)
> ----------------------------------------------------------------------------------------------
>
>
>
>
> On Sat, May 22, 2010 at 12:20 AM, Tal Galili <tal.galili at gmail.com> wrote:
>
>   
>> Hi David,
>>
>> I want to get both the 4 and the "1+3" that created it.
>>
>> I am trying to help someone else on the mailing list that is looking for a
>> way to "sink" the console into word, so he could have word read it to him
>> (he is blind).
>> I know how to do the second part, but the first part (using sink with the
>> commands, and not just the output), I am somehow missing...
>>
>> Best,
>> Tal
>>
>>
>>
>> ----------------Contact
>> Details:-------------------------------------------------------
>> Contact me: Tal.Galili at gmail.com |  972-52-7275845
>> Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
>> www.r-statistics.com (English)
>>
>> ----------------------------------------------------------------------------------------------
>>
>>
>>
>>
>> On Sat, May 22, 2010 at 12:17 AM, David Winsemius <dwinsemius at comcast.net>wrote:
>>
>>     
>>> On May 21, 2010, at 5:02 PM, Tal Galili wrote:
>>>
>>>  Hi all,
>>>       
>>>> I am trying to use type message with sink, like this:
>>>>
>>>> sink("all.Rout", type="message")
>>>> 1+3
>>>>
>>>> sink()
>>>>
>>>> readLines(con = "all.Rout")
>>>>
>>>> So to get the following output:
>>>>
>>>>  1+3
>>>>         
>>>> [1] 4
>>>>
>>>> Obviously this doesn't work.
>>>>
>>>>
>>>>         
>>> What are you trying to do? The sink help page has two rather dire warnings
>>> about not using type="message",  and using type="output would give you what
>>> you ask:
>>>
>>>       
>>>> sink("all.Rout", type="output")
>>>>         
>>>> 1+3
>>>>
>>>> sink()
>>>>
>>>> readLines(con = "all.Rout")
>>>>         
>>> [1] "[1] 4"
>>>
>>> The extra "[1]" and quotes are from the readLines function, not from
>>> all.Rout.
>>>
>>>
>>>  I tried some variations (based on the explanations in the help) but am
>>>       
>>>> missing something on how to make it work.
>>>>
>>>> Any suggestions?
>>>>
>>>> (p.s: I need this so to help Faiz Rasool in his latest post)
>>>>
>>>> Thanks,
>>>>
>>>> Tal
>>>>
>>>>         
>>> --
>>> David Winsemius, MD
>>> West Hartford, CT
>>>
>>>
>>>       
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list