Using source () in a loop to automatically load my functions?

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

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

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

I tried

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

And it does not 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 also tried it with a for loop and it does not work.

+6
source share
6 answers

If you have a collection of many features, you can also create the R package. Benefits:

  • A good way to include documentation along with your features, especially when using roxygen2 .
  • An easy way to distribute code to other people.
  • Tests can be included in the source code.
  • Easy loading of all your functions with library .
  • Ability to publish only top-level functions for the user, leave low-level only for internal use.

See Writing R Extensions for more information.

+10
source

For some R code to search for a file directory, see ?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 a function, simply enter something like:

 sourceDir("C:/function") 

You can always put a function in your Rprofile.


One minor point, you have the .txt file extension, which means that in the above function you have to change the pattern match to:

 pattern = "\\.txt$" 
+6
source

Minor change to seancarmody answer:

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

Note that sapply requires a function as a second argument.

+4
source

You might like this ...

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

Have a look? sourceDirectory for good quality ...

 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(). 
+2
source

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

+1
source

Source: https://habr.com/ru/post/924064/


All Articles