Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
764 views
in Technique[技术] by (71.8m points)

r - shiny leaflet ploygon click event

I have a map called business which is downloaded from natural earth website. What I am doing here is I created a basic Map output which shows the map. I am mainly using two columns which are admin(which is the name of the country) and economy here. Then I added a drop down list called Business under ui so that when I click the polygon of the country the list would refresh and show the country that I am clicking. I assume when I write p <- input$Map_shape_click shiny would know p is a business object so it has column admin and I have reference this admin ID to refresh my Business drop down list. But it doesn't work.The link shows what I am seeing - the list wouldn't refresh when I click a different country.

enter image description here

server.r

country <- readOGR(dsn = tmp, layer = "ne_110m_admin_0_countries", encoding   = "UTF-8")
business<-country[country@data$admin %in% c("Brazil","Colombia","Panama","Kazakhstan","Argentina","India","","Chile","Dominican Republic","United Kingdom","El Salvador","United States of America"),]
business@data$category <- factor(sample.int(20L, nrow(business@data), FALSE))


shinyServer(function(input, output,session) {

output$Map <- renderLeaflet({
  factpal <- colorFactor(topo.colors(20), business@data$category)
  state_popup <- paste0("<strong>Name of the country </strong>", 
                        business$admin, 
                        "<br><strong> information is  </strong>", 
                        business$economy)
    leaflet() %>%
    addProviderTiles("CartoDB.Positron") %>%
    addPolygons(data=business,
                layerId=~admin,
                fillColor= ~factpal(category),
                fillOpacity = 0.7, 
                color = "#BDBDC3", 
                weight = 1, 
                popup = state_popup,
                highlight = highlightOptions(
                weight = 5,
                color = "#666",
                dashArray = "",
                fillOpacity = 0.7,
                bringToFront = TRUE))})


observeEvent(input$Map_shape_click, { # update the location selectInput on map clicks
  p <- input$Map_shape_click
  if(!is.null(p$admin)){
    if(is.null(input$Business) || input$Business!=p$admin) updateSelectInput(session, "Business", selected=p$admin)
  }
})
}
)

ui.r

navbarPage("Market Portal",
tabPanel("About",
           bootstrapPage(
             leafletOutput("Map",width="100%",height="800px"),
             absolutePanel(top=100, right=50,
             selectInput("Business", "Business", c("Brazil","Colombia","Panama","Kazakhstan","Argentina","India","Chile","Dominican Republic","United Kingdom","El Salvador","United States of America"), selected="")
))))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The click event in leaflet returns lat, lng and id (and a random value). So you can only access one of those elements.

The id value relates to the layerId you specify in the shape plotting function, so in your case that's layerId=~admin.

So you acccess the admin value through the click's id field

replace p$admin with p$id and you should have your solution.


If you want to see what's in the click event, just put a print statement around it

observeEvent(input$Map_shape_click, { # update the location selectInput on map clicks
  p <- input$Map_shape_click
  print(p)
})

and it will print the object to the console.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...