Search code examples
rshinyshinydashboardbs4dash

Add conditional text outpuT to cardprofile


I am using a cardProfile from bs4Dash library to show the current user in my Shiny app.

The cardProfile looks like this in the ui part:

   cardProfile(
     src = "logo.png",
     title = 'SHOW HERE THE USERNAME'
     subtitle = "Administrator",
     cardProfileItemList(
       bordered = TRUE,
       cardProfileItem(
         title = "Email",
         description = 'SHOW HERE THE EMAIL'
       )
     )

What should I use in the title and description to show a name and email depending on an input?

I have tried with a textOutput:

title = textOutput('title')

description = textOutput('email')

And a reactive in the server part with no result:

reactive({
  USER <- input$user
  output$title<- USER 
  output$email<- usuarios$email[usuarios$usuario == USER ]
})

Solution

  • You need to define your card server-side in a renderUI(), and then display it in the UI with a UIOuptut().

    Almost everytime you need to display in the UI something that is reactive, you must code it server-side or use an updateInput function when it exists.

    library(shiny)
    library(bs4Dash)
    
    df_email <- data.frame(user = "toto",
                           email = "[email protected]")
    
    shinyApp(
      ui = dashboardPage(,
                         header = dashboardHeader(),
                         sidebar = dashboardSidebar(),
                         body = dashboardBody(
                           bs4Card(
                             uiOutput("card_user")
                           )
                         ),
                         title = "DashboardPage"
      ),
      
      server = function(input, output) { 
    
        # USER <- reactive(input$user) #uncomment
        USER <- reactive("toto") #comment
        
        output$card_user <- renderUI({
          cardProfile(
            # src = "logo.png",
            title = 'SHOW HERE THE USERNAME',
            subtitle = "Administrator",
            cardProfileItem(
              title = USER(),
              description = df_email$email[df_email$user == USER()] #replace with your own data
              
            )
          )
        })
        }
    )