Skip to content

Commit

Permalink
Merge pull request #234 from DavisVaughan/fix/case-when-matrix
Browse files Browse the repository at this point in the history
Convert to vectors before calling `case_when()`
  • Loading branch information
hughjonesd authored Dec 12, 2022
2 parents ead3ad9 + b8af40a commit 7c342c9
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions R/mapping-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ by_regex <- function(..., .grepl_args = list(), ignore_na = TRUE) {
#'
#' `by_colorspace()` can be used to set background, border or
#' text colors, visually differentiating high or low values.
#'
#'
#' @param ... Colors
#' @param range Numeric endpoints. If `NULL`, these are determined from the data.
#' @param na_color Color to return for `NA` values. Can be `NA` itself.
Expand Down Expand Up @@ -579,9 +579,13 @@ by_cases <- function (..., ignore_na = TRUE) {
case_fn <- function (ht, rows, cols, current) {
res <- current
myenv <- new.env()
assign(".", as.matrix(ht[rows, cols]), envir = myenv)
selection <- as.matrix(ht[rows, cols])
dim <- dim(selection)
selection <- as.vector(selection)
assign(".", selection, envir = myenv)
cases <- lapply(cases, stats::as.formula, env = myenv)
vals <- dplyr::case_when(!!! cases)
vals <- array(vals, dim = dim)
res[] <- vals
res <- maybe_ignore_na(res, current, ignore_na)
res
Expand Down

0 comments on commit 7c342c9

Please sign in to comment.