Very weird behavior using levelplot inside a function:
> foo <- function() { require(lattice); levelplot(matrix(rnorm(100),10,10)) }
> bar <- function() { require(lattice); levelplot(matrix(rnorm(100),10,10)); return(1) }
> foo() ## graph gets generated
Loading required package: lattice
> graphics.off()
> bar() ## NO GRAPH GETS GENERATED
[1] 1
foo() works as expected, and bar() does not generate any plot. Any ideas?
By default, the function returns the last generated object. In function
foo, this is the plot. In functionbar, it's1.If you want to generate a plot and return another object, you have to create the plot with
print.When you call
bar(), the plot will be created and1will be returned.