Shiny

Yesterday I tried making an application in Shiny for interactive data visualization. This app shows the proportion of accidents in the city of Baton Rouge, LA with respect to a certain factor, such as road condition or lighting. The data for this application was takeng from publicly avaliable repository (link). This version is pretty bare-bones, but I plan on adding more features, such as real-time k-means clustering, in the future. An important caveat is that although this database is updated daily, as specified on the site, I used the static version (since I lack a direct link to the database) downloaded on March 17, 2018. You can see the app in action here, and the code below. Since markdown has troubles displaying Russian characters, this code only has English titles.


          library(lattice)
          cars_br <- read.csv(file="Baton_Rouge_Traffic_Incidents.csv", header=TRUE, sep=",")
          cars_names <- names(cars_br)
          allowed_names <- cars_names[c(13, 21:33)]
          library(shiny)




          # Define UI for application that draws a histogram
          ui <- fluidPage(

              # Application title
          titlePanel("Accidents in Baton Rouge, LA"),

              # Sidebar with a slider input for number of bins 
          sidebarLayout(
            sidebarPanel(
              selectInput('xcol', 'Factor', allowed_names),
              hr(),
              helpText("Frequency of accidents in Baton Rouge depending on the factor. Choose the factor above and see the proportion of accidents associated with it.")
              ),

            # Show a plot of the generated distribution
            mainPanel(
               plotOutput("distPlot")
            )
          )
          )



          # Define server logic required to draw a histogram
          server <- function(input, output) {

              output$distPlot <- renderPlot({
            # generate bins based on input$bins from ui.R
           selectedData <- reactive({
             cars_br[, input$xcol]
           })

           x    <- selectedData()
           # draw the histogram with the specified number of bins

           dotplot(summary(x)/length(x), col = rgb(0.2,0.2,0.8,0.7), main = "Proportion of Accidents", lwd = 10, pch = 17, cex =3.0, scales=list(cex=1.3), xlab = "Proportion")
          })
          }

          # Run the application 
          shinyApp(ui = ui, server = server)
Avatar
Anton Leontyev
Assistant Professor of Psychology & Data Scientist

I am a scientist interested in applyting machine learning, statistics and data visualization techniques to answer political, psychological and economic questions.