library(R8.io)
dir <- file.path(tempdir(), "files")
dir.create(dir, showWarnings = FALSE)
Visits <- new_table_type("Visits", columns = list(
id = type_integer(),
ward = type_factor(levels = c("ED", "ICU")),
sbp = option(type_double()),
note = option(type_character()),
admitted = type_date(),
seen = type_posixct()
))
visits <- Visits(
id = 1:3,
ward = factor(c("ED", "ICU", "ED"), levels = c("ED", "ICU")),
sbp = c(128.5, NA, 141),
note = c("a, b", "", NA),
admitted = as.Date(c("2026-01-03", "2026-01-04", "2026-01-05")),
seen = as.POSIXct(
c("2026-01-03 10:00:00", "2026-01-04 11:30:00", "2026-01-05 09:15:30"),
tz = "UTC"
)
)18 Writing a file
r8write_table(x, file) writes the columns of a table, or of a data frame with columns that a table takes, as a CSV, TSV, or Parquet file, by the extension of file or by format. r8read_table() with the type of the table reads the same values back. Units and descriptions are not written.
18.1 CSV and TSV
A CSV or TSV file is UTF-8. A missing cell is empty. A text is quoted where it holds the delimiter, a quote, or the end of a line, and where it is empty or NA, so that it reads back as a text. 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 with microseconds. The cell of a factor is its label:
csv <- file.path(dir, "visits.csv")
r8write_table(visits, csv)
cat(readLines(csv), sep = "\n")id,ward,sbp,note,admitted,seen
1,ED,128.5,"a, b",2026-01-03,2026-01-03T10:00:00.000000Z
2,ICU,,"",2026-01-04,2026-01-04T11:30:00.000000Z
3,ED,141,,2026-01-05,2026-01-05T09:15:30.000000Z
r8equal(r8read_table(csv, Visits), visits)[1] TRUE
delimiter gives another character between two cells, as it does for a read.
18.2 Parquet
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 is a value. Every column is optional, and the pages are compressed with snappy:
parquet <- file.path(dir, "visits.parquet")
r8write_table(visits, parquet)
r8equal(r8read_table(parquet, Visits), visits)[1] TRUE
Other readers of Parquet read the file as R8.io wrote it:
nanoparquet::read_parquet(parquet) id ward sbp note admitted seen
1 1 ED 128.5 a, b 2026-01-03 2026-01-03 10:00:00
2 2 ICU NA 2026-01-04 2026-01-04 11:30:00
3 3 ED 141.0 <NA> 2026-01-05 2026-01-05 09:15:30
Without the type, a read gives a raven, whose factor column is text, the type that the file stores (Parquet and time zones):
r8read_table(parquet)<raven table> 3 rows ⨉ 6 columns id ward sbp note admitted seen integer character double character Date POSIXct 1 NA 1 NA 1 1 ED 128.5 a, b 2026-01-03 2026-01-03 10:00:00 UTC 2 2 ICU NA 2026-01-04 2026-01-04 11:30:00 UTC 3 3 ED 141.0 NA 2026-01-05 2026-01-05 09:15:30 UTC