|
4 | 4 | #' @importFrom dplyr n_distinct |
5 | 5 | #' @importFrom stats na.omit |
6 | 6 | #' @keywords internal |
7 | | -check_args <- function(mc, dotArgs) { |
| 7 | +check_args <- function(mc, dotArgs, caller) { |
8 | 8 |
|
9 | | - caller <- sub(".+::","",as.character(sys.call(-1))[1]) |
10 | 9 | pf <- parent.frame() |
11 | 10 | errmsg <- character() |
12 | | - caller_orig <- caller |
13 | 11 |
|
14 | | - if (caller == "FUN") { |
| 12 | + if (mc[[1]] == "FUN" || mc$x == "dd[x, , drop = FALSE]") { |
15 | 13 | pf$flag_by <- TRUE |
16 | | - # When stby() was called, deduce caller from formals |
17 | | - if ("cumul" %in% names(pf)) |
18 | | - caller <- "freq" |
19 | | - else if ("transpose" %in% names(pf)) |
20 | | - caller <- "descr" |
21 | | - else if ("chisq" %in% names(pf)) |
22 | | - caller <- "ctable" |
23 | | - else if ("graph.col" %in% names(pf)) |
24 | | - caller <- "dfSummary" |
25 | 14 | } else { |
26 | 15 | pf$flag_by <- FALSE |
27 | 16 | } |
@@ -114,6 +103,32 @@ check_args <- function(mc, dotArgs) { |
114 | 103 | errmsg %+=% "'cumul' must be either TRUE or FALSE" |
115 | 104 | } |
116 | 105 |
|
| 106 | + if ("na.val" %in% names(mc) && !is.null(pf$na.val)) { |
| 107 | + varname <- pf$varname %||% "x" |
| 108 | + if (length(pf$na.val) > 1) { |
| 109 | + errmsg %+=% "'na.val' can only contain one value" |
| 110 | + } |
| 111 | + if (!is.factor(pf$x)) { |
| 112 | + if (isFALSE(st_options("freq.silent"))) |
| 113 | + message("'na.val' only applies to factors & will be ignored for ", |
| 114 | + varname) |
| 115 | + } |
| 116 | + if (!isTRUE(test_character(pf$na.val, any.missing = FALSE))) { |
| 117 | + errmsg %+=% "'na.val' must be character" |
| 118 | + } |
| 119 | + if (is.factor(pf$x)) { |
| 120 | + if (!pf$na.val %in% levels(pf$x)) { |
| 121 | + if (isFALSE(st_options("freq.silent"))) |
| 122 | + message(paste0("'", pf$na.val, "' is not a level of ", |
| 123 | + varname, " and will be ignored")) |
| 124 | + pf$na.val <- NULL |
| 125 | + } else if (anyNA(pf$x)) { |
| 126 | + errmsg %+=% paste(varname, "contains NA values; 'na.val' is only", |
| 127 | + "valid in the absence of actual NA values") |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
117 | 132 | if ("order" %in% names(mc)) { |
118 | 133 | order <- switch(tolower(substr(sub("[+-]", "", pf$order), 1, 1)), |
119 | 134 | d = "default", |
@@ -187,7 +202,7 @@ check_args <- function(mc, dotArgs) { |
187 | 202 | if (!identical(pf$weights, NA)) { |
188 | 203 | if (is.null(pf$weights)) { |
189 | 204 | errmsg %+=% "weights vector not found" |
190 | | - } else if (caller_orig != "FUN" && |
| 205 | + } else if (isFALSE(pf$flag_by) && |
191 | 206 | length(pf$weights) != nrow(as.data.frame(pf$x))) { |
192 | 207 | errmsg %+=% "weights vector must have same length as 'x'" |
193 | 208 | } |
@@ -288,18 +303,16 @@ check_args <- function(mc, dotArgs) { |
288 | 303 | } |
289 | 304 | } else { |
290 | 305 | # order has length > 1 -- all elements must correspond to column names |
291 | | - if (length(ind <- which(!pf$order %in% colnames(pf$x.df))) > 0) { |
| 306 | + if (length(ind <- which(!pf$order %in% colnames(pf$xx))) > 0) { |
292 | 307 | errmsg %+=% paste("Following ordering element(s) not recognized:", |
293 | 308 | paste(pf$order[ind], sep = ", "), |
294 | 309 | collapse = " ") |
295 | 310 | } |
296 | 311 | } |
297 | 312 | } |
298 | 313 |
|
299 | | - if (!identical(pf$weights, NA)) { |
300 | | - if (is.null(pf$weights)) { |
301 | | - errmsg %+=% "weights vector not found" |
302 | | - } else if (caller_orig != "FUN" && (length(pf$weights) != nrow(pf$x.df))) { |
| 314 | + if (!is.null(pf$weights)) { |
| 315 | + if (isFALSE(pf$flag_by) && (length(pf$weights) != nrow(pf$xx))) { |
303 | 316 | errmsg %+=% "weights vector must have same length as 'x'" |
304 | 317 | } |
305 | 318 | } |
@@ -449,14 +462,6 @@ check_args_print <- function(mc) { |
449 | 462 | errmsg %+=% "'file' path is not valid - check that directory exists" |
450 | 463 | } |
451 | 464 |
|
452 | | - # # Change method to browser when file name was (most likely) provided by user |
453 | | - # if (grepl("\\.html$", pf$file, ignore.case = TRUE, perl = TRUE) && |
454 | | - # !grepl(pattern = tempdir(), x = pf$file, fixed = TRUE) && |
455 | | - # pf$method == "pander") { |
456 | | - # pf$method <- "browser" |
457 | | - # message("Switching method to 'browser'") |
458 | | - # } |
459 | | - # |
460 | 465 | if (pf$method == "pander" && !is.na(pf$table.classes)) { |
461 | 466 | errmsg %+=% "'table.classes' option does not apply to method 'pander'" |
462 | 467 | } |
@@ -613,22 +618,23 @@ check_args_st_options <- function(mc) { |
613 | 618 | errmsg %+=% "'ctable.totals' must be either TRUE or FALSE" |
614 | 619 | } |
615 | 620 |
|
616 | | - if ("descr_stats" %in% names(mc)) { |
617 | | - valid_stats <- c("mean", "sd", "min", "q1", "med", "q3","max", "mad", |
618 | | - "iqr", "cv", "skewness", "se.skewness", "kurtosis", |
619 | | - "n.valid", "pct.valid") |
| 621 | + if ("descr.stats" %in% names(mc)) { |
620 | 622 |
|
621 | | - if (length(pf$descr_stats) == 1 && |
622 | | - !(pf$descr_stats %in% c("fivevnum", "common")) && |
623 | | - !(pf$descr_stats %in% valid_stats)) { |
| 623 | + # Check for invalid items |
| 624 | + stats <- tolower(pf$descr.stats) |
| 625 | + invalid_stats <- setdiff( |
| 626 | + stats, c(.st_env$descr.stats.valid$no_wgts, |
| 627 | + paste0("-", .st_env$descr.stats.valid$no_wgts), |
| 628 | + "all", "common", "fivenum")) |
| 629 | + |
| 630 | + if (length(invalid_stats) > 0) { |
624 | 631 | errmsg %+=% |
625 | | - paste("'descr_stats' value", dQuote(pf$descr_stats), "not recognized;", |
626 | | - "allowed values are: ", |
627 | | - paste('"fivenum", "common", or a combination of :', |
628 | | - paste0(dQuote(valid_stats), sep = ", "))) |
| 632 | + paste("descr.stats: values", |
| 633 | + paste(dQuote(invalid_stats), collapse = ", "), |
| 634 | + "not recognized; see ?descr") |
629 | 635 | } |
630 | 636 | } |
631 | | - |
| 637 | + |
632 | 638 | if ("descr.transpose" %in% names(mc) && |
633 | 639 | !isTRUE(test_logical(pf$descr.transpose, len = 1, any.missing = FALSE))) { |
634 | 640 | errmsg %+=% "'descr.transpose' must be either TRUE or FALSE" |
|
0 commit comments