shiny: dynamically change tab names
Asked Answered
G

1

9

I'm working on a Shiny application that is supposed to handle multiple languages. I managed to dynamically translate almost all elements of the app depending on a selectInput to choose the language. However the "hard stuff" remains the navbarPage tabs as well as the tabPanels inside my pages. I cannot change their names. I tried this, but it does not work:

library(shiny)
ui <- navbarPage("App Title",
                 tabPanel("tab1", 
                          selectInput("language", "language", c("EN", "FR"), width = '300px'),
                          textOutput("text")),
                 uiOutput("render_tab2"))
server <- function(input, output, session) {
  output$text = renderText({ switch(input$language, "EN"="hello world", "FR"="bonjour monde")  })
  output$render_tab2 = renderUI({
    tabPanel( title=switch(input$language, "EN"="tab2", "FR"="onglet2") )})}
shinyApp(ui, server)

And the updatenavbarpanel() family of functions are just to set the active tab, not change their characteristics...Is there a way to do it, if possible that does not change the structure of all my app... THanks a lot.

Glebe answered 19/12, 2017 at 23:45 Comment(1)
Check out renderUI to dynamically render the tabs with different namesSanctimonious
R
22

This piece of code set the title dynamically :

library(shiny)
ui <- navbarPage("App Title",
                 tabPanel(title = uiOutput("title_panel"), 
                          selectInput("language", "language", c("EN", "FR"), width = '300px')
                )
    )

server <- function(input, output, session) {

    output$title_panel = renderText({
        switch(input$language, "EN"="hello world", "FR"="bonjour monde") 
    })
}

shinyApp(ui, server)

Edit : Works with both uiOutput("title_panel") & textOutput("title_panel")

Rafaello answered 20/12, 2017 at 10:15 Comment(1)
thanks! That's a great solution i haven't thought of !Glebe

© 2022 - 2024 — McMap. All rights reserved.