2

我试图模仿或弄清楚与 LeafletProxy 类似的功能如何在 Shiny 应用程序(flexdashboard)内的 mapview 包中工作。这个想法是我有一个参数化的数据库查询,它根据用户输入获取一个 sf 数据集(约 4200 个多边形),然后在 mapview 中绘图。但是,似乎每次完成此操作时都会重新绘制整个地图?

下面是一个使用默认 franconia 数据集和闪亮输入来控制线条不透明度的可重现示例。我还包括我的代码(已注释掉)以展示如何按预期使用它的示例(即基于数据库获取动态重绘多边形层)

有没有办法在闪亮的情况下绘制一次所有背景地图样式的“基础”地图,然后只在检索到新的多边形数据时重新绘制它们?

谢谢!


    ---
    title: "MRE"
    output: 
      flexdashboard::flex_dashboard:
        orientation: columns
        vertical_layout: fill
    runtime: shiny
    ---

    ```{r setup, include=FALSE}
    library(flexdashboard)
    library(mapview)
    library(shiny)
    library(leaflet)
    #library(RSQLite)
    #library(sf)
    ```


    Sidebar {.sidebar}
    -----------------------------------------------------------------------

    ```{r}
    # This is shiny input that will trigger entire map redraw
    sliderInput("gridlines", "Grid Line Opacity", 0, 1, 0, step = 0.1, ticks = FALSE)
    ```

    ```{r}
    ### THIS IS MY ACTUAL CODE...For reference

    # sp_grid <- reactive({
    #   db <- "../data/modeldata.db"
    #   con <- dbConnect(SQLite(), db )
    #   
    #   # set the sql based on user input
    #   sql <- 'SELECT id, Time,Cell_I, Cell_J, Cell_K, Cell_Botdepth_M, Zmax, Salinity, WKT_GEOMETRY 
    #                 FROM vwGridTK 
    #                 WHERE Time = :time 
    #                   AND Cell_K = :layer'
    #   
    #   df.grid <- dbGetQuery(con, sql, params = list(time = 0,
    #                                                layer = 1))
    #   dbDisconnect(con)
    #   
    #   st_as_sf(df.grid, wkt = "WKT_GEOMETRY") %>% st_set_crs(4326)
    #  
    # })

    ```

    Column {data-width=500}
    -----------------------------------------------------------------------

    ### Reproducible Example

NOTE the shiny input to control opacity

    ```{r}
    renderLeaflet({
      m <- mapview(franconia, zcol = "district", alpha = input$gridlines)
      m@map

    })

    ```

    Column {data-width=500}
    -----------------------------------------------------------------------

    ### My Example

    ```{r}

    # renderLeaflet({
    #     m <- mapview(sp_grid(), zcol = "Salinity",  
    #           legend = TRUE, alpha = input$gridlines)
    #   
    #   m@map
    # 
    # })
    ```
4

0 回答 0