在Shiny应用程序中动态隐藏包中的模块。

huangapple go评论78阅读模式
英文:

Dynamically hide module from package in Shiny app

问题

我尝试构建一个动态显示和隐藏模块的应用程序。根据这个教程和这个代码,我可以显示和删除自己构建的模块。然而,当调用来自外部包(例如来自mapediteditModu模块)时,通过提供的工作流程无法删除此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 &lt;- fluidPage(
  actionButton(
    inputId = &quot;add_module&quot;,
    label = &quot;Add a module&quot;
  ),
  actionButton(
    inputId = &quot;remove_module&quot;,
    label = &quot;Remove a module&quot;
  ),
  div(
    id = &quot;add_here&quot;
  )
)

server &lt;- function(input, output, session) {
  active_modules &lt;- reactiveVal(value = NULL)
  
  # leaflet blank map for module
    map&lt;-leaflet()%&gt;%
      addProviderTiles(providers$CartoDB.Positron)
    
  observeEvent(input$add_module, {
    current_id &lt;- paste0(&quot;id_&quot;, input$add_module)
    active_modules(c(current_id, active_modules()))
    
    callModule(editMod, map,id = current_id)
    
    insertUI(
      selector = &quot;#add_here&quot;,
      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()) &gt; 0) {
      current_id &lt;- active_modules()[1]
      removeUI(
        selector = paste0(&quot;#&quot;, 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(&quot;#&quot;, current_id, &quot;-map&quot;)
  )

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.

huangapple
  • 本文由 发表于 2023年7月27日 17:29:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76778337.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定