2

我正在为当地学区开发一款闪亮的应用程序,该应用程序将为根据空气污染状况不应该放假的学校显示一个标志。该应用程序将过滤特定的一天,我添加了一个名为“休息”的列,根据空气污染物,它被标记为“待在室内!” 或“开始播放!”。然后,我创建了一个图标列表 - 一个用于“播放!”的儿童游戏图标。以及“留在里面!”的危险图标。

我遇到问题的地方是功能。我试图让它告诉传单显示当天可以放假的学校的孩子玩耍图标,以及当天不应该放假的学校的危险图标。

这是我遇到的问题的一个示例:

# Load Libraries
library(tidyverse)
library(leaflet)



# Vectors
Schools <- c("CHS", "BHS", "DHS")
latitude <- c(60, 61, 62)
longitude <- c(100, 101, 102)
recess <- c("Stay Inside!", "Play on!", "Play on!")

# Data frame
bad_air <- data.frame(Schools, latitude, longitude, recess)

# Map Icons
recessIcons <- awesomeIconList(
  child = makeAwesomeIcon(icon = "child", library = "fa", 
                          markerColor = "blue"),
  danger = makeAwesomeIcon(icon = "exclamation", library = "fa", 
                           markerColor = "darkred")
)

# Function to grab map icon
getrecessIcon <- function(bad_air){
  sapply(bad_air$recess, function(recess){
    if(bad_air$recess == "Stay Inside"){
      recessIcons$child
    } else {
      recessIcons$danger
    }
  })
}

# Build Leaflet Map
leaflet(bad_air) %>%
  addProviderTiles(providers$CartoDB.Positron) %>% 
  addAwesomeMarkers(~longitude,
                    ~latitude,
                    icon = ~getrecessIcon(bad_air),
                    label = ~Schools,
                    labelOptions = labelOptions(noHide = F))

然后我得到这个错误:

Warning messages:
1: In if (bad_air$recess == "Stay Inside") { :
  the condition has length > 1 and only the first element will be used
2: In if (bad_air$recess == "Stay Inside") { :
  the condition has length > 1 and only the first element will be used
3: In if (bad_air$recess == "Stay Inside") { :
  the condition has length > 1 and only the first element will be used

我哪里错了?任何帮助将不胜感激!

4

1 回答 1

1

我认为这可以在不使用该功能的情况下简化。

添加一列icon以指示您要为每个学校/行使用哪个图标:

bad_air$icon <- ifelse(bad_air$recess == "Stay Inside!", "danger", "child")

然后,通过从您创建的图标中选择适当的图标recessIcons(即recessIcons[icon])来访问您需要的每所学校的图标:

# Build Leaflet Map
leaflet(bad_air) %>%
  addProviderTiles(providers$CartoDB.Positron) %>% 
  addAwesomeMarkers(~longitude,
                    ~latitude,
                    icon = ~recessIcons[icon],
                    label = ~Schools,
                    labelOptions = labelOptions(noHide = F))
于 2020-07-18T20:32:06.753 回答