How to load all the functions of a folder?

2

Suppose in the "D:" folder there are several functions that I use in a project. How do I load all these functions with a script?

    
asked by anonymous 17.02.2014 / 21:14

2 answers

2

It may be interesting to create a function that reads the files from an arbitrary folder, so you can reuse the code multiple times. To leave the code cleaner, you can use sapply instead of a loop with for (but in that case it does not make much difference, I'll use it to give an example):

lerFuncoes <- function(pasta){
  files <-list.files(pasta) #pega os arquivos
  sapply(sapply(pasta, paste, files, sep=""),source, echo=FALSE)
  NULL
}

Then just use lerFuncoes(pasta) .

    
17.02.2014 / 21:49
2

To load functions you can use a for loop where the functions you want to load are available.

setwd("D:")    
funcoes<-list.files()
len<-length(funcoes)
for (i in 1:len)
{source(funcoes[i])      
}
    
17.02.2014 / 21:16