Почему точки на карте «leaflet» не реагируют на «sliderInput»? - PullRequest
0 голосов
/ 01 мая 2019

У меня есть карта leaflet с точками, которые я хочу отреагировать на sliderInput. Я использую Shiny. Кажется, проблема в части кода server, но я не могу понять, в чем проблема. Что мне здесь не хватает?

Это то, что я сделал ...

Пример данных

> dput(df)
structure(list(DATA = c("16/10/1987", "01/05/2003", "18/06/1987", 
"30/12/2001", "12/11/1991", "16/08/2002", "07/05/1992", "24/01/1996", 
"23/01/2003", "27/11/2002", "05/03/1996", "04/03/1986", "02/10/1992", 
"02/10/1992", "16/04/1998", "22/12/2015", "30/10/1996", "17/01/2018", 
"29/12/2012", "17/10/2007", "25/07/2015", "10/09/2015", "07/02/2003", 
"21/05/2008", "06/02/2003", "18/05/2011", "07/02/2003", "21/05/2008", 
"07/02/2003", "17/12/1999", "07/02/2003", "21/05/2008", "01/06/1999", 
"25/07/2015", "25/07/2015", "01/08/2012", "06/12/2017", "26/04/2018", 
"03/05/2018", "26/04/2018", "31/05/2016", "24/01/2012", "03/04/2008", 
"28/12/2007", "17/07/2015", "27/05/2009", "06/12/2017", "25/05/2015", 
"25/07/2015", "25/07/2015", "25/07/2015", "31/05/2016", "30/10/2015", 
"21/10/2015", "28/12/2007", "31/05/2016", "13/11/2017", "17/07/2015", 
"13/11/2017", "31/05/2016", "19/09/2013", "20/12/2017", "23/09/2014", 
"30/01/2015", "30/10/2015", "20/12/2017", "20/12/2017", "25/05/2015"
), LAT = c(-23.557659, -23.592218, -23.591597, -23.585119, -23.586508, 
-23.590983, -23.572339, -23.572116, -23.568473, -23.57741, -23.583074, 
-23.57572, -23.595178, -23.575972, -23.582423, -23.60028, -23.603026, 
-23.59812, -23.61295, -23.597057, -23.587932, -23.597373, -23.582405, 
-23.59963, -23.58241, -23.600301, -23.5824, -23.600301, -23.582405, 
-23.600301, -23.582405, -23.600301, -23.575351, -23.59544, -23.591615, 
-23.580784, -23.561284, -23.609304, -23.589302, -23.606689, -23.594632, 
-23.592094, -23.598217, -23.598217, -23.577526, -23.602157, -23.584508, 
-23.593796, -23.605709, -23.587303, -23.612873, -23.608257, -23.608956, 
-23.586778, -23.598217, -23.599373, -23.579991, -23.56737, -23.60463, 
-23.591894, -23.586626, -23.583842, -23.593201, -23.579477, -23.581758, 
-23.593199, -23.585853, -23.568539), LON = c(-46.759414, -46.783406, 
-46.754998, -46.804457, -46.800644, -46.744225, -46.747706, -46.751357, 
-46.685479, -46.765616, -46.792158, -46.775386, -46.790114, -46.752669, 
-46.757516, -46.747442, -46.780426, -46.736658, -46.695209, -46.775849, 
-46.722389, -46.752566, -46.750786, -46.788074, -46.750791, -46.78911, 
-46.750786, -46.78911, -46.750786, -46.78911, -46.750786, -46.78911, 
-46.716556, -46.754709, -46.798076, -46.758347, -46.712058, -46.719745, 
-46.725999, -46.725028, -46.729975, -46.75271, -46.781338, -46.781338, 
-46.736328, -46.750617, -46.739825, -46.732154, -46.726389, -46.72504, 
-46.72122, -46.72336, -46.678182, -46.789662, -46.781338, -46.796804, 
-46.778211, -46.745922, -46.724957, -46.789791, -46.761014, -46.788742, 
-46.753462, -46.767823, -46.752019, -46.753528, -46.758925, -46.763161
), DATA_CLEAN = structure(c(6497, 12173, 6377, 11686, 7985, 11915, 
8162, 9519, 12075, 12018, 9560, 5906, 8310, 8310, 10332, 16791, 
9799, 17548, 15703, 13803, 16641, 16688, 12090, 14020, 12089, 
15112, 12090, 14020, 12090, 10942, 12090, 14020, 10743, 16641, 
16641, 15553, 17506, 17647, 17654, 17647, 16952, 15363, 13972, 
13875, 16633, 14391, 17506, 16580, 16641, 16641, 16641, 16952, 
16738, 16729, 13875, 16952, 17483, 16633, 17483, 16952, 15967, 
17520, 16336, 16465, 16738, 17520, 17520, 16580), class = "Date"), 
    ANO = c(1987, 2003, 1987, 2001, 1991, 2002, 1992, 1996, 2003, 
    2002, 1996, 1986, 1992, 1992, 1998, 2015, 1996, 2018, 2012, 
    2007, 2015, 2015, 2003, 2008, 2003, 2011, 2003, 2008, 2003, 
    1999, 2003, 2008, 1999, 2015, 2015, 2012, 2017, 2018, 2018, 
    2018, 2016, 2012, 2008, 2007, 2015, 2009, 2017, 2015, 2015, 
    2015, 2015, 2016, 2015, 2015, 2007, 2016, 2017, 2015, 2017, 
    2016, 2013, 2017, 2014, 2015, 2015, 2017, 2017, 2015)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -68L), na.action = structure(c(`19` = 19L, 
`21` = 21L, `22` = 22L, `23` = 23L, `24` = 24L, `25` = 25L, `26` = 26L, 
`27` = 27L, `28` = 28L, `29` = 29L, `30` = 30L, `32` = 32L, `34` = 34L, 
`35` = 35L, `50` = 50L, `52` = 52L, `57` = 57L, `58` = 58L, `60` = 60L, 
`63` = 63L, `64` = 64L, `66` = 66L, `67` = 67L, `68` = 68L, `69` = 69L, 
`73` = 73L, `80` = 80L, `81` = 81L, `86` = 86L, `90` = 90L, `91` = 91L, 
`92` = 92L), class = "omit"))

