【问题标题】:Change title on button click in Shiny Navbar App在 Shiny Navbar App 中单击按钮更改标题
【发布时间】:2020-10-22 20:28:03
【问题描述】:

更改闪亮应用标题的好方法是什么?在下面的示例应用程序中,我有一个 radioButtons 输入,并且我希望根据收音机选择更改应用程序标题(“项目标题”)。

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)

【问题讨论】:

    标签: javascript r shiny


    【解决方案1】:

    您可以使用shiny::textOutputshiny::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)
    

    当您更改您的单选按钮时,input$title_change 也会随之更改。当它发生变化时,它会更新output$title

    【讨论】:

      【解决方案2】:
      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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-15
        • 1970-01-01
        • 2020-09-13
        • 1970-01-01
        • 2020-01-03
        • 2014-02-16
        • 2019-07-06
        • 1970-01-01
        相关资源
        最近更新 更多