From 88492d7688387c2c5fb0f23297324a4e49f9f9f9 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 24 Jan 2022 00:31:03 +0100 Subject: [PATCH] allow names list for "at" (see https://github.com/easystats/datawizard/issues/44) --- R/visualisation_matrix.data.frame.R | 66 ++++++++++++++++++----------- man/visualisation_matrix.Rd | 5 ++- 2 files changed, 46 insertions(+), 25 deletions(-) diff --git a/R/visualisation_matrix.data.frame.R b/R/visualisation_matrix.data.frame.R index 7af806d6..694c79a7 100644 --- a/R/visualisation_matrix.data.frame.R +++ b/R/visualisation_matrix.data.frame.R @@ -3,7 +3,7 @@ #' Create a reference matrix, useful for visualisation, with evenly spread and combined values. `data_matrix()` is an alternative name for `visualisation_matrix()`. #' #' @param x An object from which to construct the reference grid. -#' @param at,target Can be "all" or list of characters indicating columns of interest. Can also contain assignments (e.g., `at = "Sepal.Length = 2"` or `at = c("Sepal.Length = 2", "Species = 'setosa'")` - note the usage of single and double quotes to assign strings within strings). The remaining variables will be fixed. (`target` is the deprecated name of that argument). +#' @param at,target Can be "all" or list of characters indicating columns of interest. Can also contain assignments (as named list, e.g. `at = list(c(Sepal.Length = c(2, 4), Species = "setosa"))`, or as string, e.g. `at = "Sepal.Length = 2"` or `at = c("Sepal.Length = 2", "Species = 'setosa'")` - note the usage of single and double quotes to assign strings within strings). The remaining variables will be fixed. (`target` is the deprecated name of that argument). #' @param length Length of numeric "at" variables. #' @param range Can be one of `c("range", "iqr", "ci", "hdi", "eti")`. If `"range"` (default), will use the min and max of the original vector as end-points. If any other interval, will spread within the range (the default CI width is `95%` but this can be changed by setting something else, e.g., `ci = 0.90`). See [IQR()] and [bayestestR::ci()]. #' @param factors Type of summary for factors. Can be "reference" (set at the reference level), "mode" (set at the most common level) or "all" to keep all levels. @@ -36,6 +36,9 @@ #' visualisation_matrix(data, at = c("Sepal.Length = 3", "Species")) #' visualisation_matrix(data, at = c("Sepal.Length = c(3, 1)", "Species = 'setosa'")) #' +#' # with list-style at-argument +#' visualisation_matrix(data, at = list(Sepal.Length = c(1, 3), Species = "setosa")) +#' #' # Standardize #' vizdata <- visualisation_matrix(data, at = "Sepal.Length") #' effectsize::standardize(vizdata) @@ -62,8 +65,9 @@ visualisation_matrix.data.frame <- function(x, at = "all", target = NULL, factor # find numerics that were coerced to factor in-formula numeric_factors <- colnames(x)[sapply(x, function(i) isTRUE(attributes(i)$factor))] + specs <- NULL + if (is.null(target)) { - specs <- NULL targets <- data.frame() } else { # Valid target argument @@ -75,14 +79,8 @@ visualisation_matrix.data.frame <- function(x, at = "all", target = NULL, factor target <- names(x)[target] } - # Deal with targets ========================================================== - - # Find eventual user-defined specifications for each target - specs <- do.call(rbind, lapply(target, .visualisation_matrix_clean_target, x = x)) - specs$varname <- as.character(specs$varname) # make sure it's a string not fac - specs <- specs[!duplicated(specs$varname), ] # Drop duplicates + # Deal with factor in-formula transformations ============================ - # Deal with factor transformations x[] <- lapply(x, function(i) { if (isTRUE(attributes(i)$factor)) { as.factor(i) @@ -91,23 +89,43 @@ visualisation_matrix.data.frame <- function(x, at = "all", target = NULL, factor } }) - specs$is_factor <- sapply(x[specs$varname], function(x) is.factor(x) || is.character(x)) - # Create target list of factors ----------------------------------------- - facs <- list() - for (fac in specs[specs$is_factor == TRUE, "varname"]) { - facs[[fac]] <- visualisation_matrix(x[[fac]], target = specs[specs$varname == fac, "expression"]) - } + # Deal with targets ========================================================== - # Create target list of numerics ---------------------------------------- - nums <- list() - for (num in specs[specs$is_factor == FALSE, "varname"]) { - nums[[num]] <- visualisation_matrix(x[[num]], - target = specs[specs$varname == num, "expression"], - reference = reference[[num]], - ... - ) + if (is.character(target)) { + + # Find eventual user-defined specifications for each target + specs <- do.call(rbind, lapply(target, .visualisation_matrix_clean_target, x = x)) + specs$varname <- as.character(specs$varname) # make sure it's a string not fac + specs <- specs[!duplicated(specs$varname), ] # Drop duplicates + + specs$is_factor <- sapply(x[specs$varname], function(x) is.factor(x) || is.character(x)) + + # Create target list of factors ----------------------------------------- + facs <- list() + for (fac in specs[specs$is_factor == TRUE, "varname"]) { + facs[[fac]] <- visualisation_matrix(x[[fac]], + target = specs[specs$varname == fac, "expression"]) + } + + # Create target list of numerics ---------------------------------------- + nums <- list() + for (num in specs[specs$is_factor == FALSE, "varname"]) { + nums[[num]] <- visualisation_matrix(x[[num]], + target = specs[specs$varname == num, "expression"], + reference = reference[[num]], + ... + ) + } + } else if (is.list(target)) { + + # we have a list as at-values + facs <- target[sapply(target, is.factor)] + nums <- target[sapply(target, is.numeric)] } - # Assemble the two + + # Assemble the two - the goal is to have two named lists, where variable + # names are the names of the list-elements: one list contains elements of + # numeric variables, the other one factors. targets <- expand.grid(c(nums, facs)) # Preserve range --------------------------------------------------------- diff --git a/man/visualisation_matrix.Rd b/man/visualisation_matrix.Rd index 72682cb0..808c8705 100644 --- a/man/visualisation_matrix.Rd +++ b/man/visualisation_matrix.Rd @@ -32,7 +32,7 @@ data_matrix(x, ...) \item{...}{Arguments passed to or from other methods (for instance, \code{length} or \code{range} to control the spread of numeric variables.).} -\item{at, target}{Can be "all" or list of characters indicating columns of interest. Can also contain assignments (e.g., \code{at = "Sepal.Length = 2"} or \code{at = c("Sepal.Length = 2", "Species = 'setosa'")} - note the usage of single and double quotes to assign strings within strings). The remaining variables will be fixed. (\code{target} is the deprecated name of that argument).} +\item{at, target}{Can be "all" or list of characters indicating columns of interest. Can also contain assignments (as named list, e.g. \code{at = list(c(Sepal.Length = c(2, 4), Species = "setosa"))}, or as string, e.g. \code{at = "Sepal.Length = 2"} or \code{at = c("Sepal.Length = 2", "Species = 'setosa'")} - note the usage of single and double quotes to assign strings within strings). The remaining variables will be fixed. (\code{target} is the deprecated name of that argument).} \item{factors}{Type of summary for factors. Can be "reference" (set at the reference level), "mode" (set at the most common level) or "all" to keep all levels.} @@ -72,6 +72,9 @@ visualisation_matrix(data, at = c("Sepal.Length", "Species"), numerics = 0) visualisation_matrix(data, at = c("Sepal.Length = 3", "Species")) visualisation_matrix(data, at = c("Sepal.Length = c(3, 1)", "Species = 'setosa'")) +# with list-style at-argument +visualisation_matrix(data, at = list(Sepal.Length = c(1, 3), Species = "setosa")) + # Standardize vizdata <- visualisation_matrix(data, at = "Sepal.Length") effectsize::standardize(vizdata)