Skip to content

Instantly share code, notes, and snippets.

@ijlyttle
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ijlyttle/9954598 to your computer and use it in GitHub Desktop.
Save ijlyttle/9954598 to your computer and use it in GitHub Desktop.
ggvis and shiny
library(shiny)
library(ggvis)
data(diamonds, package = "ggplot2")
shinyServer( function(input, output, session) {
hist_standard <- reactive({
ggvis(diamonds, props(x = ~table)) +
layer_histogram(binwidth = 1)
})
hist_stacked <- reactive({
ggvis(diamonds,
by_group(cut),
props(x = ~table, fill = ~cut),
transform_bin(binwidth = 1)) +
layer(
props(x = ~xmin__, x2 = ~xmax__, y = ~count__, fillOpacity := 0.6),
layer(
transform_stack(),
mark_rect(props(y = ~ymax__, y2 = ~ymin__))
)
)
})
observe_ggvis(hist_standard, "hist_standard", session)
observe_ggvis(hist_stacked, "hist_stacked", session)
})
library(shiny)
shinyUI( fluidPage(
titlePanel("Histogram test"),
sidebarLayout(
sidebarPanel(
helpText(
p("The goal is to get a ggvis plot to work within shiny."),
p("Using the diamonds dataset, a couple of ggvis plots are made:"),
tags$ol(
tags$li("a \"standard\" histogram using", code("layer_histogram()")),
tags$li("a \"stacked\" histogram using techniques outlined in the ggvis",
a("histogram demo.",
href = "https://github.com/rstudio/ggvis/blob/master/demo/histogram.r") )
),
p("The standard histogram works interactively and in shiny.",
"The stacked histogram works interactively, but not in shiny."),
p("I would not be surprised if this has to do with non-standard evaluation within a reactive",
"environment, and my inability to navigate between the two."),
p("As a next step, I would be like to be able to use a", code("selectInput()"),
"to allow the user to see how the distributions change according to cut, color, depth, etc.",
"First, I need to be able to get the stacked historgram to work.")
)
),
mainPanel(
ggvis_output("hist_standard"),
ggvis_output("hist_stacked")
)
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment