6

I have about 30 functions for a research project and don't want to type

source(paste("C:/functions","/function1.txt",sep=""))

30 times where C:/functions is my directory of functions and /function1.txt is a particular function.

I've tried

files <- list.files("C:/functions")
sapply(1:length(files),source(paste("C:/functions/",files[i],sep="")))

And it doesn't work. Error message: Error in match.fun(FUN) : c("'source(paste(\"C:/functions/\", ' is not a function, character or symbol", "' files[i], sep = \"\"), TRUE)' is not a function, character or symbol")

I've also tried it with a for loop and it doesn't work.

1
  • 1
    Note that the edit you made makes some parts of your question not make much sense. For instance... what you have now actually does work and wouldn't correspond to the error you have posted. Commented Aug 29, 2012 at 12:55

6 Answers 6

10

If you have a collection of that many functions, you could also create an R package. Advantages:

  • Nice way to include documentation alongside your functions, especially when using roxygen2.
  • Easy way to distribute your code to other people.
  • Tests can be included with the source code.
  • Easy loading of all your functions using library.
  • The ability to only expose top level functions to the user, leave the low level ones only for internal use.

For more details I refer to Writing R extensions.

Sign up to request clarification or add additional context in comments.

1 Comment

Better answer than mine, albeit requiring a little more effort :-)
7

Slight change of seancarmody's answer:

files <- list.files("C:/functions",full.names=TRUE,pattern="\\.txt")
sapply(files, source)

Comments

6

Some R code for sourcing a directory of files is given in the ?source help. In particular:

## If you want to source() a bunch of files, something like
## the following may be useful:
sourceDir <- function(path, trace = TRUE, ...) {
    for (nm in list.files(path, pattern = "\\.[RrSsQq]$")) {
        if(trace) cat(nm,":")           
        source(file.path(path, nm), ...)
        if(trace) cat("\n")
     }
}

To call the function, just have something like:

sourceDir("C:/function")

You could always put the function in your Rprofile.


One minor point, you have a file extension of .txt, this means in the above function you would change the pattern matcher to:

pattern = "\\.txt$"

Comments

5
files <- list.files("C:/functions")
sapply(files, function(x) source(paste0("C:/functions/", x)))

Note that sapply requires a function as the second argument.

Comments

2

Perhaps you'll like this...

install.packages('R.utils')
library(R.utils)
sourceDirectory( 'C:/functions', '*.txt' )

See ?sourceDirectory for the sourcing goodness...

Arguments

path    
A path to a directory to be sourced.

pattern 
A regular expression file name pattern to identify source code files.

recursive   
If TRUE, subdirectories are recursively sourced first, otherwise not.

envir   
An environment in which the code should be evaluated.

onError 
If an error occures, the error may stop the job, give a warning, or silently be skipped.

verbose 
A logical or a Verbose object.

... 
Additional arguments passed to sourceTo().

Comments

1

It doesn't hurt to get away from the Matlab paradigm of "one function per file." You could put all of your functions into a single my_research_functions.R file, and then just do source('C:/functions/my_research_functions.R')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.