簡體   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