我想根据选择作为输入的年份来更改数据以绘制在shapefile(世界)上。根据您的说法,哪个更明智:
将数据设置为宽(data_2020,data_2019,data_2018 ...)并在输入更改时更改列? -或-
将数据设置为long(数据,年份),并在输入更改时根据year == 20XX过滤数据列?
我已经尝试了好几天,但无法使其正常工作。下面的示例是我的server.R
文件,其中数据已设置为长格式,并且我选择了一年以使反应性正常工作,但它不会更新数据。宽格式不适用于(带注释的)开关功能。
function(input, output) {
selectedYear <- reactive({
#shape_data <- subset(shape_data, year == input$year)
shape_data$month
#datayear <- switch(input$year,
# "2017" = shape_data$month2017,
# "2018" = shape_data$month2018,
# "2019" = shape_data$month2019)
})
output$mymap <- renderLeaflet({
bins <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
pal <- colorBin(c("#3EA055", "#FFE5B4", "#F2BB66", "#FFDB58", "#FBB117", "#C68E17", "#E56717", "#C34A2C", "#C11B17", "#9F000F", "#8C001A" , "#7E3517"), domain = selectedYear(), bins = bins, na.color = "#808080")
leaflet(shape_data) %>%
setView(0, 37.8, 2) %>%
addTiles() %>%
addPolygons(fillColor = ~pal(selectedYear()),
weight = 2,
opacity = 1,
color = "black",
fillOpacity = 0.7)
})
observe({
req(input$year)
bins <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
pal <- colorBin(c("#3EA055", "#FFE5B4", "#F2BB66", "#FFDB58", "#FBB117", "#C68E17", "#E56717", "#C34A2C", "#C11B17", "#9F000F", "#8C001A" , "#7E3517"), domain = selectedYear(), bins = bins, na.color = "#808080")
leafletProxy("mymap", data = shape_data) %>%
clearShapes() %>%
addPolygons(fillColor = ~pal(selectedYear()),
weight = 2,
opacity = 1,
color = "black",
fillOpacity = 0.7)
})
编辑:拼写
答案 0 :(得分:0)
您的声明
shape_data <- subset(shape_data, year == input$year)
选择第一年后,更改shape_data
。请更改为
shape_data2 <- subset(shape_data, year == input$year)