【问题标题】:Plotting points on map returns "object of type 'closure' is not subsettable"地图上的绘图点返回“'closure'类型的对象不是子集”
【发布时间】:2021-12-26 20:39:27
【问题描述】:

我是 Shiny 的新手。我想绘制以下test 数据:

      name     lat      long
1     AN-02M25 51.95509 -0.960327
2     AN-03M02 52.01291 -0.925606
3     AN-04M04 52.13251 -0.957313
4    AN-05268A 52.10275 -0.812983
5     AN-05M02 52.07297 -0.807966

我的代码如下:

ui <- fluidPage(
  actionButton("recalc", "New points"),
  mainPanel(
    tabsetPanel(
      tabPanel("Order Locations", leafletOutput("map",width="80%",height="400px")),
      tabPanel("Markers", verbatimTextOutput("markers"))
    )
  )
)

server <- function(input, output, session) {
  
  points <- reactive({data=test})
  
  output$map <- renderLeaflet({
    leaflet() %>%
      addTiles() %>%
      addMarkers(lng=points$long,lat=points$lat)
  })
  output$markers <- renderPrint({print(points)})
}

shinyApp(ui, server)

当我在 RStudio 中运行它时,我得到:

Warning: Error in $: object of type 'closure' is not subsettable
  [No stack trace available]

我是否使用错误的反应函数将test 传递给server

【问题讨论】:

    标签: r dataframe shiny r-leaflet


    【解决方案1】:

    pointsreactive。要访问它,您需要使用函数调用语法(因为它本质上是一个函数):

    output$map <- renderLeaflet({
      leaflet() %>%
        addTiles() %>%
        addMarkers(lng = points()$long, lat = points()$lat)
    })
    output$markers <- renderPrint({print(points())})
    

    此外,您的响应式初始化很奇怪:您正在创建第二个本地对象 data。那可能不是故意的。将{data=test} 替换为test

    points <- reactive(test)
    

    【讨论】:

    • 谢谢。不过,错误消息有点神秘。
    • @Zizzipupp 确实是这样(众所周知,此时它是一个模因)。但是现在你已经遇到了它,你可以更普遍地识别它:这个消息实际上总是意味着你要么在函数调用/反应值之后忘记了(),要么你正在访问一个函数而不是一个变量(由于拼写错误,或者因为您忘记在当前范围内初始化变量)。
    猜你喜欢
    • 2014-08-01
    • 2016-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-16
    • 2017-06-01
    • 1970-01-01
    • 2015-06-18
    相关资源
    最近更新 更多