Home > Back-end >  Plotting points on map returns "object of type 'closure' is not subsettable"
Plotting points on map returns "object of type 'closure' is not subsettable"

Time:11-16

I am new to Shiny. I have the following test data I want to plot:

      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

My code is the following:

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)

When I run this in RStudio, I get:

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

Am I passing test to server using the wrong reactive function?

CodePudding user response:

points is a reactive. To access it, you need to use function call syntax (because it is essentially a function):

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

Also, the initialisation of your reactive is weird: you’re creating a second local object, data. That probably wasn’t intended. Replace {data=test} with just test:

points <- reactive(test)
  • Related