How to use the source Function in R Last Updated : 28 Mar, 2022 Comments Improve Suggest changes Like Article Like Report In this article, we will be looking at the practical implementation of the source function in the R programming language. Source Function: Source function in R is used to use functions that are created in another R script. The syntax of this function is given below: source("Users/harsh/Desktop/GeeksforGeeks/gfg.R") All we are required is to put the above line at the top of the script in which we want to use a function of the gfg.R file. Advantages of the source function:Source function increases the reusability of the code. Once written then we can use the same code in any script.Helpful in handling big projects.Tasks can be completed in lesser time. Example: Let's consider that we have an R script that contains two simple functions add() and subtract(). add() function takes two numbers as input and displays the result of the same. subtract() function on the other hand also takes two numbers as input and displays the output of the same. R # gfg.R # Define a function that # adds two number and return the same add <- function(number1, number2) { return(number1 + number2) } # Define a function that # subtracts two number and return the same subtract <- function(number1, number2) { return(number1 - number2) } Now consider that we are using main.R script and within that script, we want to use add and subtract functions that are defined under gfg.R script. So, we can use the statement source("gfg.R") at the top of the script main.R in order to use these functions. Note: Here, main.R script and gfg.R script resides in the same folder. If we want to use some other R script then the full path of the file must be given. Here, we are creating a main.R script and here at the start of this script we are using the source function passed as the parameter gfg.R to use the functions from the gfg.R file within the main.R file. R # main.R # Source function source("gfg.R") # Defining variables number1 = 10 number2 = 20 # Calling add() function defined under gfg.R file add(number1, number2) # Calling subtract() function defined under gfg.R file subtract(number1, number2) Output: Comment More infoAdvertise with us Next Article How to use the source Function in R B bhuwanesh Follow Improve Article Tags : R Language Geeks Premier League Geeks-Premier-League-2022 Similar Reads How to Use sum Function in R? In this article, we will discuss how to use the sum() function in the R Programming Language. sum() function: This is used to return the total/sum of the given data Syntax: sum(data) Arguments: data can be a vector or a dataframeExample 1: Using sum() function to calculate the sum of vector elements 5 min read How to View the Source Code for a Function in R? If you're diving into R programming, there will come a time when you want to look under the hood and see how a function works. Maybe you're curious about the mechanics, or you want to understand it better to use it more effectively. Here's a guide to help you view the source code for a function in R 4 min read How to Use file.path() Function in R R programming language is becoming popular among developers, analysts, and mainly for data scientists. Students are eagerly learning R with Python language to use their analytical skills at their best. While learning any language, one is faced with many difficulties, and the individual learning R Pr 3 min read How to Use the (?) Operator in R The ? operator in R is a simple yet powerful tool that provides quick access to documentation and help pages for functions, datasets, and other objects within the R environment. Understanding how to effectively use this operator can significantly enhance your productivity and help you learn R Progra 4 min read How to use data.table within functions and loops in R? data. table is the R package that can provide the enhanced version of the data. frame for the fast aggregation, fast ordered joins, fast add/modify/delete of the columns by the reference, and fast file reading. It can be designed to provide a high-performance version of the base R's data. frame with 3 min read Like