Skip to content

Instantly share code, notes, and snippets.

@trestletech
Last active January 3, 2016 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save trestletech/8522356 to your computer and use it in GitHub Desktop.
Save trestletech/8522356 to your computer and use it in GitHub Desktop.
A Shiny example using a sliderInput in a sidebar layout with pre-processing done in global.R.
# Define a dataset globally which will be available
# both to the UI and to the server.
# Rely on the 'WorldPhones' dataset in the datasets
# package (which generally comes preloaded).
library(datasets)
# Trim out the non-consecutive year from
# the 'WorldPhones' dataset (the first row).
myData <- WorldPhones[-1,]
library(shiny)
# Define a server for the Shiny app
shinyServer(function(input, output) {
# Fill in the spot we created for a plot
output$phonePlot <- renderPlot({
# Render a barplot
barplot(myData[as.character(input$year),]*1000,
main=paste("Phones in", input$year),
ylab="Number of Telephones",
xlab="Region",
ylim=c(0,max(myData)*1000))
})
})
library(shiny)
# Store the years for the data as a numeric
years <- as.numeric(rownames(myData))
# Define the overall UI
shinyUI(
# Use a fluid Bootstrap layout
fluidPage(
# Give the page a title
titlePanel("Telephones Around the World"),
sidebarLayout(
# Define the sidebar with one input
sidebarPanel(
sliderInput("year", "Year:",
min=min(years),
max=max(years),
value=min(years),
format="####",
animate=TRUE
),
hr(),
helpText("Data from AT&T (1961) The World's Telephones.")
),
# Create a spot for the barplot
mainPanel(
plotOutput("phonePlot")
)
)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment