如何在ShinyDashboard R中的仪表板标题中心显示图像? [英] How have a image in the center of the dashboard header in Shinydashboard R?

查看:0
本文介绍了如何在ShinyDashboard R中的仪表板标题中心显示图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码可以创建一个简单闪亮的应用程序。

```
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)
```

图像输出在右侧菜单的顶部,但我的目标是使图像更多地位于仪表板的中间。我知道菜单会使导航略有变化,但我希望尽可能将其保持在中心位置。

但我想要的输出应该是这样的。我用油漆做了一个样品。是否可以保留一些文本,或者如果可以发布引用以了解有关仪表板标题函数的更多信息,我将不胜感激。

推荐答案

给您

您无法使用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)

IMG放置在右侧页眉的中心,而不是整个页眉长度的中心。如果要调整到整个长度的中心,请尝试将img的translateX(-50%)更改为您喜欢的数字。

这篇关于如何在ShinyDashboard R中的仪表板标题中心显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