R传单中有没有办法为某些位置分配不同的图标?
Is there a way in R leaflet to assign different icons to certain locations?
我正在为当地学区开发一款闪亮的应用程序,该应用程序将为根据空气污染状况不应放假的学校显示一面旗帜。该应用程序将针对特定的一天进行过滤,我添加了一个名为 recess 的列,根据空气污染物,该列被标记为“Stay Inside!”或“继续播放!”。然后我创建了一个图标列表 - 一个 child 播放图标“继续播放!”以及“呆在里面!”的危险图标。
我遇到问题的地方是函数。我试图让它告诉传单为当天可以放假的学校显示 child 播放图标,为当天不应该放假的学校显示危险图标。
这是我遇到的问题的一个例子:
# 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
我哪里错了?任何帮助将不胜感激!
我认为这可以在不使用函数的情况下进行简化。
添加一列 icon
以指明您要为每个 school/row 使用哪个图标:
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))
我正在为当地学区开发一款闪亮的应用程序,该应用程序将为根据空气污染状况不应放假的学校显示一面旗帜。该应用程序将针对特定的一天进行过滤,我添加了一个名为 recess 的列,根据空气污染物,该列被标记为“Stay Inside!”或“继续播放!”。然后我创建了一个图标列表 - 一个 child 播放图标“继续播放!”以及“呆在里面!”的危险图标。
我遇到问题的地方是函数。我试图让它告诉传单为当天可以放假的学校显示 child 播放图标,为当天不应该放假的学校显示危险图标。
这是我遇到的问题的一个例子:
# 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
我哪里错了?任何帮助将不胜感激!
我认为这可以在不使用函数的情况下进行简化。
添加一列 icon
以指明您要为每个 school/row 使用哪个图标:
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))