How to determine if a function generates a chart?

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 to run

 createFigure(f(x,y))#Returns TRUE createFigure(g(x,y))#Returns FALSE 

thanks

+7
r graphics
source share
2 answers
 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() .

+20
source share

If for your purposes it is normal for all devices to be turned off before checking. Checks would be fine, because then the build teams make a new device. But then lines () and periods () will be exceptions.

In fact, this suggests that the question has not only a true or false answer, but also depends on the conditions. Some functions will draw something, even if there is no open device, while others will draw something if there is something else. What would you like to do in this case?

0
source share

All Articles