在闪亮的反应函数中使用SpatialPolygonDataFrame时出错。

时间:2015-10-15 09:07:34

标签: shiny leaflet rgdal

我一直在收到错误

Error in states() <- reactive({ : invalid (NULL) left side of assignment

当我创建反应函数时,返回具有类SpatialPolygonsDataFrame的对象。我做了一个例子来证明这一点;

我可以单独在传单中进行子集,它可以工作。和,

我可以将整个对象放在有光泽的传单中。

但是如果你把它放在一个反应​​函数中它会给出错误。如何将这些空间物体在光泽中进行子集化以传递到闪亮的传单?

library(leaflet)
library(sp)
library(rgeos)
library(rgdal)
library(shiny)

# From https://www.census.gov/geo/maps-data/data/cbf/cbf_state.html
# Get the 20m file, unzip and put in your working directory.
states <- readOGR("cb_2014_us_state_20m.shp",
                  layer = "cb_2014_us_state_20m", verbose = FALSE)

neStates <- subset(states, states$STUSPS %in% c(
  "CT","ME","MA","NH","RI","VT","NY","NJ","PA"
))

leaflet(neStates) %>%
  addPolygons(
    stroke = FALSE, fillOpacity = 0.5, smoothFactor = 0.5,
    color = ~colorQuantile("YlOrRd", states$AWATER)(AWATER)
  )

ui <- shinyUI(bootstrapPage(
  tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
  leafletOutput("map", width = "100%", height = "100%"),
  absolutePanel(class = "panel panel-default", 
                top = 1, right = 1, width = 300,
  selectInput("which_state","which state", 
              c("CT","ME","MA","NH","RI","VT","NY","NJ","PA"))
)))

#input <- list(which_state = "CT")
server <- function(input, output) {
  states() <- reactive({
    subset(neStates,neStates$STUSPS == input$which_state)
  })

  output$map <- renderLeaflet({
    leaflet() %>%
    addPolygons(states())
    #addPolygons(data = neStates)
  })
}

shinyApp(ui,server)

1 个答案:

答案 0 :(得分:0)

anything() <- 2语法不正确,因此出错。你不能给看起来像函数调用的东西(没有参数)。

在这两个地方你可能都需要states而不是states()