简体   繁体   中英

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

I have a dataTableOutput in my main panel. Then I have an action button "Go". Once I click "Go" I want rHandsOutput to appear in the main panel but not beneath dataTableOutput. How can I remove dataTableOutput in the main panel and display 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.

Please help me!1

You can use a combination of insertUI and removeUI to make UI components dynamic. 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 :

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”.

Generate the ui dynamically.

Use uiOutput("someidentifier") in the ui.r and then fill it with your datatable with the following in 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. The necessary code for that ui then moves to renderUI in server.r .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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