如何查看哪个列表元素在 R 中生成 warning/error 消息?

How can I see which list element generates a warning/error message in R?

我有一个包含大约一千个数据帧的列表,我正在使用 lapply 对所有这些数据帧应用一个函数。但是,似乎有八个列表元素(数据框)会生成此警告消息:

In mapply(FUN = f, ..., SIMPLIFY = FALSE) :
  longer argument not a multiple of length of shorter

所以基本上我只想追踪产生错误的元素,这样我就可以对它们进行必要的修复,但不知道如何做。到目前为止,只是通过对数据帧应用函数并查看特定数据帧是否产生错误(如 testdata <- my_function(df[[1]], "X"))来单独检查数据帧,但正如预期的那样,它会永远经历大声笑。

你可以试试 possibly()。例如。如果某些值不是数字,我们不能将数字除以它(因此会出错)。有关使用 purrr 进行错误处理的更多信息,请参阅 https://aosmith.rbind.io/2020/08/31/handling-errors/

library(purrr)
library(dplyr)
my_function <- function(x) { 20/x}
find_error = possibly(.f = my_function, otherwise = NULL)

df <- 
  list(
    df1 = tibble(values =c(1,2,3)),
    df2 = tibble(values = c("1","2","3")
    ))

df %>% 
  map(find_error) %>% 
  keep(~is.null(.x))

我通常会发现 purrr::quietly() 在收到 警告 时很有帮助(对于错误,possibly() 更好)。每次迭代都会生成一个包含以下元素的列表:

  • 结果
  • 输出
  • 警告
  • 条消息

这里有一个关于如何识别给您带来问题的数据帧的代表:

library(purrr)

# Replace "log" with your function, and the vector with your list of dataframes
res <- 
  c(10, 20, -1) %>% 
  map(quietly(log)) # note the quietly()


# This gives you the first index where you got a warning
res %>% 
  detect_index(~length(.x$warnings) > 0)
#> [1] 3

# With this map you can find the warning of all dataframes, also those who don't 
# have any. The index will tell you where all problems are
res %>% 
  map(~.x$warnings)
#> [[1]]
#> character(0)
#> 
#> [[2]]
#> character(0)
#> 
#> [[3]]
#> [1] "NaNs produced"

# With keep you can see all results from iterations with warnings
res %>% 
  keep(~length(.x$warnings) > 0)
#> [[1]]
#> [[1]]$result
#> [1] NaN
#> 
#> [[1]]$output
#> [1] ""
#> 
#> [[1]]$warnings
#> [1] "NaNs produced"
#> 
#> [[1]]$messages
#> character(0)

reprex package (v2.0.1)

于 2022-04-05 创建