Change title on button click in Shiny Navbar App

Viewed 285

What is a good way to change a Shiny app title? In the example app below I have a radioButtons input and I'd like the app title ("Project Title") to change given the radio selection.

library(shiny)

ui <- navbarPage("Project Title",
        tabPanel(title = "Tab 1",
                   radioButtons("title_change", label = "Change title of App", choices = c("Title 1", "Title 2"))
                   )
                 
)

server <- function(input, output) {
  
}

shinyApp(ui = ui, server = server)
2 Answers
library(shiny)

js <- "
$(document).ready(function(){
  $('#title_change').on('change', function(){
    var title = $('input[name=title_change]:checked').val();
    $('span.navbar-brand').text(title);
  });
});
"

ui <- fluidPage(
  tags$head(tags$script(HTML(js))),
  navbarPage(
    "Project Title",
    tabPanel(
      title = "Tab 1",
      radioButtons("title_change", label = "Change title of App", choices = c("Title 1", "Title 2"))
    )
  )
)

server <- function(input, output) {}

shinyApp(ui = ui, server = server)

You can do this with a shiny::textOutput and shiny::renderText

library(shiny)
ui <- navbarPage(textOutput("title"),
                 tabPanel(title = "Tab 1",
                          radioButtons("title_change", label = "Change title of App", choices = c("Title 1", "Title 2"))
                 )
)
server <- function(input, output) {
    output$title <- renderText({
        input$title_change
    })
}
shinyApp(ui = ui, server = server)

When you change your radio button, that changes input$title_change. When that changes, it updates output$title.

Related