Skip to content

Instantly share code, notes, and snippets.

@bayesball
Last active October 31, 2018 02:03
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 bayesball/2e5fa35bd7af051cb0349460dcf45626 to your computer and use it in GitHub Desktop.
Save bayesball/2e5fa35bd7af051cb0349460dcf45626 to your computer and use it in GitHub Desktop.
Data Science Club, November 2, 2018: Some Shiny examples
# histogram of some random data
# choose between normal, uniform, exponential distributions
# using radio buttons
# add a select input (sample size)
# ----------------------
# add a slider for the number of bins in histogram
library(shiny)
ui <- fluidPage(
selectInput(inputId = "n",
label = "Sample size:",
choices = c(10, 25, 50, 100, 200),
selected = 10),
radioButtons(inputId = "dist",
label = "Distribution:",
choices = c("Normal" = "norm",
"Uniform" = "unif",
"Exponential" = "exp"),
inline = FALSE ),
sliderInput(inputId = "b",
label = "Number of Bins:",
min = 2,
max = 20,
value = 8),
plotOutput(outputId = "hist")
)
server <- function(input, output){
output$hist <- renderPlot({
dist <- switch(input$dist,
norm = rnorm,
unif = runif,
exp = rexp,
rnorm)
data <- dist(input$n)
bins <- seq(min(data), max(data),
length.out = input$b + 1)
hist(data, breaks = bins)
})
}
shinyApp(ui = ui, server = server)
# the simplest Shiny app
library(shiny)
ui <- fluidPage(
)
server <- function(input, output){
}
shinyApp(ui = ui, server = server)
# histogram of some random data
# choose between normal, uniform, exponential distributions
# using radio buttons
library(shiny)
ui <- fluidPage(
radioButtons(inputId = "dist",
label = "Distribution:",
choices = c("Normal" = "norm",
"Uniform" = "unif",
"Exponential" = "exp")),
plotOutput(outputId = "hist")
)
server <- function(input, output){
output$hist <- renderPlot({
dist <- switch(input$dist,
norm = rnorm,
unif = runif,
exp = rexp,
rnorm)
hist(dist(100))
})
}
shinyApp(ui = ui, server = server)
# histogram of some random data
# choose between normal, uniform, exponential distributions
# using radio buttons
# ----------------------
# add a select input (sample size)
library(shiny)
ui <- fluidPage(
selectInput(inputId = "n",
label = "Sample size:",
choices = c(10, 25, 50, 100, 200),
selected = 10),
radioButtons(inputId = "dist",
label = "Distribution:",
choices = c("Normal" = "norm",
"Uniform" = "unif",
"Exponential" = "exp"),
inline = FALSE ),
plotOutput(outputId = "hist")
)
server <- function(input, output){
output$hist <- renderPlot({
dist <- switch(input$dist,
norm = rnorm,
unif = runif,
exp = rexp,
rnorm)
data <- dist(input$n)
hist(data)
})
}
shinyApp(ui = ui, server = server)
# a simple ggplot2 graph
# graphing the runs scored per game per team against season
# plot points and show a smooth
library(Lahman)
library(ggplot2)
ggplot(Teams, aes(yearID, R / G)) +
geom_point() + geom_smooth()
# motivation for a Shiny app
########################################
# change y variable?
# change span of years?
# change graph characteristics (color of points and smooth)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment