Brief explanation

Workflow

library(tidyverse)
test_function <- function(x){
  tryCatch(
    log(x),                           # your function body 
    error = function(c){return(NA)}   # action you want to see if an error occurs 
  )
}
test_list <- list(5, "a", -1, 0, NA)
output <- for (i in 1:5){
  log(test_list[[i]])
}
output 
# Error in log(test_list[[i]]) : 
#   non-numeric argument to mathematical function
map(test_list, log)
# Error in .Primitive("log")(x, base) : non-numeric argument to mathematical function
map(test_list, test_function)
## Warning in log(x): NaNs produced
## [[1]]
## [1] 1.609438
## 
## [[2]]
## [1] NA
## 
## [[3]]
## [1] NaN
## 
## [[4]]
## [1] -Inf
## 
## [[5]]
## [1] NA
map_dbl(test_list, log)  
# Error in .Primitive("log")(x, base) : non-numeric argument to mathematical function
map_dbl(test_list, test_function)
## Warning in log(x): NaNs produced
## [1] 1.609438       NA      NaN     -Inf       NA