【问题标题】:Shiny: How to change the page/window title in Shiny?Shiny:如何更改 Shiny 中的页面/窗口标题?
【发布时间】:2020-11-25 01:11:38
【问题描述】:

关于更改其他 Shiny 应用程序标题的帖子有很多,例如:
Change the title by pressing a shiny button Shiny R
Shiny page title and image
Shiny App: How to dynamically change box title in server.R?

我的问题是相关的,但其中任何一个都没有回答。我想让<head><title>...</title></head> 标记具有反应性,或者至少可以从server.R 中的observeEvent 中控制。

以下方法不起作用,因为 ui 找不到 theTitle,但我希望这种方法可行:

library(shiny)

ui <- fluidPage(
   title = theTitle(),
   textInput("pageTitle", "Enter text:")
)

server <- function(input, output, session) {
    theTitle <- reactiveVal()
    
    observeEvent( input$pageTitle, {
      if(is.null(input$pageTitle)) {
        theTitle("No title yet.")
      } else {
        theTitle(input$pageTitle)
      }
    })
}

我尝试在observeEvent 中使用if..else 逻辑创建output$theTitle &lt;- renderText({...}),然后在uifluidPage 中设置title = textOutput("theTitle"),但这会生成&lt;div ...&gt; 作为标题文本,或&lt;span ...&gt;,如果我们将inline=True 传递给renderText

如果这澄清了我正在寻找的内容,那么答案将等同于 literal(用该字符串替换字符串变量)ui 生成的

ui <- fluidPage(
    title = "No title yet.",
    ....
)

在用户在框中输入任何文本之前;如果他们输入了“Shiny is great!”进入input$pageTitle的盒子,然后我们会得到文字

ui <- fluidPage(
    title = "Shiny is great!",
    ....
)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    截至 2021 年 6 月,有一个名为 shinytitle 的 R 包可以从 Shiny 的反应式上下文中更新窗口标题:https://cran.r-project.org/package=shinytitle

    【讨论】:

      【解决方案2】:

      一种方法是编写一些 javascript 来解决这个问题。例如

      ui <- fluidPage(
        title = "No title yet.",
        textInput("pageTitle", "Enter text:"),
        tags$script(HTML('Shiny.addCustomMessageHandler("changetitle", function(x) {document.title=x});'))
      )
      
      server <- function(input, output, session) {
        observeEvent( input$pageTitle, {
          title <- if(!is.null(input$pageTitle) && nchar(input$pageTitle)>0) {
            input$pageTitle
          } else {
            "No title yet."
          }
          session$sendCustomMessage("changetitle", title)
        })
      }
      
      shinyApp(ui, server)
      

      这是在 How to send messages from the browser to the server and back using Shiny guide 之后创建的

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-17
        • 1970-01-01
        • 2016-05-09
        • 2021-12-06
        • 1970-01-01
        • 2017-05-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多