How to load all functions from a folder?

Asked

Viewed 195 times

2

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

2 answers

2

To load the functions can be used a for-loop where the functions you want to load are.

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

2


It might be interesting to create a function that reads files from an arbitrary folder, so you can reuse the code several times. To make the code cleaner, you can use the sapply instead of a loop with for (but in this case it doesn’t make much difference, I will use to exemplify):

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).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.