将标题放置在shinydashboard的标题中

6l7fqoea  于 2023-02-01  发布在  其他
关注(0)|答案(2)|浏览(176)

拥有这个基础的闪亮应用程序:我想把我的职位放在标题中,如下图中红色所示:
已经有一些解决方案Add text on right of shinydashboard header,但我想知道是否有一个更“直”的方式?
@matrixloading的解决方案很吸引人,但并不令人满意,因为文本前面有一个点:

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"),
  dashboardSidebar(),
  dashboardBody(
    # Boxes need to be put in a row (or column)
    fluidRow(
      box(plotOutput("plot1", height = 250)),
      
      box(
        title = "Controls",
        sliderInput("slider", "Number of observations:", 1, 100, 50)
      )
    )
  )
)

server <- function(input, output) {
  set.seed(122)
  histdata <- rnorm(500)
  
  output$plot1 <- renderPlot({
    data <- histdata[seq_len(input$slider)]
    hist(data)
  })
}

shinyApp(ui, server)

jdg4fx2g

jdg4fx2g1#

我们可以在dashboardHeader的de nav元素中添加一个子元素。

dashboardHeader(title = "Basic dashboard") |>
    tagAppendChild(
      div(
        "This is My Title",
        style = "
      display: block;
      font-size: 1.5em;
      margin-block-start: 0.5em;
      font-weight: bold;
      color: darkred;
      margin-right: 50%",
        align = "right"
      ),
      .cssSelector = "nav"
    )

wyyhbhjk

wyyhbhjk2#

CSS的text-align属性可以用来使标题居中,我使用了titlePanel函数的title参数来调整代码。
下面是调整的代码:

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    titlePanel(title = tags$h2(
      tags$b("Title for the Basic Dashboard"),
      tags$style(HTML("h2 { text-align: center; }"))
    )
    ),
    fluidRow(
      box(plotOutput("plot1", height = 250)),
      
      box(
        title = "Controls",
        sliderInput("slider", "Number of observations:", 1, 100, 50)
      )
    )
  )
)

server <- function(input, output) {
  set.seed(122)
  histdata <- rnorm(500)
  
  output$plot1 <- renderPlot({
    data <- histdata[seq_len(input$slider)]
    hist(data)
  })
}

shinyApp(ui, server)

相关问题