Skip to contents

This function sources and executes an R script from a specified file path in a new environment, optionally passing objects to that environment.

Usage

run_script_with_args(path, ...)

Arguments

path

A character string specifying the path to the R script to be run.

...

Additional named arguments to be passed to the script's environment.

Value

The function invisibly returns the environment in which the script was evaluated.

Examples

# Create a sample data frame
my_data <- data.frame(a = 1:5, b = letters[1:5])

# Create a temporary script for demonstration
temp_script <- tempfile(fileext = ".R")
writeLines(
  "print('Executing script with data...')\nprint(head(data))",
  temp_script
)

# Run the script, passing the data frame
run_script_with_args(temp_script, data = my_data)
#> Running script: /tmp/Rtmp6vMk6o/file6df27a8ae444.R
#> [1] "Executing script with data..."
#>   a b
#> 1 1 a
#> 2 2 b
#> 3 3 c
#> 4 4 d
#> 5 5 e

# Clean up
unlink(temp_script)