【问题标题】:Updating spatial polygon dataframe with shiny使用闪亮更新空间多边形数据框
【发布时间】:2021-02-16 17:59:50
【问题描述】:

我的 shapefile 有列 meanmediansd,我想在 R Shiny 中绘制等值线图。我有一个侧边栏,用于控制地图图块是否应显示meanmediansd。但我无法在 Shiny 中做到这一点。我尝试使用响应式函数,但我不断收到以下错误

Error: Polygon data not found; please provide addPolygons with data and/or lng/lat arguments

我的代码在下面

library(shiny)
library(leaflet)
library(rgdal)
library(RColorBrewer)

val <- readOGR('exampleshapefile.shp')
mybins <- c(24,270,470,555,770,2000,Inf)

ui <- fluidPage(
  sidebarLayout(
    
    sidebarPanel(
      radioButtons("stat", "Stat Type:",
                   c("Mean" = "mean",
                     "Median" = "q0_50",
                     "Standard Deviation" = "sd"
                   )
      )
      
    ),
    mainPanel("mainpanel",
              leafletOutput("distSAM")
              
    )
  )
)

server <- function(input, output) {
  
  ###################### dist-wise
  data <- eventReactive(input$stat,{
    val@data$input$stat
  })
  
  pal <- reactive({
    colorBin(palette="RdYlGn", domain = data(), na.color = "transparent", bins=mybins, reverse = TRUE)
  })
  
  labels <- reactive({
    sprintf(
      "<strong>%s<br/>%s</strong>%.1f",
      val@data[["NAME_2"]], "SAM: ", data()
    ) %>% lapply(htmltools::HTML)
  })
  
  output$distSAM <- renderLeaflet({
    df <- data()
    pal <- pal()
    lab <- labels()
    
    leaflet()  %>% addTiles() %>%
      addPolygons(data = df,
                  fillColor = ~pal(mean),
                  weight = 2,
                  opacity = 1,
                  color = "white",
                  dashArray = "3",
                  fillOpacity = 0.7,
                  highlight = highlightOptions(
                    weight = 5,
                    color = "#666",
                    dashArray = "",
                    fillOpacity = 0.7,
                    bringToFront = TRUE),
                  label = lab,
                  labelOptions = labelOptions(
                    style = list("font-weight" = "normal", padding = "3px 8px"),
                    textsize = "15px",
                    direction = "auto")) %>%
      addLegend(pal = pal, values = ~df$mean,
                title = "SAM </br> Prevalence",
                position = "bottomleft")
    
  }
  )
}

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny leaflet


    【解决方案1】:

    val@data$input$stat 不是有效的数据选择。相反,您可以使用:

    selected_stat <- val[[input$stat]]
    

    【讨论】:

      猜你喜欢
      • 2017-08-20
      • 2018-08-01
      • 2019-02-22
      • 2018-12-13
      • 1970-01-01
      • 2016-12-04
      • 2017-03-07
      • 2019-02-16
      • 1970-01-01
      相关资源
      最近更新 更多