简体   繁体   English

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

[英]Shiny + Leaflet reactive function not working

My data consists of columns like lon , lat, region, flat-type and year. 我的数据包括lon,lat,region,flat-type和year等列。 I have used leaflet and shiny to create a map with cluster markers. 我已经使用传单和光泽来创建带有群集标记的地图。

I included 2 selectInput boxes - one for year and one for the flat-type. 我包括2个selectInput框-一个用于年份,一个用于扁平型。 Using the reactive function, it keeps giving me this error whenever I run the shiny app. 使用反应式功能,每当我运行闪亮的应用程序时,它始终会给我这个错误。

Error: Don't know how to get location data from object of class reactiveExpr,reactive 错误:不知道如何从reactactive类的对象获取位置数据,reactive

Here's my code 这是我的代码

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)

Also this code 也是这个代码

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

Whenever I include this, the app will run for a second and then close immediately. 每当我包含此内容时,该应用程序将运行一秒钟,然后立即关闭。 This code is supposed to update the map markers whenever the input changes. 只要输入发生更改,该代码就应该更新地图标记。

Thanks. 谢谢。

First, you need to refer to reactive variables as the variable name followed by () . 首先,您需要将反应变量称为变量名,后跟() In output$mymap , you refer to headlinedata , which is the data frame to be filtered, when it should be headlinedata() , which is the reactive variable that's already been filtered. output$mymap ,您应该引用headlinedata ,它是要过滤的数据帧,当它应该是headlinedata() ,它是已经过滤的反应变量。 To disambiguate the two, I changed the name of the reactive variable to df . 为了消除两者的歧义,我将反应变量的名称更改为df Then, when that reactive variable is needed in code downstream, I refer to it as df() . 然后,当下游代码中需要该反应变量时,我将其称为df()

Second, since df() is a reactive variable and we've set up the leaflet to depend upon it, whenever the reactive variable changes, the map will also change. 其次,由于df()是一个反应变量,并且我们已经设置了依赖它的传单,因此每当反应变量发生变化时,映射也会发生变化。 This means we don't need the observe(leafletProxy ... code. 这意味着我们不需要observe(leafletProxy ...代码。

Here's a reproducible example you can copy and paste. 这是一个可复制的示例,您可以复制和粘贴。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM