I am running hundreds of code lines from a script.R
using Rstudio
, but what annoys me a bit compared to Matlab
is that the Rstudio
console keeps mechanically showing the each command line
from my script file. Is there a way to prevent this from occuring?
The frustrating part comes, when one uses sprintf()
for instance, to the extent that command lines from the script file get mixed up with sprintf()
in the console.
Best,
If you're using R from the command line instead of from within RStudio, you need to use Ctrl + C instead of Esc to cancel the command.
However, if you are using RStudio on a Windows computer, you can usually use Esc to stop a currently executing R script. Then, we can press Esc to interrupt the loop.
Simply hit Ctrl+L on the keyboard and you will see that everything written in the console will be erased and the console will be cleared.
If you just want to run script.R
try using the source
function instead.
source("script.R") # assuming script.R is in your working directory.
This function will run the script without showing everything in the command line.
Run your code with ctrl+shift+S
. Or go to source on top right of your script.R
and then run just Source
and not Source with echo
. And, as for your requirement for printing @Konrad Rudolph suggestion of using message message("%f", pi)
is the best solution .
If printing the message is intended to track the status of your code here are some helpful solutions showing a status message in R
Like Matlab's sprintf
, R's sprintf
only creates a string, it doesn't print it to the output.* You have to explicitly call the print
function to see anything:
print(sprintf("%f", pi)) # prints
A plain sprintf
officially creates a string and then discards it because it wasn't saved to a variable:
sprintf("%f", pi) # does nothing
However, this sort of worked for you because the REPL (the command line that runs R code one line at a time) disobeys the directive to discard values that aren't saved to variables, but instead prints them. This is a convenience thing to make it easier to work at the command line. You can type 1+1
and get it to print 2
even though an R script would normally discard the value silently.
Other functions that print text to the console are cat
and message
, which are each slightly different. See their help files for usage.
* Technically, Matlab will print the value of any statement that isn't terminated by a ;
, including strings. So without a semicolon, the string that results from sprintf
can get printed though it's not being done by sprintf
directly, but by the generic print-all-the-things behavior of Matlab. In my opinion, this is a weird feature.
An update to this which may be helpful for future searches, if you are using the 'source' button in Rstudio (top right hand corner of the script editor window) it will default to 'source' or 'source with echo' depending on the last keyboard source input it received.
So, if you click 'ctrl+shift+enter', it will always 'source with echo' each subsequent time you use the 'source' button (i.e. print all script commands to the console).
If you click 'ctrl+shift+s' then it will simply source each time you subsequently use the 'source' button (i.e. not print the script commands to the console).
just enclose between () the code line you want supress from console:
1+1
output looks:
> 1+1
[1] 2
(1+1)
output looks:
> [1] 2
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