How do I get all the output from script I am running in RStudio

35,383

Solution 1

I'm one of the RStudio developers. Thanks for the feedback--I'll log a bug.

In the meantime, one workaround is to do source(filename, echo=T) from the console.

Solution 2

You can simply select the code you want to run and press CTRL+ENTER to do what you want in RStudio. This works for multiple lines, exactly like in Tinn-R. If you want to run everything at once in a verbose way, you press CTRL-A CTRL-ENTER.

As another option for saving to a text file, you can check ?sink :

sink(file='path/to/somefile.ext')
... # the code generating output
sink()

sink() redirects all output of the console to a connection, in this case some file. Mind you, this is only the standard output, not the warnings or errors. This command also come in handy to create output files in analyses, in combination with print(), cat(), sprintf() etc.

If you use "run all" in RStudio, you have to explicitly use any of the mentioned functions to generate the output to the file. In principle, RStudio runs silently if you run the whole script.

Solution 3

Use options(verbose=TRUE) to print all output verbosely throughout the script or session.

Share:
35,383

Related videos on Youtube

Farrel
Author by

Farrel

Not a programmer but not afraid to use simple programming / macroing such as manipulate and analyze data in R or write 3-10 line autohotkey commands.

Updated on March 01, 2020

Comments

  • Farrel
    Farrel about 4 years

    I want to see the output of a script that has 149 lines. All the way through the script there are tables that I want to see. I am using RStudio IDE. In the past I have used Tinn-R. I would run the whole script and the lines of code and the printed objects would be visible in the console.

    For instance, here is an excerpt

    attach(uniquehuman.race.eth)
    partA.eth <-table(Ethnicity, Sex,useNA="ifany")
    partA.eth
    margin.table(partA.eth,1)#row totals
    margin.table(partA.eth,2)#column totals
    nrow(uniquehuman.race.eth)#total logged in
    

    The above code would give a text output of the tables and the numbers I needed. I could then save the console or copy and paste the whole thing in a text file.

    How can I do that in RStudio? The closest I come to it is hitting CTRL-ENTER on each line, but I do not want to do that 149 times. If I hit CTRL-SHIFT-ENTER for "run all", then R processes all the data and puts the objects into memory, but I do not see the output.

    Please tell me how I can see all the output and/or send the output to a text file.

  • Farrel
    Farrel about 13 years
    I tried sink("numbersforeliz.txt") but all it does is give me the last lines of output. It is not giving me the entire output. It is the same output that I see in the Console if I use run all. But I see a lot lot more if I hit ctrl-enter (run lines) 149 times.
  • Joris Meys
    Joris Meys about 13 years
    @Farrel : it might be that you need to explicitly print the numbers you want. Hence my reference to cat(), sprintf() and so on. Everything between sink(file=...) and sink() that is printed to the console, is normally redirected to the specified file.
  • Joris Meys
    Joris Meys about 13 years
    @Farrel : answer edited with the normal way of doing that in RStudio.
  • Farrel
    Farrel about 13 years
    I want RStudio to run loudly or verbosely. I want each line of code reproduced in the console and I want any resultant output printed below it. So that if there is a line of code "nrow(uniquehuman.race.eth)#total logged in" then I will see that in the output and I will see a integer on the next line. I recognize the good practices you are referring to by telling me about print() but it would be too much to go back and edit every script I have.
  • Joris Meys
    Joris Meys about 13 years
    @Farrel : selecting the code you want to run (multiple lines possible!) and hitting CTRL+ENTER does run verbosely. An option to run-all verbosely is not present yet. Go to the RStudio guys and ask them to add the feature, plenty of people would love that. But for now I gave you all options that are currently available.
  • Farrel
    Farrel about 13 years
    Yip. That worked. That is what I wanted. I could then copy and paste the console or I could enter a sink("filenameIwantforoutput.txt") then run your command and then enter sink() again to stop the diversion. Thanks a lot
  • Alessandro Jacopson
    Alessandro Jacopson about 11 years
    What is the status of the bug? Is it solved in RStudio 0.97.173? Thank you.
  • daticon
    daticon over 10 years
    Note to RStudio developers, this bug still has not been fixed; I'm using version Version 0.98.493 and still need to use the 'echo=T' The comment below refers to the RStudio notebook. Here is the icon !enter image description here
  • Farrel
    Farrel about 10 years
    RStudio now has a feature in the top right of the script panel. It looks to be a squat notebook. "Compile and HTML notebook from the current R script" seems to do the job. Try that and tell me if it works.
  • daticon
    daticon about 10 years
    If you mean... click the small 'spreadsheet' icon... no, it doesn't work, only ever shows 100 variables. My workaround now has been to send to an excel spreadsheet.
  • Farrel
    Farrel about 10 years
    I want to upload an image to show you what I mean by the squat notebook. I cannot put an image here. I am going to put it in your answer. On the other hand, if you have a 100 variables to look at then you do not want output from your console and nor do you want it from an HTML notebook. You do indeed want to export the table as a csv and open it in LibreOffice calc or Google Spreadsheets or Excel.
  • Paul
    Paul over 7 years
    this works perfectly well if you don't care about seeing tables and other objects, but are just interested in warnings, errors, and stdout. But it doesn't answer the OP's question