Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent Rstudio console from showing script commands

Tags:

r

rstudio

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,

like image 268
owner Avatar asked Feb 09 '16 13:02

owner


People also ask

How do you stop a command in R console?

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.

How do I stop an R script in RStudio?

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.

How do I clear the console output in R?

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.


5 Answers

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.

like image 97
mfidino Avatar answered Oct 28 '22 23:10

mfidino


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 image 20
user5249203 Avatar answered Oct 28 '22 22:10

user5249203


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.

like image 3
drhagen Avatar answered Oct 29 '22 00:10

drhagen


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).

like image 2
user16919184 Avatar answered Oct 28 '22 23:10

user16919184


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
like image 1
José Fernando Giraldo Jimenez Avatar answered Oct 28 '22 22:10

José Fernando Giraldo Jimenez