我正试图以编程方式更改我的Shiny App中的基础层。 因为我不想使用'Leaflet'的LayerControl,而是希望在一个面板中拥有所有控件。我决定使用shinyjs并使用toggleState按钮在两个基本层之间切换回来。
目前我正处于改变基础层的原则阶段,因为只有一个基础层可见,所以我似乎必须删除最初加载的基础层的切片。
这样做我可以更改显示的基础层,但同时更改基础层我失去了叠加层。我怎么能避免这种情况?
再次使用按钮时,我可以在闪烁中看到叠加层仍然存在,但不再位于基础层之上。
这是一个例子:
library(shiny)
library(leaflet)
library(shinydashboard)
# Definition of Sidebar elements
sidebar <- dashboardSidebar(
sidebarMenu(
menuItem("Maps", tabName = "maps", icon = icon("globe"),
menuSubItem(
HTML(paste("Diffuse kilder NH", tags$sub("3"), sep = "")),
tabName = "map_dif_nh3", icon = icon("map-o"), selected = TRUE
)
)
)
)
# Definition of body elements
body <- dashboardBody(
tabItems(
tabItem(tabName = "map_dif_nh3",
box(
width = 12,
div(style = "height: calc(100vh - 80px);",
leafletOutput(
"m_dif_nh3", width = "100%", height = "100%"
),
absolutePanel(id = "nh3_panel", class = "panel panel-default",
fixed = TRUE, style = "opacity: 0.87",
top = 80, left = "auto", right = 50, bottom = "auto",
width = 285, height = "auto",
fluidRow(
column(width = 10, offset = 1,
actionButton(inputId = 'btn_bgr_nh3', label = "", icon = icon("globe", class = "fa-lg"))
)
)
)
)
)
)
)
)
ui <- dashboardPage(
dashboardHeader(title = "Mixed layout"),
sidebar,
body
)
server <- function(input, output) {
init_lat <- 56.085935208960585
init_lon <- 10.29481415546154
init_zoom <- 7
output$m_dif_nh3 <- renderLeaflet({
leaflet(height = "100%") %>%
addProviderTiles("Stamen.Toner", layerId = 'mb_osm', group = "base") %>%
setView(init_lon, init_lat, init_zoom) %>%
addWMSTiles(
"http://gis.au.dk/geoserver_test/PRTR/gwc/service/wms",
layers = "PRTR:prtr_nh3_2014",
layerId = "nh3_2014",
group = "overlay",
options = WMSTileOptions(format = "image/png",
transparent = TRUE, opacity = 0.8
)
)
})
observeEvent(
input$btn_bgr_nh3, {
leafletProxy("m_dif_nh3") %>%
addProviderTiles("Esri.WorldImagery", layerId = 'mb_pic', group = 'base')
leafletProxy("m_dif_nh3") %>%
removeTiles(layerId = 'mb_osm')
}
)
}
shinyApp(ui, server)
答案 0 :(得分:0)
我认为您可以执行的操作是在单击按钮后将操作按钮的ID值重置为0。因此,每次切换ID值都会被替换为0.它对我有用。希望它也适合你。
答案 1 :(得分:0)
在Leaflet JS(我不了解R)中,如果myTileLayer
已经是基础层的一部分,那么myTileLayer.addTo(map)
会执行切换工作。它没有添加在最前面;而且您不需要删除当前图层。覆盖层不受影响。