plotly.R
plotly.R copied to clipboard
Feature: convert ggplot to plotly by adding a layer
By this PR, we can convert ggplot to plotly by implementing gginteractive().
In addition, gginteractive has the interactive argument to control the conversion.
ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
gginteractive()
# gginteractive(FALSE) # can suppress conversion
This is a sort of syntax sugar for ggplotly(), which has to wrap whole ggplot:
ggplotly(
ggplot(mtcars, aes(wt, mpg)) +
geom_point()
)
Interesting, thanks! Is there a way we could do this without introducing a new function? That is, could something like this work?
ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
ggplotly()
Well, I wanted to do so.
I did not do it because the first argument of ggplotly is last_plot().
If it is missing or NULL, we can do what we want by
if (missing(p)) return(ggintearactive())
# or
if (is.nuill(p)) return(gginteractive())
The only way is to add a new argument like
ggplotly <- function(..., layer = FALSE) {
if (layer) return(gginteractive())
...
}