You don't have to use <<-, you can return the warning and test it after.
In the code below I have also changed the error trapping code to return the error.
library(tibble)
# Dummy data
data <- tibble(c(5, 6), c("a", NA_real_), c(NA_real_, NA_real_))
# Initialise output variables
result <- numeric(ncol(data))
warnings <- vector("list", ncol(data))
for(i in 1:3){
temp <- tryCatch(
max(data[,i], na.rm = TRUE),
error = function(e) e,
warning = function(w) w
)
if(inherits(temp, "warning")) {
warnings[[i]] <- conditionMessage(temp)
temp <- NaN
}
if(inherits(temp, "error")) {
warnings[[i]] <- conditionMessage(temp)
temp <- NA
}
result[i] <- temp
}
result
#> [1] 6 NA NaN
warnings
#> [[1]]
#> NULL
#>
#> [[2]]
#> [1] "only defined on a data frame with all numeric-alike variables"
#>
#> [[3]]
#> [1] "no non-missing arguments to max; returning -Inf"
Created on 2025-10-28 with reprex v2.1.1
Edit
In the code that follows, I solve the problem the OP raises in comment
But how do I change the line temp <- NaN to instead return the original output of max(data[,i], na.rm = TRUE), in this case -Inf? So warnings would be the same, but result would look like c(6, NA, -Inf). Is there some other function like conditionMessage to get this?
with a function in this SO post.
#
# https://stackoverflow.com/a/32076859/8245406
#
tryCatch.W.E <- function(expr)
{
W <- NULL
w.handler <- function(w){ # warning handler
W <<- w
invokeRestart("muffleWarning")
}
list(value = withCallingHandlers(tryCatch(expr, error = function(e) e),
warning = w.handler),
warning = W)
}
library(tibble)
# Dummy data
data <- tibble(c(5, 6), c("a", NA_real_), c(NA_real_, NA_real_))
# Initialise output variables
result <- numeric(ncol(data))
warnings <- vector("list", ncol(data))
for(i in 1:ncol(data)){
temp <- tryCatch.W.E(
max(data[, i], na.rm = TRUE)
)
if(inherits(temp$warning, c("warning", "error"))) {
warnings[[i]] <- conditionMessage(temp$warning)
}
if(inherits(temp$value, c("warning", "error"))) {
warnings[[i]] <- conditionMessage(temp$value)
result[i] <- NA # or NaN
} else result[i] <- temp$value
}
result
#> [1] 6 NA -Inf
warnings
#> [[1]]
#> NULL
#>
#> [[2]]
#> [1] "only defined on a data frame with all numeric-alike variables"
#>
#> [[3]]
#> [1] "no non-missing arguments to max; returning -Inf"
Created on 2025-10-29 with reprex v2.1.1
-Inffrommax(c(NA, NA), na.rm = TRUE)toresultwhile returning the warning message towarnings(i.e. I don't want to hardcode the result ofNaNstructure(NaN, warning = w)instead. and you shouldn't "grow" objects likeresult <- c(result, temp)especially since you are mixing types. instead pre-allocateresultlike you did forwarningsand use a list