Is there a way to determine if a function generates a figure in R?
For example, if we have functions f and g
f = function(x,y){plot(x,y)}
g = function(x,y){mean(x*y)}
I would like able to run
createFigure(f(x,y))#Returns TRUE
createFigure(g(x,y))#Returns FALSE
Thanks
makes_plot <- function(x) {
before <- .Internal(getSnapshot())
force(x)
after <- .Internal(getSnapshot())
!identical(before, after)
}
makes_plot(mean(1:10))
makes_plot(plot(1:10))
The .getSnapshot
function was discovered by looking at the source of recordPlot()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With