如何在shinydashboard的Header中定位标题?寻求更简洁实现方案
在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)
更直接的实现方案
可以直接借助dashboardHeader的自定义标签功能,在头部右侧添加标题并消除圆点样式,代码如下:
library(shiny) library(shinydashboard) ui <- dashboardPage( dashboardHeader( title = "Basic dashboard", # 自定义右侧标题,同步消除列表圆点 tags$li(class = "dropdown", tags$span(style = "padding: 15px; font-size: 18px; color: white;", "右侧标题"), tags$style(".main-header .navbar-nav .dropdown { list-style-type: none; }") ) ), dashboardSidebar(), dashboardBody( 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)
方案说明
- 用
tags$li(class = "dropdown")将标题嵌入头部右侧的导航栏区域,和原有组件风格统一 - 通过内联CSS
list-style-type: none彻底消除列表项的圆点 - 可调整
padding、font-size、color等样式参数,让右侧标题和左侧标题视觉一致
内容的提问来源于stack exchange,提问作者TarJae
相关产品推荐
相关产品推荐

