2. SimpleShinyApp.R 939 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14

#Building a simple Shiny App with an input and an output plot

#Creating a UI object with a fluidPage layout
ui <- fluidPage(
  
  #setting radio buttons input in the UI
  radioButtons("year", "Select Year: ",
               c("2015" = "2015",
                 "2016" = "2016",
                 "2017" = "2017",
                 "2018" = "2018",
                 "2019" = "2019")),
  
Sanjay Pant committed
15 16

    #setting an output in form of plot
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
  plotOutput("barplot")
  
)

#Creating the server object
server <- function(input, output){
  
  #defining the barplot introduced in the UI
  output$barplot <- renderPlot({
    
    #filtering data with respect to input from radio buttons using 'year' ID
    df_fil <- filter(df, YEAR == input$year)
    #defining the barplot using ggplot2
    ggplot(data = df_fil, aes(x = df_fil$HOUR)) + geom_bar() + xlab("Hour") + ylab("Number of Crimes") + theme_minimal() 
    
  })
}


shinyApp(ui = ui, server = server)
Sanjay Pant committed
37