Successfully reported this slideshow.
Your SlideShare is downloading. ×

{shiny}と{leaflet}による地図アプリ開発Tips

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad

Check these out next

1 of 33 Ad

More Related Content

Slideshows for you (20)

Similar to {shiny}と{leaflet}による地図アプリ開発Tips (20)

Advertisement

Recently uploaded (20)

Advertisement

{shiny}と{leaflet}による地図アプリ開発Tips

  1. 1. > me $name [1] "Takashi Kitano" $twitter [1] "@kashitan" $work_in [1] " " %>% " "
  2. 2. 
 

  3. 3. glimpse(choices) List of 4 $ : Named chr [1:58] "11101" "11102" "11103" "11104" ... ..- attr(*, "names")= chr [1:58] " " " " " " " " ... $ : Named chr [1:36] "12101" "12102" "12103" "12104" ... ..- attr(*, "names")= chr [1:36] " " " " " " " " ... $ : Named chr [1:51] "13101" "13102" "13103" "13104" ... ..- attr(*, "names")= chr [1:51] " " " " " " " " ...
  4. 4. names(choices) [1] " " " " " " " " choices[[" "]] "13101" "13102" "13103" "13104" "13105" "13106" "13107" "13108" "13109" "13110" "13111" "13112" "13113" "13114" "13115" "13116" "13117" "13118"
  5. 5. selectInput( inputId = "pref", label = " ", choices = names(choices), selected = " ", width = "100%" ) selectInput( inputId = "city", label = " ", # choices = choices$` `, selected = "13112", width = "100%" )
  6. 6. server <- function(input, output, session) { observe({ city_choices <- choices[[input$pref]] city <- input$city # # if (!(city %in% city_choices)) { city <- city_choices[1] } updateSelectInput( session, inputId = “city", choices = city_choices, selected = city ) }) }
  7. 7. # tags$style(type = "text/css", "#map {height: calc(90vh - 80px) ! important;}"), leafletOutput("map")
  8. 8. output$map <- leaflet::renderLeaflet({ leaflet::leaflet() %>% leaflet::addProviderTiles(providers$CartoDB.Positron) })
  9. 9. observe({ pal <- leaflet::colorNumeric("Spectral", dat.sub()$n, reverse = TRUE) map <- leaflet::leafletProxy("map", data = dat.sub()) %>% leaflet::clearShapes() %>% leaflet::clearControls() %>% leaflet::addPolygons(fillColor = ~ pal(n), color = "gray", popup = ~ NAME, weight = 1, fillOpacity = 0.8) %>% leaflet::addLegend("bottomright", pal = pal, values = ~ n, title = "Population", opacity = 1) map })
  10. 10. observe({ pal <- leaflet::colorNumeric("Spectral", dat.sub()$n, reverse = TRUE) map <- leaflet::leafletProxy("map", data = dat.sub()) %>% leaflet::clearShapes() %>% leaflet::clearControls() %>% leaflet::addPolygons(fillColor = ~ pal(n), color = "gray", popup = ~ NAME, weight = 1, fillOpacity = 0.8) %>% leaflet::addLegend("bottomright", pal = pal, values = ~ n, title = "Population", opacity = 1) map })
  11. 11. is.fitBounds <- FALSE observeEvent(input$city, { is.fitBounds <<- TRUE }) observe({ if (is.fitBounds) { # bbox <- dat.sub() %>% sf::st_bbox() names(bbox) <- NULL map <- map %>% leaflet::fitBounds(bbox[1], bbox[2], bbox[3], bbox[4]) is.fitBounds <<- FALSE } map
  12. 12. is.fitBounds <- FALSE observeEvent(input$city, { is.fitBounds <<- TRUE }) observe({ if (is.fitBounds) { # bbox <- dat.sub() %>% sf::st_bbox() names(bbox) <- NULL map <- map %>% leaflet::fitBounds(bbox[1], bbox[2], bbox[3], bbox[4]) is.fitBounds <<- FALSE } map
  13. 13. observeEvent(input$map_shape_click, { p <- input$map_shape_click # shp.sub <- shp[[input$pref]] %>% dplyr::filter(stringr::str_sub(KEY_CODE, 1, 5) %in% choices[[input$pref]]) # ind <- sf::st_point(c(p$lng, p$lat), dim = "XY") %>% sf::st_within(shp.sub) %>% unlist() })
  14. 14. observeEvent(input$map_shape_click, { p <- input$map_shape_click # shp.sub <- shp[[input$pref]] %>% dplyr::filter(stringr::str_sub(KEY_CODE, 1, 5) %in% choices[[input$pref]]) # ind <- sf::st_point(c(p$lng, p$lat), dim = "XY") %>% sf::st_within(shp.sub) %>% unlist() })

×