Skip to main content

15  Reading a file

The package R8.io reads CSV, TSV, and Parquet files as R8 tables, and writes them. It is a package of its own, which imports R8 and is installed beside it:

install.packages(
  "R8.io",
  repos = c("https://rtemis-org.r-universe.dev", "https://cloud.r-project.org")
)

Its readers are the Rust crate rtemis-r8-io, which converts each cell to its column’s type in one pass over the file. The chapters of this part are this one, Texts that do not read, Parquet and time zones, and Writing a file.

library(R8.io)
dir <- file.path(tempdir(), "files")
dir.create(dir, showWarnings = FALSE)
admissions <- file.path(dir, "admissions.csv")
writeLines(c(
  "id,ward,age,sbp,admitted,discharged",
  "1,ED,40,128,2026-01-03,2026-01-07",
  "2,ICU,52,141,2026-01-04,",
  "3,ED,61,NA,2026-01-05,2026-01-09"
), admissions)

15.1 Inferred types

r8read_table(file) reads a file without a type, and gives a raven with a type for each column. With infer_types = "strict", the default, 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:

r8read_table(admissions)
<raven table> 3 rows ⨉ 6 columns
        id  ward           age      sbp  admitted    discharged
   integer  character  integer  integer  Date        Date
                                   1 NA              1 NA
1        1  ED              40      128  2026-01-03  2026-01-07
2        2  ICU             52      141  2026-01-04  NA
3        3  ED              61       NA  2026-01-05  2026-01-09

Every cell is read, and none is changed to fit. A number with zeros before it, such as the identifier 00123, is not an integer, so its column is text; a whole number beyond 2^53 is text too. With infer_types = "none", every column is text:

r8read_table(admissions, infer_types = "none")
<raven table> 3 rows ⨉ 6 columns
   id         ward       age        sbp        admitted    discharged
   character  character  character  character  character   character
                                    1 NA                   1 NA
1  1          ED         40         128        2026-01-03  2026-01-07
2  2          ICU        52         141        2026-01-04  NA
3  3          ED         61         NA         2026-01-05  2026-01-09

The texts of each type are:

Type Texts
logical TRUE, True, true, FALSE, False, false
integer whole numbers, such as 42 and -7
double numbers with a decimal point or an exponent, such as 1.5 and 2e-3
date 2026-01-31
date-time 2026-01-31T10:00:00Z, with an offset, or 2026-01-31 10:00:00, the clock reading of time_zone (Parquet and time zones)

Spaces around a text are not part of it.

15.2 A declared type

A table type states what a file holds: the type of each column, its range, whether its cells may be missing, and rules across columns (Declaring a table type). r8read_table(file, type) reads each column as its type declares it, and gives a value of the type:

Admissions <- new_table_type("Admissions", columns = list(
  id = type_integer(),
  ward = type_factor(levels = c("ED", "ICU")),
  age = type_double(min = 18, max = 120),
  sbp = option(type_double(min = 40, max = 300)),
  admitted = type_date(),
  discharged = option(type_date())
))
r8read_table(admissions, Admissions)
<demo::Admissions table> 3 rows ⨉ 6 columns
        id  ward       age     sbp  admitted    discharged
   integer  factor  double  double  Date        Date
                              1 NA              1 NA
1        1  ED          40     128  2026-01-03  2026-01-07
2        2  ICU         52     141  2026-01-04  NA
3        3  ED          61      NA  2026-01-05  2026-01-09

The value is checked as a value of the type is at construction: a cell outside its range, and a row that does not satisfy a rule, are an error. A text that is not of its column’s type is collected with the other cells of that text and reported before the value is made (Texts that do not read).

A declared column reads every text of its meaning: +5, 007, and 40.0 are integers of a column declared type_integer().

A type_table() declares columns without a type name. A read against one gives a raven, checked against it:

Cases <- type_table(
  columns = list(id = type_integer(), age = type_double(min = 18)),
  open = TRUE
)
cases <- r8read_table(admissions, Cases)
class(cases)
[1] "R8::Raven" "r8_table"  "r8_value" 

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

Ids <- new_table_type("Ids", columns = list(id = type_integer()))
r8read_table(admissions, Ids)
Error:
! Invalid `demo::Ids`: 5 problems
- `ward` is not a column of the table, whose columns are `id`. [unknown_column]
- `age` is not a column of the table, whose columns are `id`. [unknown_column]
- `sbp` is not a column of the table, whose columns are `id`. [unknown_column]
- `admitted` is not a column of the table, whose columns are `id`. [unknown_column]
- `discharged` is not a column of the table, whose columns are `id`. [unknown_column]

15.3 Formats

The format is that of the file’s name: .csv, .tsv, and .parquet. A CSV or TSV file whose name ends in .gz is decompressed. format gives the format of another name, and delimiter the character between two cells:

semicolons <- file.path(dir, "wards.txt.gz")
con <- gzfile(semicolons, "w")
writeLines(c("ward;beds", "ED;24", "ICU;12"), con)
close(con)
r8read_table(semicolons, format = "csv", delimiter = ";")
<raven table> 2 rows ⨉ 2 columns
   ward          beds
   character  integer
1  ED              24
2  ICU             12

A CSV or TSV file is read as UTF-8, or as UTF-16 where it begins with the mark of UTF-16. encoding gives another encoding, "latin1" or "windows-1252". A byte that is not valid in the encoding is an error with its line:

names <- file.path(dir, "names.csv")
writeBin(c(charToRaw("id,name\n1,"), as.raw(0xe9), charToRaw("lise\n")), names)
r8read_table(names)
Error:
! `names.csv` does not read: byte 3 of line 2 is not valid in the encoding; `encoding` gives the encoding of the file, such as "latin1".
r8read_table(names, encoding = "latin1")
<raven table> 1 row ⨉ 2 columns
        id  name
   integer  character
1        1  élise

delimiter and encoding are arguments of CSV and TSV files alone; given for a Parquet file, each is an error.

15.4 Missing cells

A cell is missing where its text is one of na_strings, exactly: c("", "NA") by default. In a column of text, a quoted "" is an empty text, and a quoted "NA" is the text NA:

notes <- file.path(dir, "notes.csv")
writeLines(c("id,note", "1,", "2,\"\"", "3,NA", "4,\"NA\"", "5,seen"), notes)
r8read_table(notes)$note
[1] NA     ""     NA     "NA"   "seen"

na_strings_in adds texts of missing cells by column, which Texts that do not read describes with the decisions it belongs to.

15.5 Malformed files

A quote that is not closed, characters after a closing quote, a name that repeats in the header or is missing, and bytes that are no text in encoding are an error with the line:

quoted <- file.path(dir, "quoted.csv")
writeLines(c("id,ward", "1,\"ED", "2,ICU"), quoted)
r8read_table(quoted)
Error:
! `quoted.csv` does not read: the quote that begins a cell in line 2 is not closed.

A line with another number of cells than the header is an error of r8read_table(). r8check_file() leaves such lines out, checks the others, and lists them in $lines:

short <- file.path(dir, "short.csv")
writeLines(c("id,ward", "1,ED", "2", "3,ICU,beds"), short)
r8read_table(short)
Error:
! `short.csv` has 2 lines with another number of cells than the 2 of the header: line 3 has 1, line 4 has 3.
Wards <- new_table_type("Wards", columns = list(id = type_integer()), open = TRUE)
r8check_file(short, Wards)
The check holds on 1 row
  id  holds
2 lines with another number of cells than the header are left out ($lines)
© 2026 E.D. Gennatas