2017-06-12 41 views
1

我正在使用Shiny和Flexdashboard構建Leaflet地圖。地圖隱藏在使用條件面板的介紹文本後面。它在用戶從輸入中進行選擇後首次加載。帶條件邏輯的閃亮傳單映射不會呈現初始地圖標記

最初的output$map應該只加載一次(由於大型多邊形圖層不需要重新加載),但是隻要用戶輸入更改就會更新地圖標記。

第一次加載地圖時,它不加載標記。如果地圖加載後輸入發生變化,標記會正確加載。 我的目標是在第一次選擇後加載地圖標記。

我不包括addCircleMarkersoutput$map,因爲我不希望全部地圖 - 多邊形和所有 - 加載每次用戶輸入/無功變量的變化。

經過大量的挖掘,我發現this suggestion,但我很努力地將答案與我的代碼聯繫起來。也許我太接近代碼來看答案了。我感謝任何幫助。

這裏是一個精簡例如:

```{r setup, include=FALSE} 

library(leaflet) 
library(shiny) 
library(shinyjs) 
library(flexdashboard) 
library(dplyr) 

chargeslist <- c("Charge One","Charge Two") 

chargesdf <- data.frame(chargeslist) 

userselects <- reactive({ 
    if(input$charges != "") { 
    charges <- chargesdf[chargesdf$chargeslist %in% input$charges, ] 
    } else { 
    return(NULL) 
    } 
}) 

output$intro <- renderUI({ 
    HTML("Introduction text") 
}) 

output$map = renderLeaflet({ 
    leaflet('map') %>% addTiles() %>% 
    addProviderTiles('CartoDB.Positron') %>% 
    setView(-95.37, 29.75, zoom = 10) 
}) 

outputOptions(output, 'intro', suspendWhenHidden=FALSE) 

outputOptions(output, 'map', suspendWhenHidden=TRUE) # unless suspendWhenHidden set to TRUE, it doesn't render markers through the observer 

output$stats <- reactive({ !is.null(userselects()) }) # universal controller for conditional panels 

observe({ 
req(input$charges) 
leafletProxy("map", data = userselects()) %>% 
    clearMarkerClusters() %>% 
    addCircleMarkers(lng = ~ Longitude, lat = ~ Latitude, color = "#b20000", radius = 10), clusterOptions = markerClusterOptions()) 
}) 

``` 

Sidebar {.sidebar} 
===================================== 

```{r} 

selectizeInput("charges", "Charges", c("Choose one " = "", as.character(chargeslist))) 

``` 

Map 
===================================== 

Row 
----------------------------------------------------------------------- 
```{r} 

div(style="width:100%; float:left;", 
    conditionalPanel(condition = '!output.stats', 
        uiOutput('intro') 
)) 

div(style="width:100%;", 
conditionalPanel(condition = 'output.stats', 
    div(id="", 
     class="", 
     leafletOutput('map') 
))) 

``` 
+0

請提供'收費列表',... – BigDataScientist

+1

當然,只是增加了一個例子。謝謝。 – Jeff

回答

1

而不是使用觀測的,我所定義的地圖標記作爲輸出:

output$markers <- renderPlot({ 
    leafletProxy("map", data = userselects()) %>% 
     clearMarkerClusters() %>% 
     addCircleMarkers(lng = ~ Longitude, lat = ~ Latitude, 
         color = "#b20000", radius = 10) 
}) 

然後稱爲輸出小葉後右:

div(style="width:100%;", 
    conditionalPanel(condition = 'output.stats', 
    div(id="", 
    class="", 
    leafletOutput('map'), 
    plotOutput('markers') 
))) 

看起來像一個不完美的解決方案,但它現在可以工作。