Skip to content

Commit 137b19b

Browse files
committed
fixes
1 parent 2bdf0c7 commit 137b19b

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

R/tt_as_df.R

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,13 @@ as_result_df <- function(tt, spec = NULL,
9999
which_root_name = c("root", "rbind_root"),
100100
all = TRUE
101101
)
102+
103+
# Correcting maxlen for even number of paths (only multianalysis diff table names)
102104
maxlen <- max(lengths(df$path))
103-
105+
if (maxlen %% 2 != 0) {
106+
maxlen <- maxlen + 1
107+
}
108+
104109
# Loop for metadata (path and details from make_row_df)
105110
metadf <- do.call(
106111
rbind.data.frame,
@@ -174,6 +179,7 @@ as_result_df <- function(tt, spec = NULL,
174179
ret <- rbind(header_colnames_matrix, ret)
175180
}
176181

182+
# make_ard -----------------------------------------------------------------
177183
# ARD part for one stat per row
178184
if (make_ard) {
179185
cinfo_df <- col_info(tt)
@@ -238,11 +244,13 @@ as_result_df <- function(tt, spec = NULL,
238244
stat_name <- setNames(cell_stat_names[, col_i - min(only_col_indexes) + 1, drop = TRUE], NULL)
239245
stat <- setNames(ret_tmp[!col_label_rows, col_i, drop = TRUE], NULL)
240246
necessary_stat_lengths <- sapply(stat, length)
247+
stat[sapply(stat, is.null)] <- NA
241248

242249
# Truncating or adding NA if stat names has more or less elements than stats
243250
stat_name <- lapply(seq_along(stat_name), function(sn_i) {
244251
stat_name[[sn_i]][seq_len(necessary_stat_lengths[sn_i])]
245252
})
253+
stat_name[sapply(stat_name, function(x) length(x) == 0)] <- NA
246254

247255
# unnesting stat_name and stat
248256
tmp_ret_by_col_i <- NULL
@@ -322,7 +330,15 @@ as_result_df <- function(tt, spec = NULL,
322330
kids <- tree_children(ci_coltree)
323331
return(lapply(kids, .get_column_split_name))
324332
}
325-
sapply(pos_splits(tree_pos(ci_coltree)), spl_payload)
333+
334+
lapply(pos_splits(tree_pos(ci_coltree)), function(x) {
335+
pl <- spl_payload(x)
336+
if (!is.null(pl)) { # it is null when all obs (1 column)
337+
return(pl)
338+
} else {
339+
return(x@name)
340+
}
341+
})
326342
}
327343

328344
# Function that selects specific outputs from the result data frame
@@ -378,6 +394,9 @@ do_label_row <- function(rdfrow, maxlen) {
378394
# Special cases with hidden labels
379395
if (length(pth) %% 2 == 1) {
380396
extra_nas_from_splits <- extra_nas_from_splits + 1
397+
} else {
398+
pth <- c("<analysis_spl_tbl_name>", pth)
399+
extra_nas_from_splits <- extra_nas_from_splits - 1
381400
}
382401

383402
c(
@@ -415,15 +434,17 @@ do_content_row <- function(rdfrow, maxlen) {
415434
do_data_row <- function(rdfrow, maxlen) {
416435
pth <- rdfrow$path[[1]]
417436
pthlen <- length(pth)
418-
## odd means we have a multi-analsysis step in the path, we dont' want that in the result data frame
437+
## odd means we have a multi-analsysis step in the path, we do not want this in the result
419438
if (pthlen %% 2 == 1) {
420-
pth <- pth[-1 * (pthlen - 2)]
439+
# we remove the last element, as it is a fake split (tbl_name from analyse)
440+
# pth <- pth[-1 * (pthlen - 2)]
441+
pth <- c("<analysis_spl_tbl_name>", pth)
421442
}
422443
pthlen_new <- length(pth)
423-
if (maxlen == 1) pthlen_new <- 3
444+
if (maxlen == 1) pthlen_new <- 3 # why?
424445
c(
425446
as.list(pth[seq_len(pthlen_new - 2)]),
426-
replicate(maxlen - pthlen, list(NA_character_)),
447+
replicate(ifelse((maxlen - pthlen_new) > 0, maxlen - pthlen_new, 0), list(NA_character_)),
427448
as.list(tail(pth, 2)),
428449
list(
429450
label_name = rdfrow$label,

tests/testthat/test-result_data_frame.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,16 @@ test_that("as_result_df works fine with empty tables and no character(0) is allo
239239
)
240240
})
241241

242+
test_that("as_result_df works with only analyze tables (odd num of path elements)", {
243+
tbl <- basic_table() %>%
244+
analyze("cyl", table_names = "a") %>%
245+
analyze("mpg") %>%
246+
build_table(mtcars)
247+
248+
expect_equal(as_result_df(tbl)$group1[[1]], "<analysis_spl_tbl_name>")
249+
expect_equal(as_result_df(tbl, make_ard = TRUE)$group1[[1]], "<analysis_spl_tbl_name>")
250+
})
251+
242252
test_that("make_ard produces realistic ARD output with as_result_df", {
243253
# Testing fundamental getters/setters
244254
rc <- rcell(c(1, 2), stat_names = c("Rand1", "Rand2"))

0 commit comments

Comments
 (0)