在R中创建有光泽的传单单张地图

时间:2018-06-25 09:55:08

标签: r shiny leaflet r-leaflet

我用传单创建了一个闪亮的应用程序,效果很好。

library(shiny)
library(shinythemes)
library(leaflet)

ui2 <- fluidPage(theme = shinytheme("united"), tabsetPanel(

  tabPanel(
           titlePanel("titel"),

           mainPanel(

             leafletOutput(outputId = "mymap")),

           sidebarPanel(
             fluidRow(

               dateRangeInput("a", h4("date"),language = "en",separator = " to "),
               selectInput("select", h4("location"),
                           c(data8$city)),
               submitButton("search"))
           ))
  )
)

server <- function(input, output) {

  popupa <- paste(titel)

  output$mymap <- renderLeaflet({
    leaflet(data8) %>%
      addTiles() %>%
      addMarkers(lng = ~lng, lat = ~lat, popup = popupa)
  }) 
}
shinyApp(ui2, server)

但是目前,我想在显示的位置上添加一个dateRangeInput来过滤(date_start)。但是我不知道如何将我的dateRangeInput和selectInput连接到服务器部分的我的传单功能。此外,在地图下方,应该有一张表格,其中包含从地图中过滤出的位置-可以吗?

我使用的数据框如下:

title=c("Event1","Event2")
lng=c(23.3, 23.3)
lat=c(30, 40)
city=c("Berlin", "Hamburg" )
zip=c(39282, 27373)
date_start=c("2018-05-28","2018-05-28")
date_end=c("2018-06-27","2018-08-03")
data8 <- data.frame(title, lng, lat, city, zip, date_start, date_end)

有人知道如何做到这一点吗?感谢您的帮助! 问候

1 个答案:

答案 0 :(得分:2)

您可以尝试以下方法:

ui2 <- fluidPage(theme = shinytheme("united"), tabsetPanel(

  tabPanel(
    titlePanel("titel"),

    mainPanel(

      leafletOutput(outputId = "mymap"),
      dataTableOutput("mytable")),

    sidebarPanel(
      fluidRow(

        dateRangeInput("a", h4("date"),language = "en",separator = " to "),
        selectInput("selectLoc", h4("location"),
                    as.character(data8$city)),
        submitButton("search"))
    ))
)
)

server <- function(input, output) {

  popupa <- paste("titel")

  datatoPlot <- reactive({


    date_start <- as.character(input$a[1])
    date_end <- as.character(input$a[2])

    data8$date_start <- as.Date(data8$date_start, format = "%Y-%m-%d")
    data8 <- data8[as.Date(data8$date_start) >= date_start & as.Date(data8$date_start) <= date_end, ]
    data8 <- data8 %>% dplyr::filter(city == input$selectLoc)

  })

  output$mymap <- renderLeaflet({
    leaflet(datatoPlot()) %>%
      addTiles() %>%
      addMarkers(lng = ~lng, lat = ~lat, popup = popupa)
  }) 


  output$mytable <- renderDataTable(datatoPlot())

}
shinyApp(ui2, server)

enter image description here