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

Categories

I'm a beginner in R and I try to make a dashboard with map and charts.
I use Shiny, Leaflet for R, ShinyDashBoard.
Now I want to display charts when someone click on a polygon on my Leaflet map. I try to follow this thread about a similar problem (R Plot matching selected polygon in leaflet map) but it doesn't work for me.
For the first step I try to display the "shape clicked" sentence when the polygon is clicked but nothing.
After this, I want to display piecharts and another charts.
In the example they load the data from .csv but I use store my data in a .geojson file.

Here my us-states.geojson:

{"type":"FeatureCollection", "features":[ {"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[-87.359296,35.00118],[-88.202745,34.995703],[-88.098683,34.891641],[-88.241084,33.796253],[-88.471115,31.895754],[-88.394438,30.367688],[-88.137022,30.318396],[-88.10416,30.499135],[-88.011052,30.685351],[-87.934375,30.657966],[-87.90699,30.411504],[-87.655051,30.247195],[-87.518128,30.280057],[-87.37025,30.427934],[-87.446927,30.510088],[-87.408589,30.674397],[-87.633143,30.86609],[-87.600282,30.997536],[-85.497137,30.997536],[-85.004212,31.003013],[-85.113751,31.27686],[-85.042551,31.539753],[-85.141136,31.840985],[-85.053504,32.01077],[-85.058981,32.13674],[-84.889196,32.262709],[-85.004212,32.322956],[-84.960397,32.421541],[-85.069935,32.580372],[-85.184951,32.859696],[-85.431413,34.124869],[-85.606675,34.984749],[-87.359296,35.00118]]]},"properties":{"name":"Alabama","density":94.65,"id":"01"}} ]}

And here my Shiny App:

## app.R ##
library(geojsonio)
library(shiny)
library(leaflet)
library(rgdal)
library(shinydashboard)

shape <- geojsonio::geojson_read("data/us-states.geojson", what="sp")

ui <- dashboardPage(
  dashboardHeader(title = "Title"),
  
  dashboardSidebar(
    sidebarMenu(
      menuItem("Map", tabName = "map", icon = icon("globe")),
      menuItem("Charts", tabName = "charts", icon = icon("bar-chart"))
    )
  ),
  dashboardBody(
    tabItems(
      tabItem(
        tabName = "map",
        fluidRow(
          title = "Title",
          width = "100",
          height = "100",
          leafletOutput(outputId = "map")
        ),    
      ),
      tabItem(tabName = "charts",
              h2("xxx")
      )
    )
  )
)

server <- function(input, output) {
  bins <- c(0, 10, 20, 50, 100, 500, 1000, Inf)
  pal <- colorBin("YlOrRd", domain = contour$density, bins = bins)
  rv <- reactiveValues()
  rv$myDf <- NULL
  
  shape$idd <- 1:nrow(shape)
  
  labels <- sprintf("<strong>%s</strong><br>%g people / mi",
                    shape$name, shape$density
  ) %>% lapply(htmltools::HTML)

  output$map <- renderLeaflet({
    leaflet() %>% 
      setView(lng = -61.57793975097654, lat = 16.246270497272032, zoom = 9)%>%
      addTiles() %>%
      #Polygons Layers
      addPolygons(data = shape,
                  layerId = ~idd,
                  popup = ~name,
                  fillColor = ~pal(density),
                  weight = 2,
                  opacity = 1,
                  color = "black",
                  dashArray = "0",
                  fillOpacity = 0.7,
                  highlight = highlightOptions(
                    weight = 5,
                    color = "black",
                    dashArray = "",
                    fillOpacity = 0.7,
                    bringToFront = TRUE),
                  label = labels,
                  labelOptions = labelOptions(
                    style = list("font-weight" = "normal", padding = "3px 8px"),
                    textsize = "15px",
                    direction = "auto"),
                  group = "group1")%>%
      addPolygons(data = shape,
                  fillOpacity = 0.2,
                  fillColor = "Green",
                  group = "group2")%>%

      addLegend(pal = pal, values = shape$density, opacity = 0.7, title = "Title", group = "group1",
                position = "bottomright")%>%

      addLayersControl(
        baseGroups = c("group1", "group2"),
        options = layersControlOptions(collapsed = FALSE)
      )
  })
  observeEvent(input$mymap_shape_click,{
    print("shape clicked")
    event <- input$mymap_shape_click

  })
}   
shinyApp(ui, server)

If anyone can explain me how to use reactiveValues when you store data in .geojson file or .json file, thank for the help.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.4k views
Welcome To Ask or Share your Answers For Others

1 Answer

等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...