Running below code, I get "arguments must have same length" error from the tapply line. Outside of shiny,
with(crime2, tapply(number, list(type), summary))
runs fine though. (Btw, the widgets have a problem as all of them show up as red -- but I posted this problem on a different post.)
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)