Run R Script within Other Scripts (2 Examples)

 

In this R programming post you’ll learn how to include an R script into another script.

Table of contents:

Let’s dig in.

 

Preparing the Examples

We’ll use the following user-defined function as basement for this R programming tutorial:

my_fun <- function() {                          # Create user-defined function
  cat("Yeay - This function exists!")
}
my_fun()                                        # Apply user-defined function
# Yeay - This function exists!

Have a look at the previous output of the RStudio console. It shows that our example function returns the sentence “Yeay – This function exists!”.

Let’s assume that we have stored the R script that defines this function in an R file on the desktop of our computer.

In the following examples I’ll explain how to read this R script from another R script.

 

Example 1: Reading R Code from a File Using source() Function

Let’s make sure that the content of our R script that we want to load (i.e. the function my_fun) is not stored in our environment:

rm(my_fun)                                      # Remove user-defined function

If we now try to run our function, the error message Error in my_fun() : could not find function “my_fun” is returned, indicating that our R script is not loaded yet:

my_fun()                                        # Apply user-defined function
# Error in my_fun() : could not find function "my_fun"

We can read the R script containing our function by applying the source command as shown below:

source("C:/Users/Joach/Desktop/my_file.R")      # Apply source function

Let’s try to run our function again:

my_fun()                                        # Apply user-defined function
# Yeay - This function exists!

Yeay – it worked!

 

Example 2: Reading R Code from a File in Case Certain Functions do not Exist

Depending on the size of your R script you might prefer to load it only in case it is not loaded yet. This example shows how to check whether an R script is already loaded before reading it.

In preparation for this example, let’s make sure that our function is not loaded yet…

rm(my_fun)                                      # Remove user-defined function

…and that we cannot execute it:

my_fun()                                        # Apply user-defined function
# Error in my_fun() : could not find function "my_fun"

Now, we can apply the exists function within an if-statement to check if our script was already loaded. If the function my_fun already exists, our script is not loaded again.

if(!exists("my_fun")) {                         # Check if user-defined function exists
 
  source("C:/Users/Joach/Desktop/my_file.R")    # Apply source function
}

Let’s run our function again to see if it’s loaded:

my_fun()                                        # Apply user-defined function
# Yeay - This function exists!

Yes, it is!

 

Video, Further Resources & Summary

I have recently published a video on my YouTube channel, which illustrates the R code of this tutorial. Please find the video below.

 

The YouTube video will be added soon.

 

In addition, you might want to read the related tutorials of my website. I have released numerous articles about R scripts already:

 

In summary: In this post, I showed how to combine and run multiple R scripts from another script in R. Let me know in the comments section, if you have any further comments and/or questions. Furthermore, don’t forget to subscribe to my email newsletter for regular updates on new posts.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top