闪亮+小叶反应功能不起作用

时间:2019-07-14 20:04:34

标签: r shiny leaflet react-leaflet

我的数据由lon,lat,region,flat-type和year等列组成。我用传单和发亮的方法来创建带有聚类标记的地图。

我包括2个selectInput框-一个用于年份,一个用于扁平型。使用反应式功能,每当我运行闪亮的应用程序时,它始终会给我这个错误。

  

错误:不知道如何从类的对象获取位置数据   reactExpr,反应性

这是我的代码

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))

只要包含此内容,该应用程序将运行一秒钟,然后立即关闭。只要输入发生更改,该代码就应该更新地图标记。

谢谢。

1 个答案:

答案 0 :(得分:0)

首先,您需要将反应变量称为变量名,后跟()。在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)