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.
By default, the RStudio IDE opens a preview window to display the output of your . Rmd file. However, you can choose to display the output in a dedicated viewer pane. To do this, select “View in Pane” for m the drop down menu that appears when you click on the “Run Document” button (or “Knit HTML” button).
To run the entire document press the Ctrl+Shift+Enter key (or use the Source toolbar button).
You can also save the entire R console screen within the GUI by clicking on "Save to File..." under the menu "File." This saves the commands and the output to a text file, exactly as you see them on the screen. This includes the prompt (">"), so this is not good for redoing commands.
Usually the Script Window is shown at the top left in RStudio. If this window is not shown, it will be visible is you open a previously saved R script, or if you create a new R Script. You create new R Script by clicking on File > New File > R Script in the RStudio menu bar.
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.
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.
Use options(verbose=TRUE) to print all output verbosely throughout the script or session.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With