Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #115 from NIFU-NO/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
sda030 authored Aug 24, 2024
2 parents dc38761 + ffedc40 commit ca05cb1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
8 changes: 6 additions & 2 deletions R/global_settings.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ global_settings_get <- function(fn_name = "makeme") {
#' @param fn_name String, one of `"make_link"`, `"fig_height_h_barchart"` and `"makeme"`.
#' @param new List of arguments (see `?make_link()`, `?makeme()`, `fig_height_h_barchart()`)
#' @param quiet Flag. If `FALSE` (default), informs about what has been set.
#' @param null_deletes Flag. If `FALSE` (default), `NULL` elements in `new`
#' become `NULL` elements in the option. Otherwise, the corresponding element,
#' if present, is deleted from the option.
#'
#' @return Invisibly returned list of old and new values.
#' @export
#'
#' @examples global_settings_set(new=list(digits=2))
global_settings_set <- function(new, fn_name = "makeme", quiet=FALSE) {
global_settings_set <- function(new, fn_name = "makeme", quiet = FALSE, null_deletes = FALSE) {
saros_options <- getOption("saros", list())
current_options <- saros_options[[paste0(fn_name, "_defaults")]]
updated_options <- utils::modifyList(current_options, new)
updated_options <- utils::modifyList(current_options, new, keep.null = !null_deletes)
saros_options[[paste0(fn_name, "_defaults")]] <- updated_options
options(saros = saros_options)
if(isFALSE(quiet)) {
Expand Down
14 changes: 10 additions & 4 deletions R/make_content.cat_table_html.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ make_content.cat_table_html <-
label_separator = dots$label_separator,
warn_multiple = TRUE)
indep_label <- unique(indep_label)
if(nchar(indep_label)==0) browser() #cli::cli_warn("Indep {.var {indep_pos}} lacks a label.")
if(nchar(indep_label)==0) indep_label <- dots$indep[1] #browser() #cli::cli_warn("Indep {.var {indep_pos}} lacks a label.")

} else indep_label <- character(0)

# indep_label <- unname(get_raw_labels(data = dots$data, col_pos = dots$indep))


cat_lvls <- levels(data_summary[[".category"]])
cat_lvls[is.na(cat_lvls)] <- "NA"

if(length(indep_label)==1 && length(dots$indep)==0) {
cli::cli_abort("Something wrong in function.")
}
Expand All @@ -39,8 +40,13 @@ make_content.cat_table_html <-
dplyr::arrange(as.integer(.data[[".variable_label"]]),
if(length(dots$indep)>0) as.integer(.data[[dots$indep]])) |>
tidyr::pivot_wider(id_cols = tidyselect::all_of(c(".variable_label", dots$indep, ".count_total")),
names_from = ".category", values_from = ".data_label") |>
dplyr::relocate(tidyselect::all_of(c(".variable_label", dots$indep, cat_lvls, ".count_total")), .after = 1) |>
names_from = ".category", values_from = ".data_label")
names(data_out)[names(data_out) == "NA"] <- "NA"
new_col_order <-
c(".variable_label", dots$indep, cat_lvls, ".count_total")
data_out <-
data_out |>
dplyr::relocate(tidyselect::all_of(new_col_order), .after = 1) |>
dplyr::rename_with(.cols = tidyselect::all_of(cat_lvls),
.fn = ~stringi::stri_c(ignore_null=TRUE, .x, if(dots$data_label %in% c("percentage", "percentage_bare")) " (%)")) |>
dplyr::rename_with(.cols = ".count_total",
Expand Down
11 changes: 10 additions & 1 deletion man/global_settings_set.Rd

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

15 changes: 15 additions & 0 deletions tests/testthat/test-make_content.cat_table_html.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,18 @@ testthat::test_that("make_content.cat_table_html works", {
add_n_to_label = TRUE)
testthat::expect_equal(as.character(result$.variable_label[[4]]), "Blue Party (N = 266)")
})

testthat::test_that("make_content.cat_table_html works with NA on both dep and indep", {

expected_df <-
tibble::tibble(b=factor(c("Z", NA), exclude = NULL),
`F (%)` = c(NA, "50"),
`M (%)` = c(NA, "50"),
`NA (%)` = c("100", NA),
`Total (N)` = c(1L, 2L))
attr(expected_df$b, "label") <- NA_character_
data.frame(a=factor(c("M", "F", NA), exclude = NULL),
b=factor(c(NA, NA, "Z"), exclude = NULL)) |>
saros.contents::makeme(dep=a, indep=b, showNA = "never", type="cat_table_html") |>
testthat::expect_equal(expected = expected_df)
})

0 comments on commit ca05cb1

Please sign in to comment.