2. SimpleShinyApp.R 939 B
Newer Older

#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's avatar
Sanjay Pant committed

    #setting an output in form of plot
  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's avatar
Sanjay Pant committed