16

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,

5
  • 2
    Aren't there two options when sourcing the script: Source and Source with Echo? It sounds like you are clicking Source with Echo. I always click on plain Source and don't see the script lines.
    – drhagen
    Commented Feb 9, 2016 at 13:55
  • @drhagen: thx for highlighting on the differences with Echo, although I am not clicking of any of them. while writing the script, i was executing it using ctrl +a, then f5. Based on your feedback, when i click on plain Source, the issue is that sprintf() does not show up in the console.
    – owner
    Commented Feb 9, 2016 at 14:07
  • That is because sprintf does not print to the command line; it creates a string. If you run things line by line, the REPL will print bare values for you for convenience (just like a bare 1+1), but sourcing the script doesn't do this, it just discards them. Put a print() around those sprintfs.
    – drhagen
    Commented Feb 9, 2016 at 14:12
  • 3
    @drhagen No, print isn’t really the tool for this either — message is. Commented Feb 9, 2016 at 14:16
  • @drhagen: thx.that solves my issue. best.
    – owner
    Commented Feb 9, 2016 at 14:18

5 Answers 5

17

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

4

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.

5
  • thx. unfortunately source("path/script.R") prevents sprintf() from being disclosed in the console.
    – owner
    Commented Feb 9, 2016 at 14:10
  • 1
    @owner That’s intentional, sprintf isn’t supposed to print to the console. Use message instead. Commented Feb 9, 2016 at 14:12
  • @KonradRudolph: i usually use sprintf() to print strings to console at least in Matlab. fyi: message() works too, although a confusion could be made (in apprearance) with R default warnings message. Best,
    – owner
    Commented Feb 9, 2016 at 14:27
  • @owner For the record, sprintf doesn't print strings, fprintf does that, but leaving the semicolon off a line in Matlab will print anything on that line, but whatever works for you.
    – drhagen
    Commented Feb 9, 2016 at 14:32
  • @owner Well you’re doing it wrong. It happens to work under specific circumstances but as you’re seeing here, it doesn’t work reliably. Incidentally, the same is true for Matlab. sprintf, despite its name, is for string formatting, not for printing. Commented Feb 9, 2016 at 14:33
4

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.

2
  • test this in Matlab: sprintf('invalidate misunderstanding') and please update your assertion.
    – owner
    Commented Feb 9, 2016 at 14:34
  • @owner Test this in Matlab: 1 + 2. Then test 'invalidate misunderstanding'. You will see that it also prints something. Yet clearly neither 1 + 2 nor 'invalidate misunderstanding' are print statements. The same is true for sprintf. Commented Feb 9, 2016 at 14:36
3

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

1

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
1
  • It simply does not work in RStudio
    – Julien
    Commented Nov 16, 2022 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.