diff --git a/NEWS.md b/NEWS.md index 21a86d88..c45346fc 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # serocalculator (development version) +* created unit test for `df_to_array()` +* fixed `dplyr::select()` deprecation warning in `df_to_array()` * Generalized `get_()` methods from `pop_data`-specific to `default`. # serocalculator 1.2.0 diff --git a/R/df_to_array.R b/R/df_to_array.R index c693f5f7..c772d054 100644 --- a/R/df_to_array.R +++ b/R/df_to_array.R @@ -8,7 +8,7 @@ #' #' @keywords internal #' -df.to.array <- function( +df.to.array <- function( # nolint: object_name_linter df, dim_var_names, value_var_name = "value") { @@ -18,10 +18,14 @@ df.to.array <- function( #' Convert a data.frame (or tibble) into a multidimensional array #' -#' @param df a [data.frame()] (or [tibble::tibble()]) in long format (each row contains one value for the intended array) -#' @param dim_var_names a [character()] vector of variable names in `df`. All of these variables should be factors, or a warning will be produced. -#' @param value_var_name a [character()] variable containing a variable name from `df` which contains the values for the intended array. -#' @return an [array()] with dimensions defined by the variables in `df` listed in `dim_var_names` +#' @param df a [data.frame()] (or [tibble::tibble()]) in long format +#' (each row contains one value for the intended array) +#' @param dim_var_names a [character()] vector of variable names in `df`. +#' All of these variables should be factors, or a warning will be produced. +#' @param value_var_name a [character()] variable containing a variable name +#' from `df` which contains the values for the intended array. +#' @return an [array()] with dimensions defined by the variables in `df` +#' listed in `dim_var_names` #' #' @examples #' library(dplyr) @@ -33,7 +37,9 @@ df.to.array <- function( #' cols = c("Sepal.Length", "Sepal.Width", "Petal.Width", "Petal.Length") #' ) %>% #' mutate(parameter = factor(parameter, levels = unique(parameter))) -#' arr <- df %>% serocalculator:::df_to_array(dim_var_names = c("parameter", "Species")) +#' arr <- df %>% +#' serocalculator:::df_to_array( +#' dim_var_names = c("parameter", "Species")) #' ftable(arr[,,1:5]) #' @noRd df_to_array <- function( @@ -77,6 +83,6 @@ df_to_array <- function( ) df %>% - mutate(.by = all_of(dim_var_names), obs = 1:n()) %>% + mutate(.by = all_of(dim_var_names), obs = row_number()) %>% xtabs(formula = formula(xtabs_formula)) } diff --git a/tests/testthat/test-df_to_array.R b/tests/testthat/test-df_to_array.R index b081c588..39b760aa 100644 --- a/tests/testthat/test-df_to_array.R +++ b/tests/testthat/test-df_to_array.R @@ -4,9 +4,15 @@ test_that("df_to_array() produces consistent results", { df <- iris %>% tidyr::pivot_longer( names_to = "parameter", - cols = c("Sepal.Length", "Sepal.Width", "Petal.Width", "Petal.Length") + cols = c( + "Sepal.Length", + "Sepal.Width", + "Petal.Width", + "Petal.Length" + ) ) %>% mutate(parameter = factor(parameter, levels = unique(parameter))) - arr <- df %>% serocalculator:::df_to_array(dim_var_names = c("parameter", "Species")) + arr <- df %>% + serocalculator:::df_to_array(dim_var_names = c("parameter", "Species")) arr %>% expect_snapshot_value(style = "serialize") })