Search code examples
rggplot2shinyggmaprscript

Output not showing in Shiny


I complied the below code to show the output as a GGmap in R Script. The code runs fine but the output is not shown. Could anyone please be kind enough to assist. I been trying to figure out the error but still yet not been able to. Appreciate it if anyone could help me. Thank you in advance.

    #### Below Code ####
    ## Libraries

   library(ggmap)
   library(shiny)
            
    bbox = c(left=-95.8, bottom=29.4, right=-95.0, top=30.0)
    map <- get_stamenmap(bbox, zoom = 10, source="stamen")
    
    ui <- fluidPage(
      titlePanel("Crime Offences"),
      sidebarPanel('Navigator',
                   checkboxGroupInput("crime_type", label = "Select offense type:",
                                      choices = list("Aggravated Assault" = 'aggravated assault',
                                                     "Auto Theft" = 'auto theft',
                                                     "Burglary" = 'burglary',
                                                     "Murder" = 'murder',
                                                     "Rape" = 'rape',
                                                     "Robbery" = 'robbery'),
                                      selected = 'Intel'),
                   dateRangeInput("date_range", label = "Select time period:",
                                  start = "2010-01-01",
                                  end = "2010-08-31",
                                  min = "2010-01-01",
                                  max = "2010-08-31",
                                  format = "mm/dd'yy",
                                  separator = " - ")),
      
      
      mainPanel('Crime Data Information',
                plotOutput("crime_data_final")),
      position = 'left')
    
    
    server <- function(input, output){
      output$crime_data_final <- renderPlot(
        
        ggmap(map) + stat_density2d(data = subset(crime_data, offense == input$crime_type),
                                    aes(x = "date", y = input$crime_type) +
          scale_fill_gradient(low = "green", high = "red") +
          scale_alpha_continuous(range = c(0, 0.8)) +
          geom_point(data = subset(crime_data, offense == input$crime_type),
                     aes(x = "date", y = input$crime_type), size = 0.5) + 
          guides(fill = FALSE, alpha = FALSE) +
          ggtitle('Crime in Houston TX',
                  subtitle = 'Murders are concentrated around two areas in the city'))
      )
    }
    
    ## Executing the app
    
    shinyApp(ui = ui, server = server)
    
    

Below is the output

example output


Solution

  • As you didn't provide crime_data, difficut to test, but try to replace date format:

     dateRangeInput("date_range", label = "Select time period:",
                                      start = "2010-01-01",
                                      end = "2010-08-31",
                                      min = "2010-01-01",
                                      max = "2010-08-31",
                                      format = "mm/dd'yy",
                                      separator = " - "))
    

    by

     dateRangeInput("date_range", label = "Select time period:",
                                      start = "2010-01-01",
                                      end = "2010-08-31",
                                      min = "2010-01-01",
                                      max = "2010-08-31",
                                      format = yyyy-mm-dd",
                                      separator = " - "))