import_list: Import list of data frames

Description Usage Arguments Value See Also Examples

View source: R/import_list.R

Description

Use import to import a list of data frames from a vector of file names or from a multi-object file (Excel workbook, .Rdata file, zip directory, or HTML file)

Usage

1
2
3
4
5
6
7
8
9
import_list(
  file,
  setclass,
  which,
  rbind = FALSE,
  rbind_label = "_file",
  rbind_fill = TRUE,
  ...
)

Arguments

file

A character string containing a single file name for a multi-object file (e.g., Excel workbook, zip directory, or HTML file), or a vector of file paths for multiple files to be imported.

setclass

An optional character vector specifying one or more classes to set on the import. By default, the return object is always a “data.frame”. Allowed values include “tbl_df”, “tbl”, or “tibble” (if using dplyr) or “data.table” (if using data.table). Other values are ignored, such that a data.frame is returned.

which

If file is a single file path, this specifies which objects should be extracted (passed to import's which argument). Ignored otherwise.

rbind

A logical indicating whether to pass the import list of data frames through rbindlist.

rbind_label

If rbind = TRUE, a character string specifying the name of a column to add to the data frame indicating its source file.

rbind_fill

If rbind = TRUE, a logical indicating whether to set the fill = TRUE (and fill missing columns with NA).

...

Additional arguments passed to import. Behavior may be unexpected if files are of different formats.

Value

If rbind=FALSE (the default), a list of a data frames. Otherwise, that list is passed to rbindlist with fill = TRUE and returns a data frame object of class set by the setclass argument; if this operation fails, the list is returned.

See Also

import, export_list, export

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
library('datasets')
export(list(mtcars1 = mtcars[1:10,], 
            mtcars2 = mtcars[11:20,],
            mtcars3 = mtcars[21:32,]),
    xlsx_file <- tempfile(fileext = ".xlsx")
)

# import a single file from multi-object workbook
str(import(xlsx_file, which = "mtcars1"))

# import all worksheets
str(import_list(xlsx_file), 1)

# import and rbind all worksheets
mtcars2 <- import_list(xlsx_file, rbind = TRUE)
all.equal(mtcars2[,-12], mtcars, check.attributes = FALSE)

# import multiple files
wd <- getwd()
setwd(tempdir())
export(mtcars, "mtcars1.csv")
export(mtcars, "mtcars2.csv")
str(import_list(dir(pattern = "csv$")), 1)
unlink(c("mtcars1.csv", "mtcars2.csv"))
setwd(wd)

# cleanup
unlink(xlsx_file)

rio documentation built on Nov. 22, 2021, 9:07 a.m.