简体   繁体   中英

Different backgrounds on shinydashboard tabs

I would like to create a shinydashboard where the background of the first tab contains a picture, stored locally, and the remaining tabs contain a plain white background.

I tried to modify code from here to dispay an image instead of the color:

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)

However, it does not work. Am I doing something wrong in the CSS-bit? Is there any better ways of acheiving the same thing?

Your path is from the perspective of your R working directory. It needs to be from the perspective of the browser. Change this CSS:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM