小叶/有光泽:无法绘制反应多边形

时间:2016-11-25 08:54:05

标签: r shiny leaflet polygon

浏览了Shiny integration example on the Leaflet for R page后,我无法在我的闪亮应用中显示子集并显示一些多边形。

目前,我正在使用侧边栏获取应用程序,但主显示只是“错误:不知道如何从类对象中获取路径数据”

这个想法是从GB(3个国家/地区)中选择一个国家多边形并单独显示它,具体取决于下拉选项;

require(shiny)
require(rgdal)
require(rgeos)
require(leaflet)

cont <- readOGR(".\\mypath\\mypolygons.shp", "mypolygons", stringsAsFactors=FALSE)

ui <- fluidPage(

  titlePanel("My page"),

  sidebarLayout(
    sidebarPanel(
      selectInput("countryInput", "Country:", choices = c('England','Scotland','Wales'))
    ),
    mainPanel(
      leafletOutput("mymap")
    )
  )
)

server <- function(input, output, session) {

pols <- eventReactive(input$countryInput,{cont[substr(cont@data$code,1,1)==substr(input$countryInput,1,1),]})

  output$mymap <- renderLeaflet({
    leaflet() %>%
      addProviderTiles("CartoDB.Positron") %>%
      addPolygons(data = pols)
  })
}


shinyApp(ui, server)

错误:

Warning: Error in polygonData.default: Don't know how to get path data from object of class reactive
Stack trace (innermost first):
    83: polygonData.default
    82: polygonData
    81: derivePolygons
    80: addPolygons
    79: function_list[[k]]
    78: withVisible
    77: freduce
    76: _fseq
    75: eval
    74: eval
    73: withVisible
    72: %>%
    71: func [#6]
    70: output$mymap
     4: <Anonymous>
     3: do.call
     2: print.shiny.appobj
     1: <Promise>

1 个答案:

答案 0 :(得分:1)

修正:

首先我改变了;

pols <- eventReactive(input$countryInput,{cont[substr(cont@data$code,1,1)==substr(input$countryInput,1,1),]})

要;

pols <- reactive({cont.sim[substr(cont.sim@data$gssCode,1,1)==substr(input$countryInput,1,1),]

并且&#39; addPolygons行在变量名称&#39; pols&#39;

之后缺少打开/关闭括号
addPolygons(data = pols)

变为

addPolygons(data = pols())