1
我正在使用Shiny和Flexdashboard構建Leaflet地圖。地圖隱藏在使用條件面板的介紹文本後面。它在用戶從輸入中進行選擇後首次加載。帶條件邏輯的閃亮傳單映射不會呈現初始地圖標記
最初的output$map
應該只加載一次(由於大型多邊形圖層不需要重新加載),但是隻要用戶輸入更改就會更新地圖標記。
第一次加載地圖時,它不加載標記。如果地圖加載後輸入發生變化,標記會正確加載。 我的目標是在第一次選擇後加載地圖標記。
我不包括addCircleMarkers
在output$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')
)))
```
請提供'收費列表',... – BigDataScientist
當然,只是增加了一個例子。謝謝。 – Jeff