首页 > 解决方案 > 如何让 R Shiny 标题面板从服务器中提取文本?

问题描述

有没有办法让动态 titlePanel 标题直接从 UI 中提取,如下所示?如果不可能,是否可以在标题面板正下方有类似于标题面板的第二行?

# Define UI ----
ui <- fluidPage(
  ##Whatever UI code here
  titlepanel_text = paste0("Some string", variable_with_text)
)

# Define server logic ----
server <- function(input, output) {
    titlePanel("title panel"),
   #Rest of server code here

}

标签: rshiny

解决方案


在服务器中渲染文本并在 UI 中抓取文本输出:

library(shiny)
# Define UI ----
ui <- fluidPage(
  ##Whatever UI code here
  titlePanel(textOutput("title_panel")),
  sidebarLayout(
    sidebarPanel(),
    mainPanel(h1("text"))
  )
)

# Define server logic ----
server <- function(input, output) {

  output$title_panel <- renderText({
    paste0("This is the date/time: ", Sys.time() )
  })

}

shiny::shinyApp(ui, server)

推荐阅读