Skip to content

Instantly share code, notes, and snippets.

@stephensrmmartin
Created April 4, 2014 23:22
Show Gist options
  • Save stephensrmmartin/9984959 to your computer and use it in GitHub Desktop.
Save stephensrmmartin/9984959 to your computer and use it in GitHub Desktop.
library(shiny)
library(ggvis)
colors <- c('black','red','blue','green')
shinyServer(function(input, output, session) {
dsR <- reactive({
invalidateLater(input$refresh, session);
data.frame(Value = rnorm(n = input$nObs, mean = input$mean, sd = input$sd))
})
gv <- reactive({
ggvis(dsR,props(x = ~Value,
fillOpacity := input_slider(0, 1, .8, step = .1, label = "Opacity")),
#This will cause the mark_{rect,path} to be reset to black on every refresh
# transform_bin(binwidth = (max(dsR()) - min(dsR())) / input$nBars)) +
#This works fine
transform_bin()) +
mark_rect(props(x = ~xmin__, x2 = ~xmax__, y = ~count__, y2 = 0,
fill := input_select(choices = colors,
selected = 'black',
multiple = FALSE,
label = 'Bar fill color'))) +
mark_path(props(x = ~x, y = ~count__,
fill := input_select(choices = colors,
selected = 'black',
multiple = FALSE,
label = 'Path fill color'))) +
dscale('x',type = 'numeric', domain = c(-500,500)) +
guide_axis('x',title = 'X value (binned)') + guide_axis('y',title = 'Frequency')
})
output$distPlot <- observe_ggvis(gv,plot_id = 'distPlot',session = session)
output$distPlotC <- renderControls(gv)
})
library(shiny)
library(ggvis)
shinyUI(pageWithSidebar(
# Application title
headerPanel("The effect of sample size and variance."),
# Sidebar with a slider input for number of observations
sidebarPanel(
sliderInput("nObs",
"Number of observations:",
min = 5,
max = 10000,
value = 20),
sliderInput("sd",
"Standard Deviation:",
min = .01,
max = 500,
value = 1,
step = .01),
sliderInput('mean',
'Mean:',
min = -100,
max = 100,
value = 0),
numericInput('refresh',
'Refresh Rate (in ms)',
3000,
500,
60000),
numericInput('nBars',
"Number of bars",
30,
1,
1000)
),
# Show a plot of the generated distribution
mainPanel(
ggvis_output("distPlot"),
ggvisControlOutput('distPlotC')
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment