我有一个数据集,其中包含经度和纬度信息,以及每个经度和纬度的“实验”信息。
我使用 addCircleMarkers 来添加标记。我希望每个标记都有弹出选项。我尝试在弹出窗口中调用“实验”,但它给了我错误,指出它找不到这个变量。我不确定哪里出了问题。
ui <- fluidPage(
titlePanel("Land Explorer"),
selectInput("year", "Year of G2F", choices = c("2014", "2015", "2016", "All")),
fluidRow(
mainPanel(
tags$style(type = "text/css", "#map {height: calc(100vh - 80px) !important;}"),
leafletOutput("map"), width = "100%", height = 1000)
)
)
server <- function(input, output){
pal <- colorFactor("RdYlGn", county_simplified$zone, reverse = T, na.color = "white")
state_popup <- paste0("<strong>State: </strong>",
county_simplified$region,
"<br><strong>County: </strong>",
county_simplified$subregion)
g2f <- reactive ({
if (input$year == "2014"){
g2f_latlong = g2f_latlong[g2f_latlong$year == 2014, ]
}
else if (input$year == "2015"){
g2f_latlong = g2f_latlong[g2f_latlong$year == 2015, ]
}
else if (input$year == "2016") {
g2f_latlong = g2f_latlong[g2f_latlong$year == 2016, ]
}
else if (input$year == "All"){
g2f_latlong = g2f_latlong
}
else (g2f_latlong = g2f_latlong)
})
output$map <- renderLeaflet({
leaflet() %>%
addTiles() %>%
addPolygons(data = county_simplified, weight = 1,
color = "white",
dashArray = '3',
fillOpacity = 0.7,
fillColor = ~pal(zone),
popup = state_popup) %>%
addPolygons(data = state_boundry, weight = 1, color = "grey", fill = NA) %>%
addLegend(pal = pal, value = county_simplified$zone, position = "topright", title = "Zones") %>%
addCircleMarkers(data = g2f(), lng = ~long, lat = ~lat, popup = ~Experiment, radius = 1)
})
}
shinyApp(ui = ui, server = server)
您没有从
reactive
环境返回过滤后的数据,因此您的 addCircleMarkers
函数无法获取要在传单上绘制的数据。如下所示更改您的 reactive
环境,我希望这可以解决您的问题。
g2f <- reactive({
if (input$year == "2014") {
filtered_data <- g2f_latlong[g2f_latlong$year == 2014, ]
}else if (input$year == "2015") {
filtered_data <- g2f_latlong[g2f_latlong$year == 2015, ]
}else if (input$year == "2016") {
filtered_data <- g2f_latlong[g2f_latlong$year == 2016, ]
}else if (input$year == "All") {
filtered_data <- g2f_latlong
}else {filtered_data <- g2f_latlong}
return(filtered_data)
})