在Shiny

时间:2018-05-01 05:05:55

标签: r shiny leaflet reactive-programming r-leaflet

以下代码旨在重现this example中的内容,但为" speed"添加其他参数除外。但是,我的地图数据链接断了 - 任何人都可以帮我发现错误吗?原始代码根据地图的边界更新表格,而在我的代码中更改地图缩放对我的表格没有影响。我也无法获得"速度"过滤器来处理表和地图,这是我正在寻找的功能。任何指针都会受到赞赏。

library(shiny)
library(magrittr)
library(leaflet)
library(DT)

ships <-
  read.csv(
    "https://raw.githubusercontent.com/Appsilon/crossfilter-demo/master/app/ships.csv"
  )

ui <- shinyUI(fluidPage(
  titlePanel(""),
  sidebarLayout(
    sidebarPanel(width = 3,
                 numericInput(
                   "speed", label = h5("Ship's Speed"), value = 100
                 )),
    mainPanel(tabsetPanel(
      type = "tabs",
      tabPanel(
        "Leaflet",
        leafletOutput("leafletmap", width = "350px"),
        dataTableOutput("tbl")
      )
    ))
  )
))

server <- shinyServer(function(input, output) {
  in_bounding_box <- function(data, lat, long, bounds, speed) {
    data %>%
      dplyr::filter(
        lat > bounds$south &
          lat < bounds$north &
          long < bounds$east & long > bounds$west & ship_speed < input$speed
      )
  }

  output$leafletmap <- renderLeaflet({
    leaflet() %>%
      addProviderTiles("Esri.WorldImagery", group = "ESRI World Imagery") %>%
      addCircleMarkers(
        data = ships,
        ~ long ,
        ~ lat,
        popup =  ~ speed,
        radius = 5 ,
        stroke = FALSE,
        fillOpacity = 0.8,
        popupOptions = popupOptions(closeButton = FALSE)
      )
  })

  data_map <- reactive({
    if (is.null(input$map_bounds)) {
      ships
    } else {
      bounds <- input$map_bounds
      in_bounding_box(ships, lat, long, bounds, speed)
    }
  })

  output$tbl <- DT::renderDataTable({
    DT::datatable(
      data_map(),
      extensions = "Scroller",
      style = "bootstrap",
      class = "compact",
      width = "100%",
      options = list(
        deferRender = TRUE,
        scrollY = 300,
        scroller = TRUE,
        dom = 'tp'
      )
    )
  })


})

shinyApp(ui = ui, server = server)

2 个答案:

答案 0 :(得分:2)

您要渲染的传单地图称为leafletmap。因此,不要参考map_bounds尝试将其更改为leafletmap_bounds

  data_map <- reactive({
    if (is.null(input$leafletmap_bounds)) {
      ships
    } else {
      bounds <- input$leafletmap_bounds
      in_bounding_box(ships, lat, long, bounds, speed)
    }
  })

同样在过滤器中,将ship_speed更改为speed。应该有希望工作。

答案 1 :(得分:2)

两个小变化:

  • 在您链接的示例中,leafletmap有效,因为传单输出对象称为input$leafletmap_bounds。但是,您将其重命名为dplyr,因此我们应该参考speed
  • ship_speed声明中,我们应该引用library(shiny) library(magrittr) library(leaflet) library(DT) ships <- read.csv( "https://raw.githubusercontent.com/Appsilon/crossfilter-demo/master/app/ships.csv" ) ui <- shinyUI(fluidPage( titlePanel(""), sidebarLayout( sidebarPanel(width = 3, numericInput( "speed", label = h5("Ship's Speed"), value = 100 )), mainPanel(tabsetPanel( type = "tabs", tabPanel( "Leaflet", leafletOutput("leafletmap", width = "350px"), dataTableOutput("tbl") ) )) ) )) server <- shinyServer(function(input, output) { in_bounding_box <- function(data, lat, long, bounds, speed) { data %>% dplyr::filter( lat > bounds$south & lat < bounds$north & long < bounds$east & long > bounds$west & speed < input$speed ) } output$leafletmap <- renderLeaflet({ leaflet() %>% addProviderTiles("Esri.WorldImagery", group = "ESRI World Imagery") %>% addCircleMarkers( data = ships, ~ long , ~ lat, popup = ~ speed, radius = 5 , stroke = FALSE, fillOpacity = 0.8, popupOptions = popupOptions(closeButton = FALSE) ) }) data_map <- reactive({ if (is.null(input$leafletmap_bounds)) { ships } else { bounds <- input$leafletmap_bounds in_bounding_box(ships, lat, long, bounds, speed) } }) output$tbl <- DT::renderDataTable({ DT::datatable( data_map(), extensions = "Scroller", style = "bootstrap", class = "compact", width = "100%", options = list( deferRender = TRUE, scrollY = 300, scroller = TRUE, dom = 'tp' ) ) }) }) shinyApp(ui = ui, server = server) ,而不是{{1}}。

下面给出了工作代码,希望这有帮助!

{{1}}