【问题标题】:Clear the main Panel to display the other reactive output in shiny r studio清除主面板以显示闪亮 r 工作室中的其他反应输出
【发布时间】:2017-04-21 13:36:30
【问题描述】:

我的主面板中有一个 dataTableOutput。然后我有一个动作按钮“开始”。单击“开始”后,我希望 rHandsOutput 出现在主面板中,但不在 dataTableOutput 下方。如何删除主面板中的 dataTableOutput 并显示 rHandsOutput。在我当前的代码中,两个表一起出现。单击“开始”后,第二个表位于第一个表下方,我只想在其中显示第二个表(rHandsOutput),从主面板中删除第一个表。

请帮帮我!1

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您可以结合使用insertUIremoveUI 来使UI 组件动态化。例如:

    library(shiny)
    
    ui <- fluidPage(
    
    
    sidebarLayout(
      
      sidebarPanel(
        
        actionButton(inputId = "go", label = "Go")
        
      ),
      
    mainPanel(
      
      fluidRow(
        tags$div(id = "firstOutput", 
               dataTableOutput("myDataTable"))
        ),
      
      fluidRow(
        tags$div(id = "placeholder") # the dynamic UI will be inserted relative to this placeholder
        )
    ))
    
    )
    
    
    server <- function(input, output) {
      
      output$myDataTable <- renderDataTable(iris)
        
      observeEvent(input$go, {
        
        removeUI("div:has(>#firstOutput)")
        
        insertUI(
          selector = "#placeholder",
          where = "afterEnd", # inserts UI after the end of the placeholder element
          ui = fluidRow(
            actionButton(inputId = "newButton", label = "A new button")))
        
        })
      
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      【解决方案2】:

      您可以使用shinyjs 中的showElement()hideElement()

      observeEvent(input$go, {
        shinyjs::hideElement(“firsttable”)
        shinyjs::showElement(“rHandsOutput”)
      })
      

      假设第一个表的ID是“firsttable”,第二个表的ID是“rHandsOutput”,“Go”按钮的ID是“go”。

      【讨论】:

        【解决方案3】:

        动态生成用户界面。

        ui.r 中使用uiOutput("someidentifier"),然后在server.r 中使用您的数据表填充它

        output$someidentifier <- 
          renderUI({
            dataTableOutput("datatableidentifier")
          })
        
        output$datatableidentifier <- renderDataTable(iris)
        

        uiOutput 代替您想要动态添加的任何 ui 元素(在ui.r 中)。然后,该 ui 的必要代码移至 server.r 中的 renderUI

        【讨论】:

        • 这只是在 ui 中显示表格的另一种方式,并不能解决提出的问题。
        猜你喜欢
        • 2023-03-12
        • 1970-01-01
        • 2017-09-04
        • 2014-03-03
        • 1970-01-01
        • 1970-01-01
        • 2021-12-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多