英文:
Dynamically hide module from package in Shiny app
问题
我尝试构建一个动态显示和隐藏模块的应用程序。根据这个教程和这个代码,我可以显示和删除自己构建的模块。然而,当调用来自外部包(例如来自mapedit的editModu模块)时,通过提供的工作流程无法删除此UI。
如何正确删除已使用包加载的模块的UI?
到目前为止,我使用了以下代码
library(shiny)
library(ggplot2)
library(mapedit)
library(leaflet)
ui <- fluidPage(
actionButton(
inputId = "add_module",
label = "Add a module"
),
actionButton(
inputId = "remove_module",
label = "Remove a module"
),
div(
id = "add_here"
)
)
server <- function(input, output, session) {
active_modules <- reactiveVal(value = NULL)
# leaflet blank map for module
map<-leaflet()%>%
addProviderTiles(providers$CartoDB.Positron)
observeEvent(input$add_module, {
current_id <- paste0("id_", input$add_module)
active_modules(c(current_id, active_modules()))
callModule(editMod, map,id = current_id)
insertUI(
selector = "#add_here",
ui = editModUI(id = current_id)
)
})
observeEvent(input$remove_module, {
# only remove a module if there is at least one module shown
if (length(active_modules()) > 0) {
current_id <- active_modules()[1]
removeUI(
selector = paste0("#", current_id)
)
# update the list of currently shown modules
active_modules(active_modules()[-1])
}
})
}
shinyApp(ui, server)
英文:
I try to build an application that dynamically shows and hides modules. Following this tutorial and this code, I can show and remove modules built by my own. However, when calling a module from an external package (e.g from mapedit the editModu module) this UI can not be removed through the provided workflow.
What is the proper way to remove UI from modules that have been loaded with a package?
So far I used the following code
library(shiny)
library(ggplot2)
library(mapedit)
library(leaflet)
ui <- fluidPage(
actionButton(
inputId = "add_module",
label = "Add a module"
),
actionButton(
inputId = "remove_module",
label = "Remove a module"
),
div(
id = "add_here"
)
)
server <- function(input, output, session) {
active_modules <- reactiveVal(value = NULL)
# leaflet blank map for module
map<-leaflet()%>%
addProviderTiles(providers$CartoDB.Positron)
observeEvent(input$add_module, {
current_id <- paste0("id_", input$add_module)
active_modules(c(current_id, active_modules()))
callModule(editMod, map,id = current_id)
insertUI(
selector = "#add_here",
ui = editModUI(id = current_id)
)
})
observeEvent(input$remove_module, {
# only remove a module if there is at least one module shown
if (length(active_modules()) > 0) {
current_id <- active_modules()[1]
removeUI(
selector = paste0("#", current_id)
)
# update the list of currently shown modules
active_modules(active_modules()[-1])
}
})
}
shinyApp(ui, server)
答案1
得分: 1
在你提供的代码中,使用"add_module"加载的模块不是被称为*#id_n*,而是*#id_n-map*;你需要在removeUI
中更改选择器的名称:
removeUI(
selector = paste0("#", current_id, "-map")
)
你可以通过在浏览器中右键点击,选择"检查元素",然后查找感兴趣的元素来检查任何元素的id。
英文:
In the code you provided, the module loaded with "add_module" is not called #id_n, but #id_n-map; you need to change the selector name in removeUI
for:
removeUI(
selector = paste0("#", current_id, "-map")
)
You can check the id of any element from your shiny app by right clicking in the browser, selecting "Inspect Element" and looking for the element of interest.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论