In case someone is looking for this with a simple source file and not with a saved Rdata / RDS / Rda file, the solution is very similar to the solution provided by @Hong Ooi
load_obj <- function(fileName) { local_env = new.env() source(file = fileName, local = local_env) return(local_env[[names(local_env)[1]]]) } my_loaded_obj = load_obj(fileName = "TestSourceFile.R") my_loaded_obj(7)
Print
[1] "The arg value is 7"
And in a separate source file TestSourceFile.R
myTestFunction = function(arg) { print(paste0("Value of arg is ", arg)) }
Again, this solution only works if there is exactly one file, if there are more, then it will simply return one of them (perhaps the first, but this is not guaranteed).
user2711915 Sep 28 '16 at 16:33 2016-09-28 16:33
source share