owner
owner

Reputation: 743

Prevent Rstudio console from showing script commands

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,

Upvotes: 16

Views: 27843

Answers (5)

user16919184
user16919184

Reputation: 31

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

Upvotes: 3

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

Upvotes: 1

drhagen
drhagen

Reputation: 9532

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.

Upvotes: 4

user5249203
user5249203

Reputation: 4648

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

Upvotes: 17

mfidino
mfidino

Reputation: 3055

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.

Upvotes: 4

Related Questions