How to Use the Source Function to Run Another R Script

Jesse John Feb 02, 2024
  1. the source Function in R
  2. Use Absolute Path of the Main Script in R
  3. Use Relative Path to the Main Script in R
  4. Conclusion
How to Use the Source Function to Run Another R Script

This article explores how to use the source() function to run another R script located in the same or a different directory.

We will assume the following conditions for our files.

  1. The path of the main script is hardcoded in the source() function call in the wrapper script.
  2. The resources that the main script accesses are in the same directory or its sub-directories; the paths must be relative to it.

the source Function in R

We can use the source() function to make R parse and evaluate code stored in an R script.

The chdir argument can temporarily change R’s working directory to the script’s directory passed to the source().

Use Absolute Path of the Main Script in R

If the path of the main script in the wrapper script is an absolute path, it can be anywhere in the file system relative to the wrapper script. We need to use chdir = TRUE in the source() function to enable R to find the resources needed by the main script.

For example, use a wrapper script with the following code. The wrapper file and main script can be anywhere.

# Get and print the working directory.
print("The starting working directory was:")
print(getwd())

# Call and process the main script file.
source("/absolute/path/to/main/script/filename.R", chdir = TRUE)

print("Final working directory")
print(getwd())
  1. Open a terminal window in any folder.
  2. Run the R command to get the R command line (console).
  3. Run the getwd() command.

The directory where the R command was run is the current working directory. Run the wrapper script with the following command, giving the absolute path.

source("/absolute/path/to/wrapper/script/filename.R")

R will execute the code in the wrapper script. It will change the working directory temporarily to the directory where the main script is located.

It will execute the main script and then change the working directory back to the original location from which the wrapper script was called.

We did not use chdir = TRUE when calling the wrapper script because the main script was not in the same folder as the wrapper script.

Use Relative Path to the Main Script in R

If the main script is in the same folder as the wrapper script or a sub-folder, we can use a relative path to the main script in the wrapper script.

The source() function used on the command line first gets R to temporarily change the working directory to the wrapper script, using chdir = TRUE.

If the main script is in a sub-folder, the call to the source() in the wrapper script again gets R to temporarily change the working directory to that of the main script.

For example, use a wrapper file with the following code.

# Get and print the first temporary working directory.
print("The first temporary working directory is:")
print(getwd())

# Call and process the real file.
source("relative_path_from_wrapper_to_main_script/filename.R", chdir = TRUE)

print("Current temporary working directory")
print(getwd())

The argument chdir = TRUE is only required if the main script is in a sub-folder of the folder which holds the wrapper script.

Open R in any folder, and execute the wrapper script as follows.

source("/absolute/path/to/wrapper/script/filename.R", chdir = TRUE)

Now, chdir = TRUE is required when the wrapper script is called on the command line because we want to first change the working directory to that of the wrapper script.

If we open R in the same directory as the wrapper script, both the path and chdir are not required.

After the scripts finish execution, run the getwd() command to verify that the current working directory is the one in which R was run.

Conclusion

We saw how the source() function and its chdir argument could be combined to execute the main R script from another wrapper script.

The relative (or even absolute) path to the main script must be hardcoded in the wrapper script. The resources that the main script accesses should be in the same directory as itself or in a sub-directory.

Author: Jesse John
Jesse John avatar Jesse John avatar

Jesse is passionate about data analysis and visualization. He uses the R statistical programming language for all aspects of his work.

Related Article - R Function