繁体   English   中英

R Shiny:如何在闪亮模块中使用 updateBox() 来更新模块外的框?

[英]R Shiny: how to use updateBox() within shinymodule, to update a box outside the module?

我目前正在重写一个大的闪亮应用程序,并尝试尽可能多地转换为模块。 在某些时候,用户可以选择天气来使用框 a) 内或框 b) 内的东西。 我知道如何切换或删除/恢复 shiny 中的框,但在使用闪亮模块时遇到了一个问题:在 ui 函数内部,我有一个单选按钮,服务器函数应该只观察它的值并隐藏或显示根据输入框。 好吧,隐藏或显示的实际框不在模块内部,因为它被另一个模块填充。

请参阅下面的代码示例,您会看到该框不会被删除或恢复或其他。

也许有人知道如何解决这个问题或看到我在哪里犯了错误?

谢谢!


# ui ----
testUI <- function(id){
  
  tagList(
    
    radioGroupButtons(NS(id, "switch"), label = NULL, individual = T, 
                      choices = c("show", "dont show"), selected = "dont show"),

  ) 
  
} 


# server ----
testServer <- function(id, boxid){
  
  moduleServer(id, function(input, output, session){
    
    observeEvent(input$switch, {
      if(input$switch == "show"){
        updateBox(id = boxid, action = "restore", session = session)
      } else {
        updateBox(id = boxid, action = "remove", session = session)
      }
    })

  }) 
    
}


# testing ----
testApp <- function(){

  # create ui
  ui <- dashboardPage(

    dashboardHeader(),
    dashboardSidebar(),
    dashboardBody(

      testUI("zg"),
      
      box(id = "mybox", title = "I am a box",
          strong("some content")
      ) # end box
      
    ) # end dashboardBody
    
  ) # end dahsboardPage
  
  # create server
  server <- function(input, output, session){
    testServer("zg", boxid = "mybox")
  }
  
  # start server
  shinyApp(ui, server)
  
}

# start small app for testing (comment if not in use)
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyWidgets)

testApp()

通常,为了在模块中使用updateXXX function,要更新的小部件必须在模块的 UI 部分。

但这不适用于updateBox ,我不知道为什么(我认为 package 作者应该在实现中添加一些东西)。 请参见下面的示例。 updateRadioButtons有效,但updateBox

testUI <- function(id){
  
  ns <- NS(id)
  
  tagList(
    
    radioButtons( # this widget will be updated
      ns("switch"), label = NULL, 
      choices = c("show", "dont show"), selected = "show"
    ),
    
    box( # this widget will *not* be updated
      id = ns("mybox"), title = "I am a box", strong("some content")
    )
  ) 
  
} 


# server ----
testServer <- function(id, boxid){
  
  moduleServer(id, function(input, output, session){
    
    observeEvent(input$switch, {
      if(input$switch == "show"){
        updateBox(id = boxid, action = "restore", session = session)
        updateRadioButtons(session, "switch", label = "HELLO")
      } else {
        updateBox(id = boxid, action = "remove", session = session)
        updateRadioButtons(session, "switch", label = "GOODBYE")
      }
    })
    
  }) 
  
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM