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?
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?
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)
.
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])
}