Reputation: 798
I'm trying to load some code for the server file from a folder. I've tried the following approaches, but they didn't work.
sapply(list.files(pattern="[.]R$", path="R/", full.names=TRUE), source)
sourcefiles <- list.files(full.names=TRUE, recursive = TRUE, pattern = "[.]R$")
sapply(sourcefiles, source, chdir = TRUE)
lapply(list.files(pattern = "[.]R$", recursive = TRUE), source)
This code works, but I have to call them individually.
source(file.path("./R/modules/", "plot.R"), local = TRUE)$value
source(file.path("./R/modules/", "freq.R"), local = TRUE)$value
I'm looking for a way to apply a function to loop through the files in the folder.
Upvotes: 0
Views: 587
Reputation: 7106
This code takes all the name of files in the home directory and filters the names that en in .R and maps source the ones that match.
library(purrr)
library(stringr)
list.files(path = '~') %>%
str_subset('\\.R') %>%
map(~source(.x))
Upvotes: 1