基于闪亮的小部件输入将静态传单线图转换为动态传单线图

时间:2018-06-26 08:20:14

标签: r shiny leaflet geosphere

这是结合了leafletgeosphere库的静态流程图。

enter image description here

        # My data : 

        yearam =c('1751', '1751', '1744', '1713', '1681', '1775', '1754', '1776', '1812', '1821')
        lng_from =c(-1.55000, -1.55000,  -0.11667,  -0.11667,  -0.11667, -43.23333,  -4.13861,  -3.00000, -38.51083, -34.88111)
        lat_from =c(47.21667, 47.21667,  51.50000,  51.50000,  51.50000, -22.90000,  50.39639,  53.41667, -12.97111,  -8.05389)
        lng_to =c(3.05000, 3.05000,  8.31667,  8.31667,  8.31667, 13.40722, -1.11670,  9.89337,  0.00000, 36.88833)
        lat_to =c(6.41667,  6.41667,   4.95000,   4.95000,   4.95000, -12.57833,   5.16670,   0.20739,   0.00000, -17.87861)

        routes <- data.frame(yearam, lng_from, lat_from, lng_to, lat_to)

        # Wrangling data :

        library(dplyr)
        library(leaflet)
        library(geosphere) 

        # counting (I want to display  lines width  based on n value subsequently)
        routes1 <- routes %>%
            count(lng_from, lat_from, lng_to, lat_to) 

       # converting my data with geosphere package to avoid polylines paths :  

          source <-routes1[, c("lng_from", "lat_from")]
          target <- routes1[, c("lng_to", "lat_to")]   

          geo_lines <- gcIntermediate(source,
                                      target,
                                      sp = TRUE,
                                      addStartEnd = TRUE, 
                                      n = 50)


          # my map :

          leaflet("map") %>%
            addTiles() %>%
            addPolylines(data = geo_lines, color = "#ff0000", weight = 2) 

我想使代码适应闪亮的上下文,以允许用户根据yearam列过滤数据。

我尝试过的:

library(shiny)
library(leaflet)
library(geosphere)

yearam =c(1751, 1751, 1744, 1713, 1681, 1775, 1754, 1776, 1812, 1821)
lng_from =c(-1.55000, -1.55000,  -0.11667,  -0.11667,  -0.11667, -43.23333,  -4.13861,  -3.00000, -38.51083, -34.88111)
lat_from =c(47.21667, 47.21667,  51.50000,  51.50000,  51.50000, -22.90000,  50.39639,  53.41667, -12.97111,  -8.05389)
 lng_to =c(3.05000, 3.05000,  8.31667,  8.31667,  8.31667, 13.40722, -1.11670,  9.89337,  0.00000, 36.88833)
 lat_to =c(6.41667,  6.41667,   4.95000,   4.95000,   4.95000, -12.57833,   5.16670,   0.20739,   0.00000, -17.87861)

 routes <- data.frame(yearam, lng_from, lat_from, lng_to, lat_to,  stringsAsFactors = FALSE)


ui <- bootstrapPage(

# Displaying my map :
     leafletOutput("map", width = "100%", height = "100%"),

      # Displaying year slider (id = y) : 
        absolutePanel(top = 10, right = 10,
                      style="z-index:500;",
                      sliderInput("y", "Chronology",
                                  min(routes$yearam),
                                  max(routes$yearam),
                                  value = range(routes$yearam),
                                  step = 1,
                                  sep = ""
                              )
                )
   )          


   server <- function(input, output, session) {

         # reactive filtering data from UI

                # NOT WORKING

         routes1 <- reactive({routes %>%
                    filter(yearam >= input$y[1] & yearam <= input$y[2])
                    count(lng_from, lat_from, lng_to, lat_to) })


          source <-reactive({routes1[, c("lng_from", "lat_from")] })
          target <- reactive({routes1[, c("lng_to", "lat_to")] })   

          geo_lines <- reactive({gcIntermediate(source(),
                                            target(),
                                            sp = TRUE,
                                            addStartEnd = TRUE, 
                                            n = 50)
                })



           # static backround map

          output$map <- renderLeaflet({
                  leaflet() %>%
                  addTiles() 
               })  

          # reactive  map
                   observe({
                   leafletProxy("map") %>%
                   addPolylines(data = geo_lines(), color = "#9e9ac8", weight = 2) 
                 })


   }                


              shinyApp(ui, server)

0 个答案:

没有答案