Download all files from a folder and subfolders

I know about source() in R. Download requires a path and / or file name, that is, a function that is saved in another .R file. I basically need the same command, but it should load every .R file from one folder and its subfolders.

Is there an oneliner (some library) or do I need to write the loop 'n everything?

+5
source share
3 answers

It can work

 lapply(list.files(pattern = "[.]R$", recursive = TRUE), source) 
+10
source

In the help of the R library, you can find the following:

  ## 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") } } 
0
source

You can use a simple recursive function like

 sourceRecursive <- function(path = ".") { dirs <- list.dirs(path, recursive = FALSE) files <- list.files(path, pattern = "^.*[Rr]$", include.dirs = FALSE, full.names = TRUE) for (f in files) source(f) for (d in dirs) sourceRecursive(d) } 
0
source

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


All Articles