'How to make my custom `$<-` method honor `invisible()`
I am documenting my research in rmarkdown workbooks but want to also save my ggplots into a variable to reconfigure the plots for other cases, for example resize them for presentations.
First, I had a function that prints my plot and saves into a global variable:
PLOTS <- list()
`%<p%` <- function(name, ggplot){
PLOTS[[name]] <<- ggplot
print(ggplot)
return(invisible(NULL)) # better use ggplot here (if used with %>but this is easier for question
}
# Plots and saves
"testplot" %<p% qplot(x = Sepal.Length, y = Sepal.Width, data = iris)
Soon I had the problem that I want to save one version of the plot, but plot another version, for example save the plot but then plot in my workbook two versions with different x axis limits. So I introduced that the function recognizes invisible():
`%<p%`<- function(name, ggplot){
v <- withVisible(ggplot)$visible
if(v) print(ggplot)
PLOTS[[name]] <<- ggplot
return(invisible(NULL))
}
# Save full plot but print only x between 4 and 5
"testplot" %<p% invisible(qplot(x = Sepal.Length, y = Sepal.Width, data = iris))
PLOTS$testplot + coord_cartesian(xlim = c(4,5))
Works beautifully. Then I got really lazy and wanted to use the RStudio shortcut for <- instead of typing unwieldy special characters for %<p%, so I thought of making this function an implementation of the $<- generic, for a new class "plotlist". I then create a list with this new class to cause invokation of `$<-.plotlist`() when an element is assigned to this.
PLOTS2 <- structure(list(), class = "plotlist")
`$<-.plotlist` <- function(x, name, value){
v <- withVisible(value)$visible
if(v) print(value)
NextMethod()
}
But now things get strange as now the invisible() does not work anymore. For example, this renders the ggplot:
PLOTS2$test <- invisible(qplot(x = Sepal.Length, y = Sepal.Width, data = iris))
On the other hand, if I hadn't used the custom $<- implementation, it wouldn't even print by default, for example if I use my ordinary list without the special class "plotlist" to store my plot!
PLOTS$test <- qplot(x = Sepal.Length, y = Sepal.Width, data = iris)
How is this? I do not really know how invisible() works and the manuals of invisible() and withVisibility() say only the object stays invisible "for a while". What are the criteria how long is an object invisible, why is it not in my custom $<- implementation and can I make it honor invisible()?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
