r - Shiny+leaflet:如何根据用户输入设置标记颜色
r - Shiny+leaflet: How to set Markers color depend on user input
我使用 shiny 和 leaflet 在地图上添加了点。
每个点都是不同类型的交通选项。
我想通过颜色区分不同的类型,但无法弄清楚。
尝试使用 "if" 无效。
谢谢!
这是我的基本代码
library(leaflet)
ui <- fluidPage(
leafletOutput("map"),
headerPanel("Example"),
sidebarPanel(checkboxGroupInput(inputId = "Type", label = "Data
Layer",choices = c("Bike", "Muni", "Bus", "BART"), selected = "Bike")))
server <- function(input, output) {
output$map <- renderLeaflet({
rownumber = which(Stops_and_stations$Type == input$Type)
x <- Stops_and_stations[rownumber,]
leaflet(width = 1000, height = 500) %>%
addTiles() %>%
addCircleMarkers(lng = x$stop_lon,
lat = x$stop_lat,
radius= 3, color = '#ff6633') %>%
setView(lng = -122.4000,
lat = 37.79500,
zoom = 13)
})
}
shinyApp(ui, server)
这就是我试图添加的内容
.....
if(input$Type == "Bike"){
leaflet(width = 1000, height = 500) %>%
addTiles() %>%
addCircleMarkers(lng = x$stop_lon,
lat = x$stop_lat,
radius= 3, color = '#ff6633') %>%
setView(lng = -122.4000,
lat = 37.79500,
zoom = 13)
}
if(input$Type == "Muni"){
leaflet(width = 1000, height = 500) %>%
addTiles() %>%
addCircleMarkers(lng = x$stop_lon,
lat = x$stop_lat,
radius= 3, color = '#0033ff') %>%
setView(lng = -122.4000,
lat = 37.79500,
zoom = 13)
}
.....
如果您提供 Stops_and_stations
并使其成为 reproducible example..
,那么回答您的问题会容易得多
为不同的组使用不同颜色的一种方法是将 color
列添加到 data.frame
:
由于我们不知道你的数据,我创建了一些随机数据集。
Stops_and_stations <- data.frame(
Type = rep(c("Bike", "Muni", "Bus", "BART"), each = 10),
stop_lon = -runif(40, 122.4200, 122.4500),
stop_lat = runif(40, 37.76800, 37.78900),
color = rep(c("Red", "Blue", "Green", "Yellow"), each = 10)
)
那么您可以使用 color
列而不是指定具体的颜色,例如 #ff6633
。
addCircleMarkers(lng = x$stop_lon,
lat = x$stop_lat,
radius= 3, color = x$color)
我还要指出,您的子集设置不正确:您使用的 checkboxGroupInput
可以有更多的值,因此您需要使用 %in%
运算符进行过滤。
x <- Stops_and_stations[Stops_and_stations$Type %in% input$Type,]
我使用 shiny 和 leaflet 在地图上添加了点。 每个点都是不同类型的交通选项。 我想通过颜色区分不同的类型,但无法弄清楚。 尝试使用 "if" 无效。 谢谢!
这是我的基本代码
library(leaflet)
ui <- fluidPage(
leafletOutput("map"),
headerPanel("Example"),
sidebarPanel(checkboxGroupInput(inputId = "Type", label = "Data
Layer",choices = c("Bike", "Muni", "Bus", "BART"), selected = "Bike")))
server <- function(input, output) {
output$map <- renderLeaflet({
rownumber = which(Stops_and_stations$Type == input$Type)
x <- Stops_and_stations[rownumber,]
leaflet(width = 1000, height = 500) %>%
addTiles() %>%
addCircleMarkers(lng = x$stop_lon,
lat = x$stop_lat,
radius= 3, color = '#ff6633') %>%
setView(lng = -122.4000,
lat = 37.79500,
zoom = 13)
})
}
shinyApp(ui, server)
这就是我试图添加的内容 .....
if(input$Type == "Bike"){
leaflet(width = 1000, height = 500) %>%
addTiles() %>%
addCircleMarkers(lng = x$stop_lon,
lat = x$stop_lat,
radius= 3, color = '#ff6633') %>%
setView(lng = -122.4000,
lat = 37.79500,
zoom = 13)
}
if(input$Type == "Muni"){
leaflet(width = 1000, height = 500) %>%
addTiles() %>%
addCircleMarkers(lng = x$stop_lon,
lat = x$stop_lat,
radius= 3, color = '#0033ff') %>%
setView(lng = -122.4000,
lat = 37.79500,
zoom = 13)
}
.....
如果您提供 Stops_and_stations
并使其成为 reproducible example..
为不同的组使用不同颜色的一种方法是将 color
列添加到 data.frame
:
由于我们不知道你的数据,我创建了一些随机数据集。
Stops_and_stations <- data.frame(
Type = rep(c("Bike", "Muni", "Bus", "BART"), each = 10),
stop_lon = -runif(40, 122.4200, 122.4500),
stop_lat = runif(40, 37.76800, 37.78900),
color = rep(c("Red", "Blue", "Green", "Yellow"), each = 10)
)
那么您可以使用 color
列而不是指定具体的颜色,例如 #ff6633
。
addCircleMarkers(lng = x$stop_lon,
lat = x$stop_lat,
radius= 3, color = x$color)
我还要指出,您的子集设置不正确:您使用的 checkboxGroupInput
可以有更多的值,因此您需要使用 %in%
运算符进行过滤。
x <- Stops_and_stations[Stops_and_stations$Type %in% input$Type,]