我正在为当地的一个学区开发一个闪亮的应用程序,它将根据空气污染状况显示不应该放假的学校的旗帜。这个应用程序将过滤特定的一天,我已经添加了一个名为休息的列,根据空气污染物,它被标记为“留在室内!”或者“继续玩下去!”然后我创建了一个图标列表--一个孩子玩的图标,代表“继续玩吧!”还有一个表示“待在里面!”的危险图标。
我遇到麻烦的地方是函数。我正在尝试让它告诉宣传单,在当天可以放假的学校中显示儿童玩耍图标,在不应该放假的学校中显示危险图标。
下面是我遇到的问题的一个例子:
# 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我哪里错了?任何帮助都将不胜感激!
发布于 2020-07-19 04:32:07
我认为不使用函数可以简化这一过程。
添加一个列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))https://stackoverflow.com/questions/62971701
复制相似问题