Search code examples
rshinyr-leaflettapply

using tapply inside shiny to produce summary outputs


The code below is reproducible:

library(shiny)
library(Rcpp)
library(ggmap)
library(htmlwidgets)
library(leaflet)

crime2 <- crime[1:50,]

ui <- fluidPage(
  titlePanel("Unusual Observations"),

  sidebarLayout(
    sidebarPanel(
      helpText("Create maps with 
        information from the Crime Data"),
  
      selectInput("var", 
              label = "Choose a variable to display",
              choices = c("Hour",
                          "Number"),
              selected = "Hour"),
  
      sliderInput("range", 
              label = "Range of interest:",
              min = 0, max = 10, value = c(1, 2))
    ),

    mainPanel(leafletOutput("map"))
  ),

  verbatimTextOutput("stats")
)

server <- function(input, output) {
  output$map <- renderLeaflet({
    data <- switch(input$var,
               "hour" = crime2$hour,
               "number" = crime2$number)

    getColor <- function(data){sapply(data, function(var){
       if(input$var< input$range[1]) {
         "green"
       } else if(input$var <= input$range[2]) {
         "orange"
       } else {
         "red"
        } })
    }

  icons <- awesomeIcons(
  icon = 'ios-close',
  iconColor = 'black',
  library = 'ion',
  markerColor = getColor(crime2)
)

    leaflet(crime2) %>%
      addTiles() %>%
      addAwesomeMarkers(~lon, ~lat, icon=icons)

  })

  output$stats <- renderPrint({
    with(crime2, tapply(input$var, list(type), summary))
  })
}

shinyApp(ui=ui, server=server)

For the stats output, I get an error saying:

Error: arguments must have same length.

Does anyone know how to fix the problem? Besides this, I also have all widgets showing up as red, but I have another post asking about the widget problem. Would much appreciate if anyone could help me with this.


Solution

  • Debugging:

    If we debug your code, i see that you attempt doing this:

    crime2 <- crime[1:50,]
    with(crime2, tapply("Hour", list(type), summary))
    

    Outside of shiny, i would guess your desired output is:

    with(crime2, tapply(X = hour, INDEX = type, FUN = summary))
    

    In shiny you want to access via an input, so with a character. Therefore you could rewrite your code to:

    tapply(X = unlist(crime2["hour"]), INDEX = crime2$type, FUN = summary)
    

    or dynamically:

    tapply(X = unlist(crime2[input$var]), INDEX = crime2$type, FUN = summary)
    

    .

    Full reproducible example would be:

    (input$var choices have to be changed to lower-case to enable indexing,...)

    library(shiny)
    library(Rcpp)
    library(ggmap)
    library(htmlwidgets)
    library(leaflet)
    
    crime2 <- crime[1:50,]
    
    ui <- fluidPage(
      titlePanel("Unusual Observations"),
    
      sidebarLayout(
        sidebarPanel(
          helpText("Create maps with 
                   information from the Crime Data"),
    
          selectInput("var", 
                      label = "Choose a variable to display",
                      choices = c("hour",
                                  "number"),
                      selected = "hour"),
    
          sliderInput("range", 
                      label = "Range of interest:",
                      min = 0, max = 10, value = c(1, 2))
          ),
    
        mainPanel(leafletOutput("map"))
      ),
    
      verbatimTextOutput("stats")
      )
    
    server <- function(input, output) {
      output$map <- renderLeaflet({
        data <- switch(input$var,
                       "hour" = crime2$hour,
                       "number" = crime2$number)
    
        getColor <- function(data){sapply(data, function(var){
          if(input$var< input$range[1]) {
            "green"
          } else if(input$var <= input$range[2]) {
            "orange"
          } else {
            "red"
          } })
        }
    
        icons <- awesomeIcons(
          icon = 'ios-close',
          iconColor = 'black',
          library = 'ion',
          markerColor = getColor(crime2)
        )
    
        leaflet(crime2) %>%
          addTiles() %>%
          addAwesomeMarkers(~lon, ~lat, icon=icons)
    
      })
    
      output$stats <- renderPrint({
        tapply(X = unlist(crime2[input$var]), INDEX = crime2$type, FUN = summary)
      })
    }
    
    shinyApp(ui = ui, server = server)