如何使用 purrr::possibility 来捕捉绘图错误 [英] How to use purrr::possibly to catch plot errors
问题描述
我尝试编写一个可以安全"运行其他函数的函数.这里的安全只是意味着如果某些绘图功能失败,我不希望脚本失败.简单例子:
库(ggplot2)图书馆(咕噜咕噜)## 函数我以为我可以用来安全地运行其他函数safe_plot <- function(.FUN, ...) {safe_fun <- purrr::possably(.f = .FUN, 否则 = NULL)safe_fun(...)}## 简单的绘图功能plot_fun <- 函数(df){df%>%ggplot(aes(xvar, yvar)) +geom_point()}## 适用于良好的数据some_data <- data.frame(xvar = 1:10, yvar = 1:10)safe_plot(.FUN = plot_fun, df = some_data)
## 适用于未定义的数据:>safe_plot(.FUN = plot_fun, df = not_defined)空值## 为空数据框而跌倒empty_data <- data.frame()>safe_plot(.FUN = plot_fun, df = empty_data)FUN(X[[i]], ...) 中的错误:找不到对象xvar"
我想要的是一个通用函数,我可以将绘图函数传递给它 - 如果发生错误 - 不会停止脚本.例如,我知道我可以执行以下操作:
## 带检查的简单绘图函数plot_fun <- 函数(df){如果(nrow(df)> 0){df%>%ggplot(aes(xvar, yvar)) +geom_point()}}
但在这里,我更感兴趣的是了解为什么 purrr::possibility 没有发现错误以及解决此问题的更好方法……假设我不在乎可能导致错误的原因.
可以直接使用tryCatch
.
在任何一种情况下,possably
和最终 tryCatch
都没有捕获到错误的原因是
I tried to write a function which would run other functions "safely". By safely here I just mean I don't want scripts to fall over if some plot functions fail. Simple example:
library(ggplot2)
library(purrr)
## function I thought I could use to run other functions safely
safe_plot <- function(.FUN, ...) {
safe_fun <- purrr::possibly(.f = .FUN, otherwise = NULL)
safe_fun(...)
}
## Simple plot function
plot_fun <- function(df) {
df %>%
ggplot(aes(xvar, yvar)) +
geom_point()
}
## Works for good data
some_data <- data.frame(xvar = 1:10, yvar = 1:10)
safe_plot(.FUN = plot_fun, df = some_data)
## Works here for data that is not defined:
> safe_plot(.FUN = plot_fun, df = not_defined)
NULL
## Falls over for an empty data frame
empty_data <- data.frame()
> safe_plot(.FUN = plot_fun, df = empty_data)
Error in FUN(X[[i]], ...) : object 'xvar' not found
What I would like is a generic function that I can pass plot functions to which - if an error occurs - won't stop a script. I know I could do the following for example:
## Simple plot function with check
plot_fun <- function(df) {
if (nrow(df) > 0) {
df %>%
ggplot(aes(xvar, yvar)) +
geom_point()
}
}
But here I'm more interested in learning about why purrr::possibly did not catch the error and a perhaps a better way to go about this problem...assuming I don't care what may have caused an error.
You could directly use tryCatch
.
In either cases, the reason for the error not being catched by possibly
and eventually tryCatch
is due to the lazy evaluation of print.ggplot
which occurs outside tryCatch
.
Solution is to force print()
into tryCatch()
:
library(ggplot2)
safe_plot <- function(.FUN, ...) {
tryCatch(print(.FUN(...)),error = function(e) NULL)
}
## Simple plot function
plot_fun <- function(df) {
df %>%
ggplot(aes(xvar, yvar)) +
geom_point()
}
## Works for good data
some_data <- data.frame(xvar = 1:10, yvar = 1:10)
safe_plot(.FUN = plot_fun, df = some_data)
#> NULL
empty_data <- data.frame()
safe_plot(.FUN = plot_fun, df = empty_data)
#> NULL
这篇关于如何使用 purrr::possibility 来捕捉绘图错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!