Search code examples
rshinybslib

Change text of full_screen button ("Expand") in bslib card


How can one change the text (I need basically the translation to another language) of the expand button (which is created by full_screen = TRUE) and the close button in the full screen?

enter image description here enter image description here

MWE:

library(shiny)
library(bslib)

# Define UI
ui <- fluidPage(
  theme = bs_theme(bootswatch = "minty"),
  titlePanel("Table in a card()"),
    
    mainPanel(
      card(
        title = "Data Table",
        full_screen = TRUE,
        tableOutput("mytable")
      )
  )
)

# Define server logic
server <- function(input, output) {
  # Generate sample data
  data <- data.frame(
    Name = c("John", "Jane", "Alice", "Bob", "Charlie", "David"),
    Age = c(25, 30, 35, 40, 45, 50),
    Score = c(80, 75, 90, 85, 88, 92)
  )
  
  # Render table
  output$mytable <- renderTable({
    filtered_data <- data
  })
}

# Run application
shinyApp(ui = ui, server = server)

Solution

  • Below is a solution using JS, I currently don't see a possibility to do this directly using bslib built-in functions. However, note that if manipulating bslib functions is an option for you, then you could also try this, e.g. the 'Expand' is hard-coded in bslib:::full_screen_toggle().

    enter image description here

    library(shiny)
    library(bslib)
    
    # Define UI
    ui <- page_fluid(
      tags$script(
        HTML("$(document).ready(function(){
                // The 'Expand' text can be directly manipulated
                $('.card.bslib-card > bslib-tooltip > div').text('Erweitern');
    
                // The 'Close' text needs a mutation observer on the card because it
                // is only accessible if the full-screen mode is active.
                // We replace the html if the card has data-full-screen = true.
                var observer = new MutationObserver(function(mutations) {
                    $('.bslib-full-screen-exit').html($('.bslib-full-screen-exit').html().replace('Close', 'Schließen'));
                });
                observer.observe(document.getElementsByClassName('card bslib-card')[0], { 
                  attributes: true, 
                  attributeFilter: ['data-full-screen'] });
              });")
        ),
      theme = bs_theme(bootswatch = "minty"),
      titlePanel("Table in a card()"),
      
      mainPanel(
        card(
          title = "Data Table",
          full_screen = TRUE,
          tableOutput("mytable")
        )
      )
    )
    
    # Define server logic
    server <- function(input, output) {
      # Generate sample data
      data <- data.frame(
        Name = c("John", "Jane", "Alice", "Bob", "Charlie", "David"),
        Age = c(25, 30, 35, 40, 45, 50),
        Score = c(80, 75, 90, 85, 88, 92)
      )
      
      # Render table
      output$mytable <- renderTable({
        filtered_data <- data
      })
    }
    
    # Run application
    shinyApp(ui = ui, server = server)