简体   繁体   English

清除主面板以在闪亮的 r studio 中显示其他反应输出

[英]Clear the main Panel to display the other reactive output in shiny r studio

I have a dataTableOutput in my main panel.我的主面板中有一个 dataTableOutput。 Then I have an action button "Go".然后我有一个动作按钮“Go”。 Once I click "Go" I want rHandsOutput to appear in the main panel but not beneath dataTableOutput.单击“开始”后,我希望 rHandsOutput 出现在主面板中,但不在 dataTableOutput 下方。 How can I remove dataTableOutput in the main panel and display rHandsOutput.如何删除主面板中的 dataTableOutput 并显示 rHandsOutput。 In my current code both tables appearing together.在我当前的代码中,两个表一起出现。 Once I click "Go", the second table comes under the first table where I want to appear only second table (rHandsOutput) removing 1st table from the main panel.单击“Go”后,第二个表位于第一个表下,我只想显示第二个表 (rHandsOutput),从主面板中删除第一个表。

Please help me!1请帮帮我!1

You can use a combination of insertUI and removeUI to make UI components dynamic.您可以结合使用insertUIremoveUI来使 UI 组件动态化。 For example:例如:

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)

You can use showElement() and hideElement() from shinyjs :您可以使用showElement()hideElement()shinyjs

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

Assuming the ID of the first table is “firsttable” and the ID of the second table is “rHandsOutput”, and the ID of the “Go” button is “go”.假设第一个表的ID是“firsttable”,第二个表的ID是“rHandsOutput”,“Go”按钮的ID是“go”。

Generate the ui dynamically.动态生成 ui。

Use uiOutput("someidentifier") in the ui.r and then fill it with your datatable with the following in server.r使用uiOutput("someidentifier")ui.r ,然后用你的数据表在以下填充server.r

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

output$datatableidentifier <- renderDataTable(iris)

uiOutput takes the place (in ui.r ) of whatever ui element you want to add dynamically. uiOutput代替您想要动态添加的任何 ui 元素(在ui.r )。 The necessary code for that ui then moves to renderUI in server.r .该 ui 的必要代码然后移动到server.r renderUI

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

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