Rstudio difference between run and source

Rstudio

Rstudio Problem Overview


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

Rstudio Solutions


Solution 1 - Rstudio

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

Solution 2 - Rstudio

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.

Solution 3 - Rstudio

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.

Solution 4 - Rstudio

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?

Solution 5 - Rstudio

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

Solution 6 - Rstudio

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.

Solution 7 - Rstudio

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

Solution 8 - Rstudio

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.

Solution 9 - Rstudio

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

Solution 10 - Rstudio

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2543622View Question on Stackoverflow
Solution 1 - RstudioAndy CliftonView Answer on Stackoverflow
Solution 2 - RstudioChristopher SkyiView Answer on Stackoverflow
Solution 3 - RstudioC8H10N4O2View Answer on Stackoverflow
Solution 4 - RstudioAnders Ellern BilgrauView Answer on Stackoverflow
Solution 5 - RstudiojkdView Answer on Stackoverflow
Solution 6 - RstudioJulian ZuckerView Answer on Stackoverflow
Solution 7 - Rstudiouser3771476View Answer on Stackoverflow
Solution 8 - RstudioAtharva DeshpandeView Answer on Stackoverflow
Solution 9 - RstudioKayView Answer on Stackoverflow
Solution 10 - RstudioMcKay Joseph HallView Answer on Stackoverflow