Learn R Programming

HelpersMG (version 5.1)

read_folder: Read files present in a folder and creates a list with the content of these files

Description

To create a list, the syntax is: datalist <- read_folder(folder=".", read=read.delim, header=FALSE) It returns an error if the folder does not exist. The names of the elements of the list are the filenames. The parameter file can be used to predefine a list of file. If file is NULL, all the files of the folder/directory are used.

Usage

read_folder(
  folder = try(file.choose(), silent = TRUE),
  file = NULL,
  wildcard = "*.*",
  read = read.delim,
  ...
)

Arguments

folder

Where to search for files; can be or a file path or a folder path

file

list of files

wildcard

Define which files are to be read (examples: "*.*", "*.xls", "essai*.txt"). It can be also a vector with all filenames.

read

Function used to read file. Ex: read.delim or read.xls from gdata package

...

Parameters send to the read function

Value

Return a list with the data in the files of the folder (directory for windows users)

Details

read_folder reads all files present in a folder

Examples

Run this code
# NOT RUN {
library(HelpersMG)
# Read all the .csv files from the current folder/directory
contentaslist <- read_folder(folder=".", wildcard="*.csv", read=read.csv2)
# Read all the files from the current folder/directory
contentaslist <- read_folder(folder=".", wildcard="*.*", read=read.csv2)
# Read two files from the current folder/directory
files <- c("filename1.csv", "filename2.csv")
contentaslist <- read_folder(folder=".", wildcard=files, read=read.csv2)
# }

Run the code above in your browser using DataLab