R Language *apply family of functions (functionals) Bulk File Loading

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

for a large number of files which may need to be operated on in a similar process and with well structured file names.

firstly a vector of the file names to be accessed must be created, there are multiple options for this:

  • Creating the vector manually with paste0()

     files <- paste0("file_", 1:100, ".rds")
    
  • Using list.files() with a regex search term for the file type, requires knowledge of regular expressions (regex) if other files of same type are in the directory.

     files <- list.files("./", pattern = "\\.rds$", full.names = TRUE)
    

where X is a vector of part of the files naming format used.

lapply will output each response as element of a list.

readRDS is specific to .rds files and will change depending on the application of the process.

my_file_list <- lapply(files, readRDS)

This is not necessarily faster than a for loop from testing but allows all files to be an element of a list without assigning them explicitly.

Finally, we often need to load multiple packages at once. This trick can do it quite easily by applying library() to all libraries that we wish to import:

lapply(c("jsonlite","stringr","igraph"),library,character.only=TRUE)


Got any R Language Question?