如何在 Shinydashboard R 中仪表板标题的中心显示图像?

RL_*_*Pug 2 r shiny shinydashboard

我有以下代码,可以制作一个简单的闪亮应用程序。

```
library(shinydashboard)
library(shiny)
ui <- dashboardPage(
  dashboardHeader(title = tags$img(src='https://cdn.vox-cdn.com/thumbor/Ous3VQj1sn4tvb3H13rIu8eGoZs=/0x0:2012x1341/1400x788/filters:focal(0x0:2012x1341):format(jpeg)/cdn.vox-cdn.com/uploads/chorus_image/image/47070706/google2.0.0.jpg', height = '60', width ='100')),
  dashboardSidebar(
    sidebarMenuOutput("menu")
  ),
  dashboardBody()
)

server <- function(input, output) {
  output$menu <- renderMenu({
    sidebarMenu(
      menuItem("Overview", icon = icon("tachometer"))
      
    )
  })
}
shinyApp(ui, server)
```
Run Code Online (Sandbox Code Playgroud)

图像输出在右侧菜单的顶部,但我的目标是让图像更多地位于仪表板的中间。我知道菜单稍微改变了导航栏,但我想尽可能将其保持在中心位置。

在此输入图像描述

但我想要的输出是这样的。我用油漆做了一个样品。是否仍然可以有一些文本,或者如果可以发布参考,让我可以了解有关仪表板标题功能的更多信息,我将不胜感激。

在此输入图像描述

lz1*_*100 7

干得好

您无法使用 from 的函数将图像添加到右侧的标题部分,但让我们通过将样式和标签注入标题shinydashboard来享受最新的乐趣。htmltools

library(shinydashboard)
library(shiny)
header_img <- tags$img(
    src='https://cdn.vox-cdn.com/thumbor/Ous3VQj1sn4tvb3H13rIu8eGoZs=/0x0:2012x1341/1400x788/filters:focal(0x0:2012x1341):format(jpeg)/cdn.vox-cdn.com/uploads/chorus_image/image/47070706/google2.0.0.jpg',
    style = 'height: 50px; width: 100px; position: absolute; left: 50%; transform: translateX(-50%);'
)
header <-  htmltools::tagQuery(dashboardHeader(title = ""))
header <- header$
    addAttrs(style = "position: relative")$ # add some styles to the header 
    find(".navbar.navbar-static-top")$ # find the header right side
    append(header_img)$ # inject our img
    allTags()

ui <- dashboardPage(
    header,
    dashboardSidebar(
        sidebarMenuOutput("menu")
    ),
    dashboardBody()
)

server <- function(input, output) {
    output$menu <- renderMenu({
        sidebarMenu(
            menuItem("Overview", icon = icon("tachometer"))
            
        )
    })
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

img 放置在右侧标题的中心,而不是整个标题长度的中心。如果你想调整到整个长度的中心,尝试将translateX(-50%)img更改为你喜欢的数字。

在此输入图像描述