Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine if a function generates a graph

Tags:

r

graphics

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

like image 773
csgillespie Avatar asked Apr 30 '10 09:04

csgillespie


1 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().

like image 122
hadley Avatar answered Nov 13 '22 18:11

hadley