Package 'R8.io'

Title: Read and Write Table Files with 'R8' Types
Description: Readers and writers of table files for the types of 'R8'. A column is read as its declared type, and each text that does not read is reported once, with its count and its first rows, for the user to give what it reads as. A column without a declared type has the type that all its cells have, and is text otherwise.
Authors: E.D. Gennatas [aut, cre, cph] (ORCID: <https://orcid.org/0000-0001-9280-3609>), Authors of the bundled Rust crates [ctb, cph] (Rust crates compiled into the package; see inst/AUTHORS)
Maintainer: E.D. Gennatas <[email protected]>
License: BSD_3_clause + file LICENSE
Version: 0.6.0.9000
Built: 2026-11-29 09:04:59 UTC
Source: https://github.com/rtemis-org/r8

Help Index


Read a table from a file

Description

r8read_table() reads a CSV, TSV, or Parquet file as a value of a table type, and r8check_file() reports what in the file does not read as the type or does not satisfy its rules. Reading requires: it gives a value of the type, or an error that lists what does not read. Checking looks: it raises no error for what it finds.

Usage

r8read_table(
  file,
  type = NULL,
  infer_types = "strict",
  na_strings = c("", "NA"),
  na_strings_in = NULL,
  recode_in = NULL,
  format = NULL,
  delimiter = NULL,
  encoding = NULL,
  time_zone = "UTC"
)

r8check_file(
  file,
  against,
  limit = 1000L,
  infer_types = "strict",
  na_strings = c("", "NA"),
  na_strings_in = NULL,
  recode_in = NULL,
  format = NULL,
  delimiter = NULL,
  encoding = NULL,
  time_zone = "UTC"
)

Arguments

file

Character: The file. A CSV or TSV file whose name ends in .gz is decompressed.

type

Optional table type: An R8 table type (R8::new_table_type()), or an R8::type_table(), of which the result is a R8::Raven.

infer_types

Character {"strict", "none"}: How the columns without a declared type are read.

na_strings

Character: The texts of missing cells.

na_strings_in

Optional named list: Further texts of missing cells, by column: list(age = "-99").

recode_in

Optional named list: The values that texts read as, by column: list(lab = c("<0.1" = 0.1)).

format

Optional character {"csv", "tsv", "parquet"}: The format; NULL takes it from the name of the file.

delimiter

Optional character: The character between two cells of a CSV or TSV file; NULL takes it from the format.

encoding

Optional character {"UTF-8", "latin1", "windows-1252"}: The encoding of a CSV or TSV file; NULL is UTF-8. A file that begins with the mark of UTF-16 is read as such.

time_zone

Character: The IANA name of the time zone of the date-times without an offset, such as "America/New_York".

against

A table type: an R8 table type, or an R8::type_table().

limit

Integer: The problems listed for each column and rule.

Value

r8read_table(): A value of type, or a R8::Raven; raises r8_value_error with the report, in the field report, where a cell does not read, and r8_file_error for a malformed file.

r8check_file(): A value of the struct R8io::FileReport, which extends R8::Report (R8::r8check()) with file; unread, a table of the texts that do not read, with the number of their cells and the first row; recoded, a table of the texts that recode_in names, with their values and the number of their cells; and lines, the lines that have another number of cells than the header. The cells that do not read are among problems with the code unread, and a rule has no answer in their rows.

With a type

A column is read as it is declared. A cell whose text is not of its type is collected with the other cells of the same text: 40 cells of ⁠<0.1⁠ in a column of numbers are one decision. recode_in gives what such a text reads as, and na_strings_in the texts that are missing. Both are lists by column, which a script keeps beside the type and uses for the next file, where a new text is an error again. The value of a recoded text is of the column's type; a text that reads as it is cannot be recoded.

A column of the file that the type does not declare is read, with infer_types, where the type is open, and is an error where it is not.

Without a type

With infer_types = "strict", a column has the first of these types that every cell has: logical, integer, double, date, and date-time. Otherwise it is text, with the cells as they are in the file. Every cell is read, and none is changed to fit: a column with a number that has zeros before it, such as 00123, is text. With infer_types = "none", every column is text.

Texts of each type

A logical is TRUE, True, or true, or their opposites. A number has a decimal point. A date is 2024-01-31. A date-time is ⁠2024-01-31T10:00:00Z⁠, with an offset, or ⁠2024-01-31 10:00:00⁠, without one, which is the clock reading of time_zone. Spaces around one of these are not part of it.

Parquet

A column has the type that the file stores: BOOLEAN is logical; INT32, INT64, and the other integers are integer where every value is one of R8's integers, double where every value is within 2^53, and text otherwise; FLOAT and DOUBLE are double; STRING is text, as are ENUM, JSON, and UUID; DATE is a date; TIMESTAMP is a date-time, and one without a time zone is the clock reading of time_zone; DECIMAL is integer at scale 0, and double otherwise. A string is not read as a number unless the type declares its column so. A raw BYTE_ARRAY, INT96, TIME, and a nested or repeated column are an error that names the column.

With a type, a stored value is read where it is of the declared type exactly: a whole double as an integer, an integer within 2^53 as a double, a date-time at midnight as a date, and any value as text. Another value does not read, and is reported by its text as a CSV holds it, which recode_in and na_strings name: recode_in = list(n = c("2.5" = 2L)). A string is a quoted text of a CSV.

Time zones

A date-time without an offset, a CSV text such as ⁠2024-03-10 08:15:00⁠ or a Parquet TIMESTAMP that is not adjusted to UTC, is the instant at which a clock in time_zone shows it. A clock reading that the zone skips or shows twice, at a change of daylight saving time, does not read. The database of zones is that of the package, the same on every platform.

Missing cells

A cell is missing where its text is one of na_strings, exactly. In a column of text, a quoted "" is an empty text, and a quoted "NA" is that text; na_strings_in makes them missing.

Malformed files

A quote that is not closed, characters after a closing quote, a line with another number of cells than the header, a name that repeats in the header or is missing, and bytes that are no text in encoding are errors with the line.

Examples

op <- options(r8.namespace = "demo")
file <- tempfile(fileext = ".csv")
writeLines(c("id,lab", "1,0.4", "2,<0.1", "3,12"), file)
r8read_table(file)

Labs <- R8::new_table_type("Labs", columns = list(
  id = R8::type_integer(),
  lab = R8::type_double(min = 0)
))
try(r8read_table(file, Labs))
r8check_file(file, Labs)$unread
r8read_table(file, Labs, recode_in = list(lab = c("<0.1" = 0.1)))
unlink(file)
options(op)

Write a table to a file

Description

Writes the columns of a table as a CSV, TSV, or Parquet file, so that r8read_table() with the type of the table reads the same values. Units and descriptions are not written.

Usage

r8write_table(x, file, format = NULL, delimiter = NULL)

Arguments

x

A value of a table, or a data frame with columns that a table takes.

file

Character: The file, which is replaced where it exists.

format

Optional character {"csv", "tsv", "parquet"}: The format; NULL takes it from the name of the file.

delimiter

Optional character: The character between two cells of a CSV or TSV file; NULL takes it from the format.

Details

A CSV or TSV file is UTF-8. A missing cell is empty. A text is quoted where it needs to be, and where it is empty or NA. A number has the shortest form that reads back exactly. A date and a date-time are in ISO 8601, a date-time in UTC. The cell of a factor is its label.

A Parquet file holds a logical as BOOLEAN, an integer as INT32, a double as DOUBLE, a text and a factor as STRING, the factor by its labels, a date as DATE, and a date-time as TIMESTAMP of microseconds adjusted to UTC. A missing cell is a null, and NaN a value. Every column is optional, and the pages are compressed with snappy.

Value

file, invisibly.

Examples

file <- tempfile(fileext = ".csv")
r8write_table(R8::Raven(a = c(1.5, NA), b = c("x", "NA")), file)
readLines(file)
r8read_table(file)
unlink(file)

file <- tempfile(fileext = ".parquet")
r8write_table(R8::Raven(a = c(1.5, NA), b = c("x", "NA")), file)
r8read_table(file)
unlink(file)