R/coerce.R

Defines functions defer local_deprecation_user_env coerce_chr coerce_dbl coerce_int coerce_lgl coerce

# Used internally by map and flatten.
# Exposed here for testing
coerce <- function(x, type) {
  .Call(coerce_impl, x, type)
}

coerce_lgl <- function(x) coerce(x, "logical")
coerce_int <- function(x) coerce(x, "integer")
coerce_dbl <- function(x) coerce(x, "double")
coerce_chr <- function(x) coerce(x, "character")

# Can rewrite after https://github.com/r-lib/rlang/issues/1643
local_deprecation_user_env <- function(
  user_env = caller_env(2),
  frame = caller_env()
) {
  old <- the$deprecation_user_env
  the$deprecation_user_env <- user_env
  defer(the$deprecation_user_env <- old, frame)
}

# Lightweight equivalent of withr::defer()
defer <- function(expr, env = caller_env(), after = FALSE) {
  thunk <- as.call(list(function() expr))
  do.call(on.exit, list(thunk, TRUE, after), envir = env)
}

Try the purrr package in your browser

Any scripts or data that you put into this service are public.

purrr documentation built on Nov. 5, 2025, 7:27 p.m.