Weitere ähnliche Inhalte Ähnlich wie {shiny}と{leaflet}による地図アプリ開発Tips (20) Mehr von Takashi Kitano (14) Kürzlich hochgeladen (20) {shiny}と{leaflet}による地図アプリ開発Tips9. 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] " " " " " " "
" ...
10. names(choices)
[1] " " " " " " " "
choices[[" "]]
"13101" "13102" "13103" "13104" "13105" "13106"
"13107" "13108" "13109"
"13110" "13111" "13112" "13113" "13114" "13115"
"13116" "13117" "13118"
11. selectInput(
inputId = "pref",
label = " ",
choices = names(choices),
selected = " ",
width = "100%"
)
selectInput(
inputId = "city",
label = " ",
#
choices = choices$` `,
selected = "13112",
width = "100%"
)
12. 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
)
})
}
18. 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
})
19. 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
})
23. 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
24. 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
28. 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()
})
29. 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()
})