首页 > 解决方案 > 闪亮仪表板选项卡上的不同背景

问题描述

我想创建一个闪亮的仪表板,其中第一个选项卡的背景包含一张图片,存储在本地,其余选项卡包含纯白色背景。

我试图从这里修改代码以显示图像而不是颜色:

library(shiny)
library(shinydashboard)
ui <- dashboardPage(dashboardHeader(dropdownMenuOutput("notificationMenu")
                                    ), 
                    dashboardSidebar(sidebarMenu(id='sidebar',
                                                 menuItem("Page 1", tabName = "page1"),
                                                 menuItem("Page 2", tabName = "page2")
                                                 ),
                                     uiOutput('style_tag')
                                     ),
                    dashboardBody(
                      tabItems(
                        tabItem(tabName = "page1", h4("Picture!",style='color:black')),
                        tabItem(tabName = "page2", h4('white!')
                                )
                        )
                      )
                    )

server <- function(input, output, session){
  
  output$style_tag <- renderUI({
    if(input$sidebar=='page1')
      return(tags$head(tags$style(HTML('.content-wrapper {
                                             background-image:url("~/www/background.jpg") fixed center;
                                             }')
                                  )
                       )
             )
    
    if(input$sidebar=='page2')
      return(tags$head(tags$style(HTML('.content-wrapper {background-color:white;}')
                                  )
                       )
             )
  })
}

shinyApp(ui = ui, server = server)

但是,它不起作用。我在 CSS 位中做错了吗?有没有更好的方法来实现同样的事情?

标签: rshinyshinydashboard

解决方案


您的路径是从您的 R 工作目录的角度来看的。它需要从浏览器的角度来看。更改此 CSS:

background-image:url("background.jpg")

推荐阅读