Skip to content

Instantly share code, notes, and snippets.

@marskar
Last active December 27, 2021 05:36
Show Gist options
  • Save marskar/ab1f4ecd61a705ea979d6c3026722b21 to your computer and use it in GitHub Desktop.
Save marskar/ab1f4ecd61a705ea979d6c3026722b21 to your computer and use it in GitHub Desktop.
Shiny Old Faithful Geyser example with magrittr pipes
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
library(magrittr)
# Application title
title <- "Old Faithful Geyser Data" %>%
titlePanel()
# Sidebar with a slider input for number of bins
side <- sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30) %>%
sidebarPanel()
# Show a plot of the generated distribution
main <- plotOutput("distPlot") %>%
mainPanel()
# Combine sidebar and main panels into layout
layout <- sidebarLayout(side,
main)
# Define UI for application that draws a histogram
ui <- fluidPage(title,
layout)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$distPlot <- renderPlot({
# Generate bins based on input$bins from ui.R
bins <- input$bins + 1
# Draw the histogram with the specified number of bins
faithful[, 2] %>%
hist(breaks = seq(min(.),
max(.),
length.out = bins),
col = 'darkgray',
border = 'white')
})
}
# Run the application
shinyApp(ui = ui,
server = server)
@marskar
Copy link
Author

marskar commented Dec 21, 2017

Whenever someone starts a new Shiny app in RStudio, they are greeted with the "Old Faithful" example.

I decided to work the example using magrittr pipes (%>%) where I could.

I think this method makes the code more readable, which in turn will make it easier to manage as the app grows larger.

Next time you make a Shiny app, give this method a try and let me know how it works for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment