After seeing a talk on GGVIS, I have been trying my hand unsuccessfully at creating my first Shiny/ggvis app. My plot works in R, but when I try to migrate it into a Shiny app for display on the web I get nothing. The radio buttons are displayed and from what I can tell seem to be working (I tested using the table from rStudio/Shiny/Reactivity tutorial, but it doesn't seem to place nice with my ggvis plot). I have been following the tutorials on Rstudio and using pieces from the demo folder in the ggvis source code. I am trying to create a simple kernel density plot that subsets data based on user input and displays the distribution. Attached is a reproducible example of what I have thusfar. I would appreciate any insight as to what I am missing here.
ui.R
# clear memory & load packages
rm(list=ls())
library(shiny)
library(ggvis)
# Define UI for distribution application
shinyUI(fluidPage(
# Application title
titlePanel("Diamond Carats by Color/Cut"),
# Sidebar with controls to select subset
sidebarLayout(
sidebarPanel(
radioButtons("cut", "Diamond Cut:",
c("Ideal" = "IDEAL",
"Premium" = "IDEAL",
"Good" = "GOOD",
"Very Good" = "VGOOD"))
),
# Display your plot created by GGvis
mainPanel(ggvis_output("my_plot"))
)
))
server.R
# clear memory & load packages
rm(list=ls())
library(shiny)
library(ggvis)
# Define server logic for distribution application
shinyServer(function(input, output, session) {
# load your data
dataset <- diamonds
# Reactive expression to generate the subset.
datasetInput <- reactive({
selection <-switch(input$cut,
IDEAL = "Ideal",
PREM = "Premium",
GOOD = "Good",
VGOOD = "Very Good")
subset(dataset, cut == selection)
})
# Generate your plot using GGvis and your reactive inputs
gv <- reactive({
ggvis(datasetInput(), by_group(color),
props(x = ~carat,
stroke = ~color,
fill = ~color,
fillOpacity := 0.2,
fillOpacity.hover := 0.7)) +
layer_density()
})
# necessary additions for ggvis integration to shiny
output$controls <- renderControls(gv)
observe_ggvis(gv, "my_plot", session)
})
EDIT: To follow up on this question, I have noticed that if I remove the following:
by_group(color)
and
stroke = ~color,
fill = ~color,
from my call to create the graph in server.R, the graph runs (albeit without the multicolored groupings, which is what I was after in the first place) and the radio buttons successfully select my data subset. As mentioned, when I am just running the graph in R, without the use of the reactive subsetting function, the graph is able to run and vary the stroke/fill by diamond color. Is this feature currently not supported in Shiny to ggvis interaction at this time? Or am I just misinterpreting this function somehow? Here is my updated code, so you can see my result:
ui.R
# clear memory & load packages
rm(list=ls())
library(shiny)
library(ggvis)
# Define UI for distribution application
shinyUI(fluidPage(
# Application title
titlePanel("Diamond Carats by Color/Cut"),
# Sidebar with controls to select subset
sidebarLayout(
sidebarPanel(
radioButtons("cut", "Diamond Cut:",
c("Ideal" = "IDEAL",
"Premium" = "IDEAL",
"Good" = "GOOD",
"Very Good" = "VGOOD"))
),
# Display your plot created by GGvis
mainPanel(ggvis_output("my_plot"))
)
))
server.R
# clear memory & load packages
rm(list=ls())
library(shiny)
library(ggvis)
# Define server logic for distribution application
shinyServer(function(input, output, session) {
# load your data
dataset <- diamonds
# Reactive expression to generate the subset.
datasetInput <- reactive({
selection <-switch(input$cut,
IDEAL = "Ideal",
PREM = "Premium",
GOOD = "Good",
VGOOD = "Very Good")
subset(dataset, cut == selection)
})
# Generate your plot using GGvis and your reactive inputs
gv <- reactive({
ggvis(datasetInput(),
props(x = ~carat,
fillOpacity := 0.2,
fillOpacity.hover := 0.7)) +
layer_density()
})
# necessary additions for ggvis integration to shiny
output$controls <- renderControls(gv)
observe_ggvis(gv, "my_plot", session)
})
Further search would indicate that this is a known bug for ggvis: https://github.com/rstudio/ggvis/issues/71