繁体   English   中英

R Shiny Leaflet - clearShapes()不工作?

[英]R Shiny Leaflet - clearShapes() not working?

我有一个表格的数据集(tst_geo.csv):

lat, lon, time
10, 20, 1
10, 20, 2
10, 20, 3
40, 40, 4
40, 40, 5
40, 40, 6
0, 0, 7
0, 0, 8
0, 0, 9

R代码:

library(shiny)
library(leaflet)
library(plyr)

ui <- fluidPage(
    sidebarLayout(

        sidebarPanel(
            uiOutput("slider")
        ),
        mainPanel(
            leafletOutput("map")
        )
    )
)

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

    df <- read.csv("tst_geo.csv", header=TRUE)
    df['time'] <- as.numeric(df$time)

    #make dynamic slider
    output$slider <- renderUI({
        sliderInput("time_span", "Time Span", step=1, min=min(df$time), 
                    max=max(df$time), value = c(min(df$time), max(df$time)))
    })

    filter_df <- reactive({
        df[df$time >= input$time_span[1] & df$time <= input$time_span[2], ]
    })

    output$map <- renderLeaflet(
        leaflet() %>% addTiles()
    )

    observe({
        points_df <- ddply(filter_df(), c("lat", "lon"), summarise, count = length(timestamp))
        cat(nrow(points_df))
        leafletProxy("map", data = points_df) %>% clearShapes() %>% addCircles()
    })

}

shinyApp(ui, server)

我有一个滑块只显示特定时间范围内的点数。

但是,在observe函数内部,当我调用clearShapes()时,不会清除这些点。

任何想法为什么会这样?

在这种情况下,罪魁祸首是renderUI() 因为您使用了renderUI() ,所以滑块的渲染会延迟。 当观察者第一次运行时,滑块还没有, input$time_span filter_df()最初为NULL ,因此filter_df()返回一个空数据帧。 在这种情况下我没有看到使用renderUI()的特殊原因(也许你有原因),你可以将sliderInput()移动到ui.R,或者检查if (is.null(input$time_span))在向地图添加圆圈之前,或将observe()更改为observeEvent() (如果您使用的是最新版本的闪亮 ):

observeEvent(input$time_span, {
    points_df <- ddply(filter_df(), c("lat", "lon"), summarise, count = length(timestamp))
    cat(nrow(points_df))
    leafletProxy("map", data = points_df) %>% clearShapes() %>% addCircles()
})

暂无
暂无

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

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