user2543622
user2543622

Reputation: 6756

Rstudio difference between run and source

I am using Rstudio and not sure how options "run" and "source" are different.

I tried googling these terms but 'source' is a very common word and wasn't able to get good search results :(

enter image description here

Upvotes: 57

Views: 43109

Answers (10)

McKay Joseph Hall
McKay Joseph Hall

Reputation: 51

A big practical difference between run and source is that if you get an unaccounted for error in source it'll break you out of the code without finishing, whereas run will just pass the next line to the console and keep going. This has been the main practical difference I've seen working on cleaning up other people's scripts.

Upvotes: 1

Kay
Kay

Reputation: 915

To those saying plots do not show. They won't show in Plots console. But you can definitely save the plot to disc using Source in RStudio. Using this snippet:

png(filename)
print(p)
dev.off()

I can confirm plots are written to disc. Furthermore print statements are also outputted to the console

Upvotes: 0

Atharva Deshpande
Atharva Deshpande

Reputation: 1

Source/Source with echo is used to execute the whole file whereas Run as far as my personal experience goes executes the line in which your cursor is present. Thus, Run helps you to debug your code. Watch out for the environment. It will display what's happening in the stack.

Upvotes: 0

jkd
jkd

Reputation: 1654

I also just discovered that the encoding used to read the function sourced can also be different if you source the file or if you add the function of the source file to your environment with Ctrl+Enter!

In my case there was a regex with a special character (µ) in my function. When I imported the function directly (Ctrl+Enter) everything would work, while I had an error when sourcing the file containing this function.

To solve this issue I specified the encoding of the sourced file in the source function (source("utils.R", encoding = "UTF-8")).

Upvotes: 7

C8H10N4O2
C8H10N4O2

Reputation: 19005

An important implication of @AndyClifton's answer is:

Rstudio breakpoints work in source (Ctrl-Shift-S) but not in run (Ctrl-Enter)

Presumably the reason is that with run, the code is getting passed straight into the console with no support for a partial submission.

You can still use browser() though with run though.

print() to console is supported in debugSource (Ctrl-Shift-S) as well as run.

Upvotes: 11

Julian Zucker
Julian Zucker

Reputation: 564

Run will run each line of code, which means that it hits enter at the beginning of each line, which prints the output to the console. Source won't print anything unless you source with echo, which means that ggplot won't print to pngs, as another posted mentioned.

Upvotes: 4

Christopher Skyi
Christopher Skyi

Reputation: 229

Sometimes, for reasons I don't understand, you will get different behavior depending on whether you select all the lines of code and press the run the button or go to code menu and chose 'source.' For example, in one specific case, writing a gplot to a png file worked when I selected all my lines of code but the write failed to when I went to the code menu and chose 'source.' However, if I choose 'Source with Echo,' I'm able to print to a png file again.

I'm simply reporting a difference here that I've seen between the selecting and running all your lines and code and going to code menu and choosing 'source,' at least in the case when trying to print a gplot to a png file.

Upvotes: 10

Andy Clifton
Andy Clifton

Reputation: 5056

Run and source have subtly different meanings. According to the RStudio documentation,

The difference between running lines from a selection and invoking Source is that when running a selection all lines are inserted directly into the console whereas for Source the file is saved to a temporary location and then sourced into the console from there (thereby creating less clutter in the console).

Something to be aware of, is that sourcing functions in files makes them available for scripts to use. What does this mean? Imagine you are trying to troubleshoot a function that is called from a script. You need to source the file containing the function, to make the changes available in the function be used when that line in the script is then run.

A further aspect of this is that you can source functions from your scripts. I use this code to automatically source all of the functions in a directory, which makes it easy to run a long script with a single run:

# source our functions
code.dir <- "c:\temp"
code.files = dir(code.dir, pattern = "[.r]")
for (file in code.files){
  source(file = file.path(code.dir,file))
}

Upvotes: 42

user3771476
user3771476

Reputation: 1

When using RSTudio u can press the run button in the script section - it will run the selected line. Next to it you have the re - run button, to run the line again. and the source button next to it will run entire chuncks of code.

I found a video about this topic:

http://www.youtube.com/watch?v=5YmcEYTSN7k

Upvotes: 0

Anders Ellern Bilgrau
Anders Ellern Bilgrau

Reputation: 10223

The "run" button simply executes the selected line or lines. The "source" button will execute the entire active document. But why not just try them and see the difference?

Upvotes: 7

Related Questions