闪亮+传单反应功能不起作用

Shiny + Leaflet reactive function not working

我的数据由 lon 、lat、region、flat-type 和 year 等列组成。我已经使用 leaflet 和 shiny 创建了带有集群标记的地图。

我包括了 2 个选择输入框 - 一个用于年份,一个用于平面类型。使用反应函数,每当我 运行 闪亮的应用程序时,它都会给我这个错误。

Error: Don't know how to get location data from object of class reactiveExpr,reactive

这是我的代码

library(shiny)
library(leaflet)
library(dplyr)

ui <- fluidPage(
    titlePanel("Transactions for Resale Flats"),
    h3("Model A Flats: 3-Room, 4-Room, 5-Room"),
    sidebarLayout(position = 'right',
        sidebarPanel(
            selectInput("year","Year", choices = c("2007","2008",
                                                   "2009","2010","2011",
                                                   "2012","2013","2014",
                                                   "2015","2016","2017"), selected="2007"),

            selectInput("type","Flat-Type",choices = c("3 ROOM",'4 ROOM',"5 ROOM"),selected = "3-Room"),
            width = 2),
        mainPanel(leafletOutput("mymap",height = 650,width=605)))


)


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

    headlinedata<-reactive({
        headlinedata%>%
            filter(year %in% input$year & flat_type %in% input$type)
    })

    output$mymap <- renderLeaflet({
        leaflet(data=headlinedata) %>% 
            addTiles() %>% 
            addMarkers(clusterOptions = markerClusterOptions(),
            label = paste(headlinedata$address,',',headlinedata$town))

          })

    observe(leafletProxy('mymap', data=headlinedata()))%>%
    clearMarkers()%>%
    addMarkers(clusterOptions = markerClusterOptions(),
               label = paste(headlinedata$address,',',headlinedata$town))

}

shinyApp(ui = ui, server = server)

还有这个代码

  observe(leafletProxy('mymap', data=headlinedata()))%>%
        clearMarkers()%>%
        addMarkers(clusterOptions = markerClusterOptions(),
                   label = paste(headlinedata$address,',',headlinedata$town))

每当我包含此内容时,该应用程序都会 运行 一秒钟然后立即关闭。此代码应该在输入更改时更新地图标记。

谢谢。

首先,您需要将反应变量引用为变量名后跟()。在output$mymap中,你引用headlinedata,这是要过滤的数据框,而应该是headlinedata(),这是已经过滤的反应变量。为了消除两者的歧义,我将反应变量的名称更改为 df。然后,当下游代码需要该反应变量时,我将其称为 df()

其次,由于 df() 是一个反应变量,我们已经将传单设置为依赖于它,只要反应变量发生变化,地图也会发生变化。这意味着我们不需要 observe(leafletProxy ... 代码。

这是您可以复制和粘贴的可重现示例。

library(shiny)
library(leaflet)
library(dplyr)

set.seed(1)
headlinedata <- data.frame(year = rep(2007:2017, 10),
                           flat_type = sample(c("3 ROOM",'4 ROOM',"5 ROOM"),
                                         110, replace=T),
                           lat = sample(1:50,  110, replace=T),
                           lng = sample(1:50, 110, replace=T),
                           address = "address", 
                           town = "town")

ui <- fluidPage(
    titlePanel("Transactions for Resale Flats"),
    h3("Model A Flats: 3-Room, 4-Room, 5-Room"),
    sidebarLayout(position = 'right',
                  sidebarPanel(
                      selectInput("year","Year", choices = c("2007","2008",
                                                             "2009","2010","2011",
                                                             "2012","2013","2014",
                                                             "2015","2016","2017"), selected="2007"),

                      selectInput("type","Flat-Type",choices = c("3 ROOM",'4 ROOM',"5 ROOM"),selected = "3-Room"),
                      width = 2),
                  mainPanel(leafletOutput("mymap",height = 650,width=605)))


)


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

    df<-reactive({
        headlinedata%>%
            dplyr::filter(year %in% input$year & flat_type %in% input$type)
    })

    output$mymap <- renderLeaflet({
        leaflet(data=df()) %>% 
            addTiles() %>% 
            addMarkers(clusterOptions = markerClusterOptions(),
                       label = paste(df()$address,',',df()$town))

    })


}

shinyApp(ui = ui, server = server)