Skip to main content

16  Texts that do not read

A file of laboratory results holds texts that are not numbers in columns of numbers: a value below the limit of detection, a test not yet resulted, a sample that could not be measured. R8.io decides each such text once, by column, with the user: what it reads as, or that it is missing. The examples use one file and its type:

library(R8.io)
dir <- file.path(tempdir(), "files")
dir.create(dir, showWarnings = FALSE)
labs <- file.path(dir, "labs.csv")
writeLines(c(
  "id,age,glucose,crp",
  "1,40,5.4,<0.3",
  "2,52,pending,1.2",
  "3,61,6.1,<0.3",
  "4,47,,<0.3",
  "5,130,7.2,hemolyzed",
  "6,58,?,4.8"
), labs)
Labs <- new_table_type("Labs", columns = list(
  id = type_integer(),
  age = type_double(min = 18, max = 120),
  glucose = option(type_double(min = 0)),
  crp = option(type_double(min = 0))
))

16.1 Reading requires

r8read_table() gives a value of the type, or an error. A text that does not read is listed once for its column, with the number of its cells and its first row, and the message ends with the call that completes the decisions:

r8read_table(labs, Labs)
Error:
! `labs.csv` does not read as `demo::Labs`: 6 cells of 6 rows
  glucose  "pending"    1 cell, first in row 2
  glucose  "?"          1 cell, first in row 6
  crp      "<0.3"       3 cells, first in row 1
  crp      "hemolyzed"  1 cell, first in row 5
`recode_in` gives what each text reads as, and `na_strings_in` those that are missing:
  recode_in = list(glucose = c("pending" = , "?" = ), crp = c("<0.3" = , "hemolyzed" = ))
r8check_file(labs, Labs) gives a report.

The three cells of <0.3 are one decision. The error is of class r8_value_error and holds the report of the file in its field report.

16.2 Checking looks

r8check_file(file, against) reads the same file and reports what it finds, without an error. Its value is an R8io::FileReport, which extends the Report of r8check() (Checking data):

report <- r8check_file(labs, Labs)
report
1 of 4 checks of demo::Labs holds on 6 rows
  id       holds
  age      1 row fails (row 5)
  glucose  2 rows fail (rows 2, 6)
  crp      4 rows fail (rows 1, 3, 4, ...)
  $problems lists 7 problems
Texts that do not read ($unread)
  glucose  "pending"    1 cell, first in row 2
  glucose  "?"          1 cell, first in row 6
  crp      "<0.3"       3 cells, first in row 1
  crp      "hemolyzed"  1 cell, first in row 5

$unread is the table of the texts that do not read, with the number of their cells and the first row of each:

report$unread
<R8io::Unread table> 4 rows ⨉ 4 columns
   column     text         cells  first_row
   character  character  integer    integer
1  glucose    pending          1          2
2  glucose    ?                1          6
3  crp        <0.3             3          1
4  crp        hemolyzed        1          5

A cell that does not read is a problem of the code unread in $problems. It is missing in the check of its column, so a rule has no answer in its row. The age of 130 is a cell that reads and is outside its column’s range, a problem of the type, as in r8check(). limit gives the problems listed for each column and rule, 1,000 by default.

16.3 What a text reads as

recode_in gives, by column, the value of each text: list(crp = c("<0.3" = 0.3)). A value below a limit of detection is not missing; what it reads as is a decision of the domain, which the analysis states. The value is of the column’s type.

16.4 Missing texts

na_strings_in gives, by column, further texts of missing cells: list(glucose = c("pending", "?")). They add to na_strings, which holds for every column. With both decisions, the report lists what each text read as in $recoded, and the problem of the type that remains:

decisions <- list(
  recode_in = list(crp = c("<0.3" = 0.3)),
  na_strings_in = list(glucose = c("pending", "?"), crp = "hemolyzed")
)
r8check_file(
  labs,
  Labs,
  recode_in = decisions$recode_in,
  na_strings_in = decisions$na_strings_in
)
3 of 4 checks of demo::Labs hold on 6 rows
  id       holds
  age      1 row fails (row 5)
  glucose  holds
  crp      holds
  $problems lists 1 problem
Texts that read as given values ($recoded)
  crp  "<0.3" as 0.3  3 cells
r8read_table(
  labs,
  Labs,
  recode_in = decisions$recode_in,
  na_strings_in = decisions$na_strings_in
)
Error:
! Invalid `demo::Labs`: 1 problem
- `age[[5]]` must be at most 120; found 130. [above_max]

The age of 130 is an error of the type, which a decision about texts does not change.

16.5 The next file

The decisions are a list that a script keeps beside the type and gives to the read of every file of the same source. A text that no decision names is an error again, so a new text is decided when it first appears:

labs_march <- file.path(dir, "labs-march.csv")
writeLines(c(
  "id,age,glucose,crp",
  "7,44,5.9,<0.3",
  "8,71,pending,<0.5"
), labs_march)
r8read_table(
  labs_march,
  Labs,
  recode_in = decisions$recode_in,
  na_strings_in = decisions$na_strings_in
)
Error:
! `labs-march.csv` does not read as `demo::Labs`: 1 cell of 2 rows
  crp  "<0.5"  1 cell, first in row 2
`recode_in` gives what each text reads as, and `na_strings_in` those that are missing:
  recode_in = list(crp = c("<0.5" = ))
r8check_file(labs_march, Labs) gives a report.

A decision that does not fit is an error before the file is read: a value that the column’s type does not admit, NA as a value, a text that is a missing code, a text that reads as the type, a column without a declared type, and a column that the file does not have:

r8read_table(labs, Labs, recode_in = list(crp = c("1.2" = 1)))
Error:
! `recode_in` gives a value for "1.2" of `crp`, which reads as it is.
© 2026 E.D. Gennatas