Source Files
In this page:
Source Files
source("file.R") runs another R script in the current session, making its functions and variables available, similar to importing a module. This lets you split a large project into organized, reusable files instead of one long script. Source files are commonly used to separate helper functions from the main analysis script.
Note: Keep reusable functions in a separate helpers.R file and source("helpers.R") from your main analysis script.
Example: Source Files
# Simulating a sourced helper file inline for this example
helper_code <- 'square <- function(x) x^2\ncube <- function(x) x^3'
writeLines(helper_code, "helpers.R")
source("helpers.R")
cat("Square of 4:", square(4), "\n")
cat("Cube of 3:", cube(3), "\n")
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: