Create a data frame from a Workbook (with extra features)
Source:R/read_xlsx_ext.R
read_xlsx_ext.Rdread_xlsx_ext() uses openxlsx2::read_xlsx() with a few added features:
allows use of a name repair argument (
"unique_quite"by default) to avoid blank""orNAvalues for column names.allows vector inputs for the file or sheet argument. These parameters are recycled to a common length and result in the return of a data frame list unless the
combine = TRUEis set. Ifcombine = TRUE, setnames_to(passed topurrr::list_rbind) to combined the file basename values (default) or full path values as a column (depending on thenames_fromargument).names_fromcan also be a length > 1 character vector that can be recycled to match the length of file.
Usage
read_xlsx_ext(
file,
sheet = 1,
...,
names_from = "basename",
names_to = rlang::zap(),
combine = TRUE,
repair = "unique_quiet"
)Arguments
- file
An xlsx file, wbWorkbook object or URL to xlsx file.
- sheet
Defaults to 1.
- ...
Arguments passed on to
openxlsx2::read_xlsxstart_rowfirst row to begin looking for data.
start_colfirst column to begin looking for data.
row_namesIf
TRUE, the first col of data will be used as row names.col_namesIf
TRUE, the first row of data will be used as column names.skip_empty_rowsIf
TRUE, empty rows are skipped.skip_empty_colsIf
TRUE, empty columns are skipped.rowsA numeric vector specifying which rows in the xlsx file to read. If
NULL, all rows are read.colsA numeric vector specifying which columns in the xlsx file to read. If
NULL, all columns are read.detect_datesIf
TRUE, attempt to recognize dates and perform conversion.na.stringsA character vector of strings which are to be interpreted as
NA. Blank cells will be returned asNA.na.numbersA numeric vector of digits which are to be interpreted as
NA. Blank cells will be returned asNA.fill_merged_cellsIf
TRUE, the value in a merged cell is given to all cells within the merge.named_regionCharacter string with a
named_region(defined name or table). If no sheet is selected, the first appearance will be selected. Seewb_get_named_regions()check_namesIf
TRUEthen the names of the variables in the data frame are checked to ensure that they are syntactically valid variable names.show_hyperlinksIf
TRUEinstead of the displayed text, hyperlink targets are shown.
- names_to
By default,
names(x)are lost. To keep them, supply a string tonames_toand the names will be saved into a column with that name. Ifnames_tois supplied andxis not named, the position of the elements will be used instead of the names.- combine
If
TRUE, always return a data frames. IfFALSE, return a list of data frames.- repair
Either a string or a function. If a string, it must be one of
"check_unique","minimal","unique","universal","unique_quiet", or"universal_quiet". If a function, it is invoked with a vector of minimal names and must return minimal names, otherwise an error is thrown.Minimal names are never
NULLorNA. When an element doesn't have a name, its minimal name is an empty string.Unique names are unique. A suffix is appended to duplicate names to make them unique.
Universal names are unique and syntactic, meaning that you can safely use the names as variables without causing a syntax error.
The
"check_unique"option doesn't perform any name repair. Instead, an error is raised if the names don't suit the"unique"criteria.The options
"unique_quiet"and"universal_quiet"are here to help the user who calls this function indirectly, via another function which exposesrepairbut notquiet. Specifyingrepair = "unique_quiet"is like specifyingrepair = "unique", quiet = TRUE. When the"*_quiet"options are used, any setting ofquietis silently overridden.