我试图在同一个Rshiny图中包含两种不同的标记。第一个标记(addCircleMarkers)工作得很好但是当我包含第二个标记(addMarkers)时,我收到错误消息
我的数据框:
Type Location.ID Location_discription Lat Long Flood.path FP_Icon Sample Structure.2
1 GBS_S ALB Albany, OR 44.62054 -123.1039 Yes greenleafIcon A1 Blue
2 GBS_S ALB Albany, OR 44.62054 -123.1039 Yes greenleafIcon A3 Blue
3 GBS_S ALB Albany, OR 44.62054 -123.1039 Yes greenleafIcon A4 Blue
4 GBS_S ALB Albany, OR 44.62054 -123.1039 Yes greenleafIcon A5 Blue
5 GBS_S ANG Angels' rest, Columbia Gorge, OR 45.56383 -122.1523 Yes greenleafIcon ANG1 Blue
6 GBS_S ANG Angels' rest, Columbia Gorge, OR 45.56383 -122.1523 Yes greenleafIcon ANG1b Blue
Structure.3
1 Green
2 Green
3 Green
4 Green
5 Green
6 Green
我的ui.R
library(shiny)
library(leaflet)
shinyUI(fluidPage(
# Application title
titlePanel("Aspen GBS Population Structure results on map"),
# Side bar layout
sidebarLayout(
sidebarPanel(
selectInput("structure", label = "Select K for display", choices = c("2", "3", "4", "5", "6"), selected = "2"),
radioButtons("origin", label = "Nature of the sample", choices = c("Flood", "Non Flood"), selected = "Flood")),
mainPanel(
leafletOutput("map")
)
)
)
)
我的服务器.R
greenleafIcon <- makeIcon(
iconUrl = "http://leafletjs.com/docs/images/leaf-green.png",
iconWidth = 20, iconHeight = 50,
iconAnchorX = 12, iconAnchorY = 84,
shadowUrl = "http://leafletjs.com/docs/images/leaf-shadow.png",
shadowWidth = 40, shadowHeight = 54,
shadowAnchorX = 3, shadowAnchorY = 52
)
redleafIcon <- makeIcon(
iconUrl = "http://leafletjs.com/docs/images/leaf-red.png",
iconWidth = 20, iconHeight = 50,
iconAnchorX = 12, iconAnchorY = 84,
shadowUrl = "http://leafletjs.com/docs/images/leaf-shadow.png",
shadowWidth = 40, shadowHeight = 54,
shadowAnchorX = 3, shadowAnchorY = 52
)
library(shiny)
shinyServer(function(input, output) {
dt <- reactive(
switch(input$structure,
"2" = data_K2$Structure.2,
"3" = data_K2$Structure.3))
dt2 <- reactive(
switch (input$origin,
"Flood" = data_K2$FP_Icon,
"Non Flood" = data_K2$FP_Icon))
output$map <- renderLeaflet(
leaflet(data = data_K2) %>% addTiles() %>% setView(lng = -106.1039361,lat = 50.543981, zoom = 4) %>%
addCircleMarkers(lat = ~Lat, lng = ~Long, popup = ~Location_discription, radius=2, color = ~dt(), fill = TRUE) %>%
addMarkers(lat = ~Lat, lng = ~Long, popup = ~Location_discription, icon = ~dt2())
)
})
我收到的错误消息是 - Error in icon$iconUrl : $ operator is invalid for atomic vectors
我在哪里做错了?
答案 0 :(得分:1)
问题在于:
addMarkers(lat = ~Lat, lng = ~Long, popup = ~Location_discription, icon = ~dt2())
特别是~dt2()
。这将计算为data_K2$FP_Icon
,这是一个原子向量。矢量不能用$
进行子集化。 addMarkers
的icon参数需要一个具有子集iconUrl
的对象,因此错误。
我认为你想要的是:
leafIcons <- icons(
iconUrl = ifelse(data_K2$FP_Icon == "greenleafIcon",
"http://leafletjs.com/docs/images/leaf-green.png",
"http://leafletjs.com/docs/images/leaf-red.png"
),
iconWidth = 38, iconHeight = 95,
iconAnchorX = 22, iconAnchorY = 94,
shadowUrl = "http://leafletjs.com/docs/images/leaf-shadow.png",
shadowWidth = 50, shadowHeight = 64,
shadowAnchorX = 4, shadowAnchorY = 62
)
然后
addMarkers(lat = ~Lat, lng = ~Long, popup = ~Location_discription, icon = leafIcons
这里有非常有用且清晰的文档:https://rstudio.github.io/leaflet/markers.html