Skip to content

Commit

Permalink
fantastic as_is
Browse files Browse the repository at this point in the history
  • Loading branch information
Melkiades committed Jan 29, 2024
1 parent c760e99 commit 8e27408
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 14 deletions.
8 changes: 7 additions & 1 deletion R/tt_export.R
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,14 @@ result_df_v0_experimental <- function(tt,

# Using only labels for row names and losing information about paths
if (as_is) {
rownames(ret) <- ret$label_name
tmp_rownames <- ret$label_name
ret <- ret[, -seq_len(which(colnames(ret) == "node_class"))]
if (length(unique(tmp_rownames)) == length(tmp_rownames)) {
rownames(ret) <- tmp_rownames
} else {
ret <- cbind("label_name" = tmp_rownames, ret)
rownames(ret) <- NULL
}
} else {
rownames(ret) <- NULL
}
Expand Down
30 changes: 24 additions & 6 deletions R/tt_from_df.R
Original file line number Diff line number Diff line change
@@ -1,23 +1,41 @@
#' Create `ElementaryTable` from data.frame
#'
#'
#' @param df data.frame.
#'
#'
#' @return an \code{ElementaryTable} object with unnested columns corresponding to
#' \code{names(df)} and row labels corresponding to \code{row.names(df)}
#'
#' \code{names(df)} and row labels corresponding to \code{row.names(df)}.
#'
#' @details
#' If row names are not defined in `df` (or they are simple numbers), then the
#' row names are taken from the column `label_name`, if exists. If `label_name` exists,
#' then it is also removed from the original data. Remember that this behavior is
#' compatible with [as_result_df()], when `as_is = TRUE` and the row names are not unique.
#'
#' @seealso [as_result_df()] for the inverse operation.
#'
#' @examples
#' df_to_tt(mtcars)
#'
#'
#' @export
df_to_tt <- function(df) {
colnms <- colnames(df)
cinfo <- manual_cols(colnms)
rnames <- rownames(df)
havern <- !is.null(rnames)

if ((!havern || all(grepl("[0-9]+", rnames))) &&
"label_name" %in% colnms) {
rnames <- df$label_name
df <- df[, -match("label_name", colnms)]
colnms <- colnames(df)
cinfo <- manual_cols(colnms)
havern <- TRUE
}

kids <- lapply(seq_len(nrow(df)), function(i) {
rni <- if (havern) rnames[i] else ""
do.call(rrow, c(list(row.name = rni), unclass(df[i, ])))
})

ElementaryTable(kids = kids, cinfo = cinfo)
}
11 changes: 10 additions & 1 deletion man/df_to_tt.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 19 additions & 6 deletions tests/testthat/test-result_data_frame.R
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,25 @@ test_that("as_result_df as_is is producing a data.frame that is compatible with
lyt <- make_big_lyt()
tbl <- build_table(lyt, rawdat)

# ard_out <- as_result_df(tbl, as_is = TRUE)
# mf_tbl <- matrix_form(tbl)
ard_out <- as_result_df(tbl, as_is = TRUE)
mf_tbl <- matrix_form(tbl)

# Label works
# expect_identical(
# ard_out$label_name,
# mf_strings(mf_tbl)[-seq_len(mf_nrheader(mf_tbl)), 1]
# )
expect_identical(
ard_out$label_name,
mf_strings(mf_tbl)[-seq_len(mf_nrheader(mf_tbl)), 1]
)

expect_identical(
ard_out$label_name,
df_to_tt(ard_out) %>% row.names()
)

init_tbl <- df_to_tt(mtcars)
end_tbl <- init_tbl %>% as_result_df(as_is = TRUE) %>% df_to_tt()

expect_equal(
matrix_form(init_tbl)$strings,
matrix_form(end_tbl)$strings
)
})

0 comments on commit 8e27408

Please sign in to comment.