Home > database >  Show all object with renderUI
Show all object with renderUI

Time:05-20

I'm working on a shiny app with dynamic rendering. When the user uncheck the box, he must have an output with 8 wellPanel and when the box is checked, he must have two wellPanel. I used the function renderUI to generate output but when the box is unchecked, I only have 4 wellPanel instead of 8. This is what I did :

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)


body <- dashboardBody(

  tabItems(
    
    tabItem(tabName = "menutab1",
            
            checkboxInput(inputId = "my_id", "check the box", value = TRUE),

            ####### renderUI #####
            uiOutput("results")
            
    )
  )
  
  
)

ui <- dashboardPage(
  
  title = "test",
  options = list(sidebarExpandOnHover = TRUE),
  header = dashboardHeader(disable = FALSE),
  sidebar = dashboardSidebar(
    minified = TRUE, collapsed = TRUE,
    sidebarMenu(id="mymenu",
                
                menuItem("first", tabName =  "tab1", icon = icon("fas fa-acorn"),
                         menuSubItem('menu 1',
                                     tabName = 'menutab1',
                                     icon = icon('fas fa-hand-point-right'))
                )
                
                
    )
  ),
  
  body
  
)


############# SERVER ############
server <- function(input, output) {
  
  output$results <- renderUI({
    
    if(input$my_id){
      # object 1
      fluidRow(
        column(6,
               wellPanel(
                 h1("A")
               ),
               br(),
               wellPanel(
                 h1("B")
               )
        )
      )
      
    } else {
      
      # object 2 : doesnt show, why ?
      fluidRow(
        column(6,
               wellPanel(
                 h1("C")
               ),
               br(),
               wellPanel(
                 h1("D")
               )
        ),
        column(6,
               wellPanel(
                 h1("E")
               ),
               br(),
               wellPanel(
                 h1("F")
               )
        )
      )
      
      # object 3 : I only got this
      fluidRow(
        column(6,
               wellPanel(
                 h1("H")
               ),
               br(),
               wellPanel(
                 h1("I")
               )
        ),
        column(6,
               wellPanel(
                 h1("J")
               ),
               br(),
               wellPanel(
                 h1("K")
               )
        )
      )
      
    }

  })

}


############# RUN #############
shinyApp(ui = ui, server = server)

How can we fix that ?

Some help would be appreciated

CodePudding user response:

The problem with your above code is, that only the last object of the else statement is returned. You can wrap both fluidRows in a tagList to get the desired output.

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)

body <- dashboardBody(tabItems(tabItem(
  tabName = "menutab1",
  checkboxInput(inputId = "my_id", "check the box", value = TRUE),
  uiOutput("results")
)))

ui <- dashboardPage(
  title = "test",
  options = list(sidebarExpandOnHover = TRUE),
  header = dashboardHeader(disable = FALSE),
  sidebar = dashboardSidebar(
    minified = TRUE,
    collapsed = TRUE,
    sidebarMenu(
      id = "mymenu",
      menuItem(
        "first",
        tabName =  "tab1",
        icon = icon("fas fa-acorn"),
        menuSubItem(
          'menu 1',
          tabName = 'menutab1',
          icon = icon('fas fa-hand-point-right')
        )
      )
    )
  ),
  body
)

server <- function(input, output) {
  output$results <- renderUI({
    if (input$my_id) {
      fluidRow(column(6,
                      wellPanel(h1("A")),
                      br(),
                      wellPanel(h1("B"))
      )
      )
    } else {
      tagList(
        fluidRow(
          column(6,
                 wellPanel(h1("C")),
                 br(),
                 wellPanel(h1("D"))),
          column(6,
                 wellPanel(h1("E")),
                 br(),
                 wellPanel(h1("F")))
        ),
        fluidRow(
          column(6,
                 wellPanel(h1("H")),
                 br(),
                 wellPanel(h1("I"))),
          column(6,
                 wellPanel(h1("J")),
                 br(),
                 wellPanel(h1("K")))
        )
      )
    }
  })
}

shinyApp(ui = ui, server = server)
  • Related