Brad
Brad

Reputation: 853

R: Source function by name/Import subset of functions

I have a question with importing functions.

Say I have a R script named "functions" which looks like this:

mult <- function(x,y){

   return(x*y)

}

divide <- function(x,y){

   return(x/y)

}

Currently I am importing all functions in the script:

source(file="C:\\functions.R",echo=FALSE)

The problem is that the (actual) R script is getting very large.

Is there a way to import the "mult" function only?

I was looking at evalSource/insertSource but my code was not working:

insertSource("C:\\functions.R", functions="mult")  

Upvotes: 10

Views: 4814

Answers (2)

Brad
Brad

Reputation: 853

I ended up creating functions to do what you recommended.

This first group allows for multiple functions in one call:

LoadFunction <- function(file,...) {

  dots <- match.call(expand.dots = FALSE)$...
  dots <- sapply(dots, as.character)

  output <- lapply(dots, function(x,file){eval(parse(text=paste(x," <- function(x) {0}",sep="")),envir = .GlobalEnv)
                                          suppressMessages(insertSource(file, functions=x))
                                          eval(parse(text=paste(x," <- ",x,"@.Data",sep="")),envir = .GlobalEnv) },file=file)

}


UnloadFunction <- function(...) {

  dots <- match.call(expand.dots = FALSE)$...
  dots <- sapply(dots, as.character)

  output <- lapply(dots, function(x,file){eval(parse(text=paste("rm(",x,",envir = .GlobalEnv)",sep="")))},file=file)

}

They are called like this:

LoadFunction(file="C:\\functions.R",mult,divide)
UnloadFunction(mult,divide)

The second is only one function per call:

LoadFunction2 <- function(file,function_name) {

  eval(parse(text=paste(function_name," <- function(x) {0}",sep="")),envir = .GlobalEnv)
  suppressMessages(insertSource(file, functions=function_name))
  eval(parse(text=paste(function_name," <- ",function_name,"@.Data",sep="")),envir = .GlobalEnv)         

}

UnloadFunction2 <- function(function_name) {

  eval(parse(text=paste("rm(",function_name,",envir = .GlobalEnv)",sep="")))

}

They are called like this:

LoadFunction2(file="C:\\functions.R","mult")
LoadFunction2(file="C:\\functions.R","divide")
UnloadFunction2("mult")
UnloadFunction2("divide")

Upvotes: 9

andybega
andybega

Reputation: 1437

It looks like your code will work with a slight change: define an empty object for the function you want to load first, then use insertSource.

mult <- function(x) {0}
insertSource("C:\\functions.R", functions="mult") 
mult 

Which gives:

Object of class "functionWithTrace", from source
function (x, y) 
{
    return(x * y)
}

## (to see original from package, look at object@original)

The mult object has some additional information that I suppose is related to the original application for insertSource, but you could get rid of them with mult <- [email protected], which will set mult to the actual function body only.

Also, you might be interested in the modules project on github, which is trying to implement a lightweight version of R's package system to facilitate code reuse. Seems like that might be relevant, although I think you would have to split your functions into separate files in different subdirectories.

Upvotes: 8

Related Questions