带有传单的闪亮选择范围和数值变量(作为输入)

时间:2018-06-25 15:07:32

标签: r shiny leaflet

关于闪亮,我想显示一个数字变量列表和一个滑动条,以便用户可以选择一个数字变量和一个范围。然后,低于该数字的观测值将显示为绿色,而位于该范围之间的观测值将显示为橙色,而高于该范围的观测值将显示为红色。

下面的代码在我变亮之前可以正常工作。但是我的闪亮代码不起作用,所有观察结果均为红色。

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

crime2 <- crime[1:50,]

getColor <- function(crime2) {
 sapply(crime2$hour, function(hour) {
 if(hour< 1) {
   "green"
 } else if(hour <= 1) {
   "orange"
 } else {
   "red"
  } })
}

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

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

这是无效的代码

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"))
  )
)


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)

  })
}

shinyApp(ui=ui, server=server)

有人知道如何解决“所有显示为红色的点”问题吗?

先谢谢您!

1 个答案:

答案 0 :(得分:1)

一些更改:

  1. 创建selectInput时,选择(和选择)必须小写以匹配Crime2中的列名。

    selectInput("var", 
                  label = "Choose a variable to display",
                  choices = c("hour",
                              "number"),
                  selected = "hour"),
    
  2. 要在getColor函数内部循环数据中的值,而不要输入$ var,因此应在lambda函数内部调用var而不是input$var

    getColor <- function(data){sapply(data, function(var){
          if(var< input$range[1]) {
            "green"
          } else if(var <= input$range[2]) {
            "orange"
          } else {
            "red"
          } })
        }
    
  3. 在实际创建图标时,您希望基于data中的值而不是整个Crime2数据集中的值来创建图标。

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

将它们放在一起:

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"))
  )
)


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(var< input$range[1]) {
        "green"
      } else if(var <= input$range[2]) {
        "orange"
      } else {
        "red"
      } })
    }

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

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

  })
}

shinyApp(ui=ui, server=server)