只更改导航栏颜色。

huangapple go评论73阅读模式
英文:

Bslib change only navbar color

问题

在我的Shiny应用中,我有一个选项可以更改应用的主题。是否有一种方法可以创建一个主题,其中只有导航栏有颜色,而主体是黑色或白色的?我知道可以使用CSS来做到这一点,但由于我可以在应用中更改主题,所以无法进行自定义修复。

英文:

In my shiny app, I have an option allowing to change the theme app.
Is there a way to create a theme where only the navbar is colored and the body is black or white ?
I know it's possible to do it with CSS but as I could change the theme in the app it's not possible to do a custom fix.

只更改导航栏颜色。

library(shiny)
library(markdown)
library(bslib)

main_theme = bslib::bs_theme(
  bg = "#86cecb",
  fg = "#e12885",
  primary = "#89cff0",
)

ui = navbarPage(
  "Navbar!",
  theme = main_theme,
  tabPanel("Plot",
           sidebarLayout(
             sidebarPanel(radioButtons(
               "plotType", "Plot type",
               c("Scatter" = "p", "Line" = "l")
             )),
             mainPanel(plotOutput("plot"))
           )),
  navbarMenu(title = "Ressources",
             tabPanel(
               "Options d'interface",
               selectInput(
                 "theme",
                 "Themes :",
                 c(
                   "Custom" = "custom",
                   "Dark" = "dark",
                   "Light" = "light"
                 )
               )
             ))
)

server = function(input, output, session) {
  light <- bs_theme()
  
  dark <- bslib::bs_theme(
    bg = "#222222",
    fg = "#FFFFFF",
    primary = "#FFFFFF",
    secondary = "#434343"
  )
  main_theme = bslib::bs_theme(
    bg = "#86cecb",
    fg = "#e12885",
    primary = "#89cff0",
  )
  
  observe(session$setCurrentTheme({
    if (input$theme == "dark")
      dark
    else if (input$theme == "light")
      light
    else if (input$theme == "custom")
      main_theme
  }))
}

if (interactive())
  shinyApp(ui, server)

答案1

得分: 3

你可以使用 Bootstrap 变量 navbar-bg 单独设置导航栏的背景颜色:

library(shiny)
library(markdown)
library(bslib)

ui <- navbarPage(
  "导航栏!",
  theme = bslib::bs_theme(
    "navbar-bg" = "#86cecb",
    bg = "#222222",
    fg = "#e12885",
    primary = "#89cff0",
  ),
  tabPanel(
    "图表",
    sidebarLayout(
      sidebarPanel(radioButtons(
        "plotType", "图表类型",
        c("散点图" = "p", "折线图" = "l")
      )),
      mainPanel(plotOutput("plot"))
    )
  ),
  navbarMenu(
    title = "资源",
    tabPanel(
      "界面选项",
      selectInput(
        "theme",
        "主题:",
        c(
          "自定义" = "custom",
          "暗色" = "dark",
          "亮色" = "light"
        )
      )
    )
  )
)

server <- function(input, output, session) {
  
}

shinyApp(ui, server)
#> 
#> 正在监听 http://127.0.0.1:5052

只更改导航栏颜色。<!-- -->

英文:

You could set the background color for the navbar individually using the Bootstrap variable navbar-bg:

library(shiny)
library(markdown)
library(bslib)

ui &lt;- navbarPage(
  &quot;Navbar!&quot;,
  theme = bslib::bs_theme(
    &quot;navbar-bg&quot; = &quot;#86cecb&quot;,
    bg = &quot;#222222&quot;,
    fg = &quot;#e12885&quot;,
    primary = &quot;#89cff0&quot;,
  ),
  tabPanel(
    &quot;Plot&quot;,
    sidebarLayout(
      sidebarPanel(radioButtons(
        &quot;plotType&quot;, &quot;Plot type&quot;,
        c(&quot;Scatter&quot; = &quot;p&quot;, &quot;Line&quot; = &quot;l&quot;)
      )),
      mainPanel(plotOutput(&quot;plot&quot;))
    )
  ),
  navbarMenu(
    title = &quot;Ressources&quot;,
    tabPanel(
      &quot;Options d&#39;interface&quot;,
      selectInput(
        &quot;theme&quot;,
        &quot;Themes :&quot;,
        c(
          &quot;Custom&quot; = &quot;custom&quot;,
          &quot;Dark&quot; = &quot;dark&quot;,
          &quot;Light&quot; = &quot;light&quot;
        )
      )
    )
  )
)

server &lt;- function(input, output, session) {
  
}

shinyApp(ui, server)
#&gt; 
#&gt; Listening on http://127.0.0.1:5052

只更改导航栏颜色。<!-- -->

huangapple
  • 本文由 发表于 2023年3月7日 23:34:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75664021.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定