read_data: Automatic reading in data from a meta-table of external data...

View source: R/read_data.R

read_dataR Documentation

Automatic reading in data from a meta-table of external data sets.

Description

Automatic reading in data from a meta-table of external data sets.

Usage

read_data(x, ...)

## S3 method for class 'data.frame'
read_data(x, name_variable = "name", file_variable = "file", ...)

## S3 method for class 'character'
read_data(
  x,
  data_table,
  fun = NULL,
  frame = parent.frame(),
  name_variable = "name",
  file_variable = "file",
  clean_names_option = FALSE,
  remove_blank_rows_cols_option = FALSE,
  ...
)

Arguments

x

character string or data.frame. If it is a character then it is the name of the object to be created, and referenced within the data to find the file path. If it is a dataframe then read_data is repeated across all the rows of the data.frame.

...

other arguments to supply to fun.

name_variable

character string giving the variable name within data that has the object names to be referenced. Defaults to "name".

file_variable

character string giving the variable name within data that has the file names to be referenced. Defaults to "file".

data_table

data frame containing the meta-table of file paths of the external data files, and their desired R object names.

fun

the function to be used to read in the data file. If unspecified it picks up file extensions ".xsl" and ".xslx" to use readxl::read_xls and readxl::read_xlsx, otherwise uses read.csv. This could actually be any function applied to the file path character string that is extracted from data_table, but a warning is issued if the function name does not contain "read".

frame

Environment in which an object with name given by x is created. Default is parent.frame(). Or if NULL the data read in is returned with no assignement.

clean_names_option

logical to apply the clean_names function internally. Defaults to FALSE for compatibility with apply_macro_dict.

remove_blank_rows_cols_option

logical to apply the remove_blank_rows_cols function internally. Defaults to FALSE for compatibility with apply_macro_dict.

Details

The idea is to improve the tracibility of reading in external data. This should be used in two steps: create a meta-table in R that has a minimum of 2 columns, one with the name of the R data.frame to be created, and the other giving the file path to the external data; use read_data as a wrapper to read in the data as specified. This ends up with less code, and allows an table of extenral data and associated meta-data to be easily produced using data_table_summary. If options("verbose") is TRUE then read_data will display messages describing what objects have been created.

This is a generic method with methods defined for a character string, and a data.frame. The former just reads in one data.frame, the latter reads in all the data.frames specified.

Value

read_data assigns or returns a data frame reading in data from an external file

Methods (by class)

  • read_data(data.frame): data.frame method for read_data generic

  • read_data(character): character method for read_data generic

See Also

read.csv read_excel data_table_summary apply_macro_dict var_lab extract_form

Examples

data_table <- data.frame(name=c("dirtydata","meta"),
                         file=c("dirtydata.csv", "meta_table.xlsx"),
                         folder=system.file("extdata", package="cctu"),
                         stringsAsFactors=FALSE
                         )
data_table_summary(data_table)
options("verbose"=TRUE)
read_data(data_table)
summary(dirtydata)
summary(meta)


shug0131/cctu documentation built on Nov. 10, 2023, 12:03 p.m.