56

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

user2543622
  • 5,760
  • 25
  • 91
  • 159

10 Answers10

40

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))
}
Andy Clifton
  • 4,926
  • 3
  • 35
  • 47
  • 4
    Alternatively you could create a package out of your functions. – Dason Jun 25 '14 at 21:05
  • 1
    True. I often do this for data analysis where file formats and processing changes from project to project. I also wanted to demonstrate how the OP could use `source` in a real-world application. – Andy Clifton Jun 25 '14 at 21:18
  • 2
    That code is essentially equivalent to the code given in the example for source. You could just use that: `example(source); sourceDir("c:\\temp")` – Dason Jun 25 '14 at 21:20
10

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.

Christopher Skyi
  • 229
  • 1
  • 3
  • 12
9

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.

C8H10N4O2
  • 18,312
  • 8
  • 98
  • 134
7

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?

Anders Ellern Bilgrau
  • 9,928
  • 1
  • 30
  • 37
  • 3
    For what it's worth, I just quickly responded to an unanswered question at the time. But now, some 6 years later, it regularly seeing downvotes and I see how it might be come off as a bit rude in the context of the many elaborate answers. – Anders Ellern Bilgrau May 06 '20 at 18:24
7

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

jkd
  • 1,327
  • 14
  • 29
  • I just had different behavior when manually running a file and running it with source. It was exactly because I was filtering a `tibble` with a string with an accented character. Running passing the encoding solved the problem. Thanks! – Cassio Pereira May 17 '20 at 20:15
4

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.

Julian Zucker
  • 564
  • 4
  • 13
  • Yes _Run_ hits enter at the beginning of each line. But it _does_ print output to the console. Create a new R file and enter ```print("Hello World")``` Save this file (as x.R) and then from the console do ```source("x.R") ``` You should see ```[1] "Hello World"``` on your _Console_ Please see my other answer where I show it can write plots also – Kay Dec 17 '20 at 11:54
1

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.

0

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

0

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.

0

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

Kay
  • 799
  • 1
  • 11
  • 29