Skip to content

Commit

Permalink
update replace_null()
Browse files Browse the repository at this point in the history
  • Loading branch information
teunbrand committed Dec 8, 2023
1 parent 8470aa2 commit 4ee4cfb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
3 changes: 1 addition & 2 deletions R/guide-.R
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,7 @@ Guide <- ggproto(
key <- params$key

# Setup parameters and theme
params$position <- params$position %||% position
params$direction <- params$direction %||% direction
params <- replace_null(params, position = position, direction = direction)
params <- self$setup_params(params)
elems <- self$setup_elements(params, self$elements, theme)
elems <- self$override_elements(params, elems, theme)
Expand Down
10 changes: 9 additions & 1 deletion R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -773,9 +773,17 @@ vec_rbind0 <- function(..., .error_call = current_env(), .call = caller_env()) {
)
}

# This function is used to vectorise the following pattern:
#
# list$name1 <- list$name1 %||% value
# list$name2 <- list$name2 %||% value
#
# and express this pattern as:
#
# replace_null(list, name1 = value, name2 = value)
replace_null <- function(list, ..., env = caller_env()) {
# Collect dots without evaluating
dots <- match.call(replace_null, expand.dots = FALSE)$`...`
dots <- enexprs(...)
# Select arguments that are null in `list`
nms <- names(dots)
nms <- nms[vapply(list[nms], is.null, logical(1))]
Expand Down

0 comments on commit 4ee4cfb

Please sign in to comment.