leafletProxy无法找到被动数据

时间:2017-02-18 14:06:15

标签: r shiny leaflet

如果我在这里犯了某种类型的简单错误,我很抱歉,但是我没有使用Shiny太多而且我无法获得我正在用传单更新的地图。我正在使用的数据可以找到here

想法是用户选择日期,然后选择区域,然后使用单选按钮单击狂欢节游行的名称。执行此操作后,地图将更新游行路线和下方的表格以及更多详细信息。该表运行完美,没有leafletProxy()的地图也是如此。当我包含leafletProxy代码块时,它给出了以下错误:

message = "argument \"data\" is missing, with no default",

当我调试这个时,我可以看到数据对象(test2)并且它是一个像我期望的数据帧,并且没有NA,因为在其他一些SO问题中显示它是一个问题关于这个话题。 (就像我说的,它可以作为一张桌子使用。)

我正在使用的代码是(有问题的部分已注释掉):

#library(shiny); library(tidyverse); library(leaflet)
carnaval <- read_csv("carnaval.csv")

ui <- fluidPage(#theme = "main.min.css", # not important here
  titlePanel("Cadê o meu bloco?"),
  sidebarLayout(
    sidebarPanel(
      dateInput("day", "Escolha a data:", min = min(carnaval$Data),
                max = max(carnaval$Data),
                value = Sys.time(), language = "pt-BR"),
      selectInput("region", "Escolha a região:",  
                  choices = carnaval$Regional),
      uiOutput("Bloco")), 
    mainPanel(
      leafletOutput("mapa"),
      uiOutput("detalhes")
    )
  )
)

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

   small_carnaval <- reactive({
    if (is.null(input$day)) {
      return(NULL)
     }
    if (is.null(input$region)) {
     return(NULL)
    }
    test <- carnaval %>% filter(Data == input$day, 
                    Regional == input$region) %>% 
      select(Bloco = `Nome do bloco`, Concentração, etapa, rota, lon, lat)
    return(test)
   })

  output$Bloco <- renderUI({  
    radioButtons(inputId = "blocos", label = "Qual bloco?", 
                 choices = unique(small_carnaval()$Bloco),
                 selected = small_carnaval()$Bloco[1])
  })

  smaller_carnaval <- reactive({
    if (is.null(input$blocos)) {
      return(NULL)
    }
    test2 <- small_carnaval() %>% filter(Bloco %in% input$blocos) %>% 
      filter(!is.na(rota))
    return(test2)
  })

  output$detalhes <- renderUI({
    DT::dataTableOutput("table")
  })

  output$table <- DT::renderDataTable(DT::datatable({
    smaller_carnaval()
  }))

  output$mapa <- renderLeaflet({
    leaflet(data = carnaval) %>%
      addTiles() %>% 
      fitBounds(lng1 = -46.743221, lat1 = -23.635246,
                lng2 = -46.463757, lat2 = -23.472222)
  })


  # observe({
  # 
  #   leafletProxy("mapa", data = small_carnaval()) %>%
  #     clearShapes() %>%
  #     clearPopups() %>%
  #     clearMarkers() %>%
  #     addMarkers(data = small_carnaval(),
  #                popup = ~paste(Bloco, etapa, sep = "\n"),
  #                lng = ~lon, lat = ~lat) %>% 
  #     fitBounds(lng1 = ~min(lon, na.rm = T), lat1 = ~min(lat, na.rm = T),
  #               lng2 = ~max(lon, na.rm = T), lat2 = ~max(lat, na.rm = T)) %>%
  #     setView(lng = ~lon,lat = ~lat, zoom = 10) %>%
  #     addPolylines(data = small_carnaval(),
  #                  lng = ~lon, lat = ~lat)
  # })

}

shinyApp(ui, server)

0 个答案:

没有答案