Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Melkiades committed Nov 18, 2024
1 parent f122af0 commit 05bfdaa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
16 changes: 10 additions & 6 deletions R/tt_as_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,11 @@ as_result_df <- function(tt, spec = NULL,

# Moving colnames to rows (flattening)
ret_w_cols <- NULL
# col_i <- only_col_indexes[1]
for (col_i in only_col_indexes) {
tmp_ret_by_col_i <- cbind(
group1 = column_split_names[[ret_tmp[, col_i][[1]]]],
group1_level = ret_tmp[, col_i][[1]],
# instead of avar_name row_name label_name
# instead of avar_name row_name label_name ("variable_label" is not present in ARDs)
setNames(core_row_names, c("variable", "variable_level", "variable_label")), # missing stat_name xxx
stat = I(setNames(ret_tmp[!col_label_rows, col_i], NULL))
)
Expand All @@ -204,11 +203,16 @@ as_result_df <- function(tt, spec = NULL,
ret <- ret_w_cols
}

# Simplify the result data frame
out <- if (simplify) {
.simplify_result_df(ret)
} else {
ret
}

Check warning on line 212 in R/tt_as_df.R

View workflow job for this annotation

GitHub Actions / SuperLinter 🦸‍♀️ / Lint R code 🧶

file=R/tt_as_df.R,line=212,col=1,[trailing_whitespace_linter] Trailing whitespace is superfluous.
# take out rownames
rownames(out) <- NULL

Check warning on line 215 in R/tt_as_df.R

View workflow job for this annotation

GitHub Actions / SuperLinter 🦸‍♀️ / Lint R code 🧶

file=R/tt_as_df.R,line=215,col=1,[trailing_whitespace_linter] Trailing whitespace is superfluous.
} else {
# Applying specs
out <- spec(tt, ...)
Expand All @@ -230,13 +234,13 @@ as_result_df <- function(tt, spec = NULL,
# Function that selects specific outputs from the result data frame
.simplify_result_df <- function(df) {
col_df <- colnames(df)
if (!all(c("row_name", "node_class") %in% col_df)) {
stop("Please simplify the result data frame only when it has 'row_name' and 'node_class' columns.")
if (!all(c("label_name", "node_class") %in% col_df)) {
stop("Please simplify the result data frame only when it has 'label_name' and 'node_class' columns.")
}
row_names_col <- which(col_df == "label_name")
label_names_col <- which(col_df == "label_name")
result_cols <- seq(which(col_df == "node_class") + 1, length(col_df))

df[, c(row_names_col, result_cols)]
df[, c(label_names_col, result_cols)]
}

.remove_empty_elements <- function(char_df) {
Expand Down
8 changes: 5 additions & 3 deletions tests/testthat/test-result_data_frame.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,17 @@ test_that("as_result_df works with visual output (data_format as numeric)", {

mf <- matrix_form(tbl)
string_tbl <- mf_strings(mf)[-seq_len(mf_nlheader(mf)), ]
string_tbl <- string_tbl[nzchar(string_tbl[, 2]), ]
string_tbl <- as.data.frame(string_tbl[nzchar(string_tbl[, 2]), ])
colnames(string_tbl) <- colnames(res)
expect_equal(res, data.frame(string_tbl))
rownames(string_tbl) <- NULL
expect_equal(res, string_tbl)

res <- expect_silent(as_result_df(tbl, simplify = TRUE, data_format = "strings", expand_colnames = TRUE))
string_tbl <- mf_strings(mf)
string_tbl <- data.frame(string_tbl[nzchar(string_tbl[, 2]), ])
colnames(string_tbl) <- colnames(res)
string_tbl$row_name[seq_len(mf_nlheader(mf))] <- res$row_name[seq_len(mf_nlheader(mf))]
string_tbl$label_name[seq_len(mf_nlheader(mf))] <- res$label_name[seq_len(mf_nlheader(mf))]
rownames(string_tbl) <- NULL
expect_equal(res, string_tbl)

expect_silent(basic_table() %>% build_table(DM) %>% as_result_df())
Expand Down

0 comments on commit 05bfdaa

Please sign in to comment.