Подготовка данных

library(tidyverse)
library(lubridate)
library(leaflet)
library(shiny)
library(xts)
library(dplyr)
library(sp)
library(assertthat)
library(crosstalk)
library(cellranger)
library(colorspace)
library(broom)

df <- df %>% 
  select(DATA = DT_CRIACAO, LAT = LATITUDE, LON = LONGITUDE)

converter_mes <- function(x){
  nomes <- c("jan", "fev", "mar", "abr", "mai", "jun",
             "jul", "ago", "set", "out", "nov", "dez")

  numeros <- str_pad(1:12, width = 2, pad = "0")

  x <- str_replace_all(x, nomes[1], numeros[1])
  x <- str_replace_all(x, nomes[2], numeros[2])
  x <- str_replace_all(x, nomes[3], numeros[3])
  x <- str_replace_all(x, nomes[4], numeros[4])
  x <- str_replace_all(x, nomes[5], numeros[5])
  x <- str_replace_all(x, nomes[6], numeros[6])
  x <- str_replace_all(x, nomes[7], numeros[7])
  x <- str_replace_all(x, nomes[8], numeros[8])
  x <- str_replace_all(x, nomes[9], numeros[9])
  x <- str_replace_all(x, nomes[10], numeros[10])
  x <- str_replace_all(x, nomes[11], numeros[11])
  x <- str_replace_all(x, nomes[12], numeros[12])

  x

}

df <- df %>% 
  mutate(DATA_CLEAN = dmy(converter_mes(DATA)),
         LAT = LAT/1e6,
         LON = LON/1e6
  ) %>% 
  mutate(ANO = year(DATA_CLEAN)) %>% 
  # remover linhas onde LAT ou LON ? NA
  na.omit()

summary(df)

df %>% 
  count(ANO) %>% 
  ggplot(aes(x = ANO, y = n)) + 
  geom_col(fill = "darkorange1") + 
  theme_minimal() +
  labs(x = NULL, y = NULL,
       title = "Quantidade de escolas fundadas por ano em Sao Paulo") +
  scale_x_continuous(breaks = scales::pretty_breaks(n = 10))

praca_se <- leaflet() %>%
  addTiles() %>%  
  addCircles(lng=-46.634123, lat=-23.548408,
             popup="Praca da Se")

all_points <- leaflet(df) %>%
  addTiles() %>%  
  addCircles(lng= df$LON, lat= df$LAT,
             popup="Praca da Se")

date <- df$ANO

Блестящий

ui

ui <- fluidPage(
  sliderInput("obs", "Year", min(date), 
              max(date),
              value = max(date),
              step=1,
              animate=T),
  leafletOutput("all_points")
)

server

server <- function(input, output, session) {
  obs <- reactive({
    df %>% 
      filter(date==input$obs)
  })

    output$all_points <- renderLeaflet({
    leaflet(df) %>%
      addTiles() %>%  
      addCircles(lng= df$LON, lat= df$LAT,
                 radius = .005,
                 color= "black",
                 stroke = F, 
                 fillOpacity = 10)
  })
}

shinyApp(ui, server)

Спасибо за ваше время.

1 Ответ

0 голосов
/ 02 мая 2019

Вы reactive и renderLeaflet оба берете df в качестве ввода, и поскольку это статические данные, листовка не будет обновляться.

Вы фильтруете данные в реактиве, который называется obs, поэтому в опции renderLeaflet вы должны получить данные из этого реактива. Поэтому вставка этой строки перед обработкой листовки должна заставить ее работать:

df <- obs()

Полный код сервера:

server <- function(input, output, session) {
  obs <- reactive({
    df %>% 
      filter(date==input$obs)
  })

  output$all_points <- renderLeaflet({
    df <- obs()

    leaflet(df) %>%
      addTiles() %>%  
      addCircles(lng= df$LON, lat= df$LAT,
                 radius = .005,
                 color= "black",
                 stroke = F, 
                 fillOpacity = 10)
  })
}
...