Learn R Programming

rtemis (version 1.4.3)

read: Read tabular data from a variety of formats

Description

Read data and optionally clean column names, keep unique rows, and convert characters to factors

Usage

read(
  filename,
  datadir = NULL,
  remove_duplicates = FALSE,
  character2factor = FALSE,
  clean_colnames = TRUE,
  delim_reader = c("data.table", "vroom", "duckdb", "arrow"),
  parquet_reader = c("nanoparquet", "arrow"),
  xlsx_sheet = 1,
  sep = NULL,
  quote = "\"",
  na_strings = c(""),
  output = c("data.table", "tibble", "data.frame"),
  attr = NULL,
  value = NULL,
  verbosity = 1L,
  fread_verbosity = 0L,
  timed = verbosity > 0L,
  ...
)

Value

data.frame, data.table, or tibble.

Arguments

filename

Character: filename, full path if datadir = NULL, an http:// or https:// URL, or an hf://datasets/ path.

datadir

Character: Optional path to directory where filename is located. If not specified, filename must be the full path. Must be NULL when filename is a URL or an hf:// path.

remove_duplicates

Logical: If TRUE, keep unique rows only.

character2factor

Logical: If TRUE, convert character variables to factors.

clean_colnames

Logical: If TRUE, clean columns names using clean_colnames.

delim_reader

Character: package to use for reading delimited data.

parquet_reader

Character: package to use for reading Parquet files. If undefined and system is WASM, uses "nanoparquet", otherwise "arrow".

xlsx_sheet

Integer or character: Name or number of XLSX sheet to read.

sep

Single character: field separator. If delim_reader = "fread" and sep = NULL, this is "auto", otherwise ",".

quote

Single character: quote character.

na_strings

Character vector: Strings to be interpreted as NA values. For delim_reader = "duckdb", this must be a single string.

output

Character: "default" or "data.table", If default, return the delim_reader's default data structure, otherwise convert to data.table.

attr

Character: Attribute to set (Optional).

value

Character: Value to set (if attr is not NULL).

verbosity

Integer: Verbosity level.

fread_verbosity

Integer: Verbosity level. Passed to data.table::fread

timed

Logical: If TRUE, time the process and print to console

...

Additional arguments to pass to data.table::fread, arrow::read_delim_arrow(), vroom::vroom(), or readxl::read_excel().

Author

EDG

Details

read is a convenience function to read:

An http:// or https:// URL is downloaded with utils::download.file() to a temporary file named after the last segment of the URL path, read like a local file, and deleted. The file extension in the URL path selects the reader, as it does for a local file. The download is bounded by getOption("timeout"), in seconds. A URL with user credentials (user:password@host) and any scheme other than http, https, and hf is rejected.

An hf://datasets/<owner>/<name>[@<revision>][/<file>] path reads a file from a public dataset repository on the Hugging Face Hub, in the path form of huggingface_hub's HfFileSystem. The revision is a branch, tag, or commit, main when none is given; a revision containing / is written URL-encoded, as refs%2Fconvert%2Fparquet. A path that names the repository only reads the repository's one data file (.parquet, .csv, .tsv, .csv.gz, .tsv.gz, .rds, .xlsx, .dta, .arff, or .fasta) and is an error when the repository holds more than one. The revision is resolved to a commit through the Hub API, the file is downloaded at that commit, and its size and hash are checked against the Hub's listing. The HF_ENDPOINT environment variable sets the Hub endpoint. Reading hf:// paths requires the jsonlite package.

Examples

Run this code
if (FALSE) {
# Replace with your own data directory and filename
datadir <- "/Data"
dat <- read("iris.csv", datadir)
# Read from a URL
dat <- read("https://example.org/data/iris.csv")
# Read from a Hugging Face dataset
dat <- read("hf://datasets/rtemis/tcga-pancan-hiseq")
}

Run the code above in your browser using DataLab