Skip to content

Commit 8536059

Browse files
committed
Improve and test extractObservationData_1()
1 parent 1a608d3 commit 8536059

File tree

2 files changed

+45
-22
lines changed

2 files changed

+45
-22
lines changed

R/extractObservationData_1.R

+13-15
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,33 @@ extractObservationData_1 <- function(
77
#header.info <- kwb.en13508.2::euCodedFileHeader()
88

99
# Create accessor function to header info fields
10-
header_field <- kwb.utils::createAccessor(header.info)
10+
fromHeader <- kwb.utils::createAccessor(header.info)
1111

1212
# Get information on the row numbers where the different blocks start
1313
indices <- getBlockIndices(eu_lines, dbg = dbg)
1414

1515
# Column separator
16-
sep <- header_field("separator")
16+
sep <- fromHeader("separator")
1717

1818
# Try to get the C-Block captions (if they are unique, otherwise rais error!)
1919
captions <- tryToGetUniqueCaptions(eu_lines[indices$C], sep)
2020

21+
tableHeader <- paste(captions, collapse = sep)
22+
rowsToRemove <- c(indices$A, indices$B, indices$B + 1L, indices$C, indices$Z)
23+
tableBody <- eu_lines[-rowsToRemove]
24+
2125
# Try to find the column types for the given captions
2226
colClasses <- getColClasses(codes = inspectionDataFieldCodes(), captions)
2327

2428
observations <- readObservationsFromCsvText(
25-
text = eu_lines[-c(indices$A, indices$B, indices$B + 1L, indices$C, indices$Z)],
29+
text = c(tableHeader, tableBody),
2630
sep = sep,
27-
dec = header_field("decimal"),
28-
quote = header_field("quote"),
29-
colClasses = unname(colClasses)
31+
dec = fromHeader("decimal"),
32+
quote = fromHeader("quote"),
33+
colClasses = colClasses,
34+
header = TRUE
3035
)
31-
32-
# Set the column names to the captions
33-
names(observations) <- if (identical(colClasses, NA)) {
34-
captions
35-
} else {
36-
captions[!sapply(colClasses, is.null)]
37-
}
38-
36+
3937
indices$B01 <- indices$B[grep("^#B01=", eu_lines[indices$B])]
4038

4139
# Try to generate a vector of inspection numbers assigning to each observation
@@ -143,7 +141,7 @@ getColClasses2 <- function(codes, as.text)
143141
readObservationsFromCsvText <- function(text, sep, dec, quote, colClasses, ...)
144142
{
145143
# If colClasses is specified, reduce it to the columns that actually occur
146-
if (! identical(colClasses, NA)) {
144+
if (!identical(colClasses, NA)) {
147145

148146
# Get the column names from the first line
149147
colNames <- strsplit(text[1L], sep)[[1L]]
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,41 @@
1-
#kwb.utils::assignPackageObjects("kwb.en13508.2")
1+
#library(testthat)
22
test_that("extractObservationData_1() works", {
33

4-
f <- kwb.en13508.2:::extractObservationData_1
4+
header.info <- kwb.en13508.2::euCodedFileHeader()
5+
6+
f <- function(eu_lines) {
7+
kwb.en13508.2:::extractObservationData_1(eu_lines, header.info, dbg = FALSE)
8+
}
59

610
expect_error(f())
711

8-
eu_lines <- c(
9-
"#C=A;B",
12+
result <- f(eu_lines = c(
13+
"#C=A;B",
1014
"1;2"
11-
)
15+
))
16+
expect_identical(result, data.frame(inspno = 1L, A = "1", B = "2"))
17+
18+
expect_error(f(eu_lines = c(
19+
"#C=A;B",
20+
"1;2",
21+
"#C=A;C",
22+
"2;4"
23+
)))
24+
25+
result <- f(eu_lines = c(
26+
"#B01=",
27+
"#C=A;B",
28+
"1;3",
29+
"#Z",
30+
"#B01=",
31+
"#C=A;B",
32+
"2;4"
33+
))
1234

13-
header.info <- euCodedFileHeader()
35+
expect_identical(result, data.frame(
36+
inspno = 1:2,
37+
A = as.character(1:2),
38+
B = as.character(3:4)
39+
))
1440

15-
expect_error(capture.output(f(eu_lines, header.info)))
1641
})

0 commit comments

Comments
 (0)