I have been working with ggvis in R in to make a Shiny app. I've been having some trouble with incorporating a "toggle" option for including a linear model on top of some interactive plotting.
I've re-created a minimal working example (that mirrors my own, more complicated app) from the basic working example here. My expansion from this to generally include the linear model runs fine:
library(shiny)
library(ggvis)
library(ggplot2)
runApp(list(
ui = bootstrapPage(
ggvisOutput("plot"),
uiOutput("plot_ui")
),
server = function(..., session) {
mtc <- mtcars
mtc$id <- 1:nrow(mtc)
all_values <- function(x) {
if(is.null(x)) return(NULL)
row <- mtc[mtc$id == x$id, ]
paste0(names(row), ": ", format(row), collapse = "<br />")
}
mtc %>% ggvis(x = ~wt, y = ~mpg) %>%
layer_model_predictions(model = "lm", se = TRUE, opacity:= 0.3) %>%
layer_points(key := ~id) %>%
add_tooltip(all_values, "hover") %>%
bind_shiny("plot", "plot_ui") }))
(Although it does bother me that I can't get rid of the hover text on the layer_model_predictions(), that's a less worrisome cosmetic issue).
I've tried variations on this setup, e.g.:
library(shiny)
library(ggvis)
library(ggplot2)
runApp(list(
ui = bootstrapPage(
ggvisOutput("plot"),
uiOutput("plot_ui"),
selectInput("radio2", "Linear Fit",
list("Off" = "Off",
"On" = "On"))
),
server = function(..., session) {
mtc <- mtcars
mtc$id <- 1:nrow(mtc)
all_values <- function(x) {
if(is.null(x)) return(NULL)
row <- mtc[mtc$id == x$id, ]
paste0(names(row), ": ", format(row), collapse = "<br />")
}
reactive({ if (input$radio2=="On"){
mtc %>% ggvis(x = ~wt, y = ~mpg) %>%
layer_model_predictions(model = "lm", se = TRUE, opacity:= 0.3) %>%
layer_points(key := ~id) %>%
add_tooltip(all_values, "hover") %>%
bind_shiny("plot", "plot_ui") } })
reactive({ if (input$radio2=="Off"){
mtc %>% ggvis(x = ~wt, y = ~mpg) %>%
#### layer_model_predictions(model = "lm", se = TRUE, opacity:= 0.3) %>%
layer_points(key := ~id) %>%
add_tooltip(all_values, "hover") %>%
bind_shiny("plot", "plot_ui") } })
}
))
... in which the plot disappears entirely, but with no errors/warnings, so it's unclear to me why it disappears.
I've tried a few other things, like
Moving the reactivity and if statements in different spots in the code (e.g.: within the ggvis plotting code block), but haven't had any luck getting that to work any better.
I've tried passing an opacity:=input$value to the linear model so that I can toggle the visibility of the linear model on and off (which is a bit hacky, as it doesn't entirely solve the issue). I tried opacity:=reactive({ as.numeric(input$value) }), where the input$value is either 0 or 100. When I've tried this, I get an error of "object 'input' not found", which makes me wonder if there is some additional hurdle to pass a UI input into the ggvis-based plot (?).
I had trouble finding a solution to this particular issue elsewhere. I'd be happy to hear any thoughts or suggestions or alternative approaches.
Version info: I am running Yosemite on my Mac with R 3.2.0 ("Full of Ingredients") and using RStudio 0.99.441. Additionally, ggplot2_1.0.1, ggvis_0.4.1, and shiny_0.12.0.