You can accomplish what you want with the withCallingHandlers
function, like so
withCallingHandlers({
for(i in 1:nrow(dat)){
w <- length(warnings())
dat$logk[i] <- log(dat$k[i])
}
}, warning = function(w){
dat$warnMsg[i] <<- w$message
invokeRestart("muffleWarning")
})
Notice that you need the <<-
operator in the warning handler to assign the message to the data frame in the global environment, since <-
would create a local copy of dat
within the handler and discard it upon exit.