只更改导航栏颜色。

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

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.

只更改导航栏颜色。

  1. library(shiny)
  2. library(markdown)
  3. library(bslib)
  4. main_theme = bslib::bs_theme(
  5. bg = "#86cecb",
  6. fg = "#e12885",
  7. primary = "#89cff0",
  8. )
  9. ui = navbarPage(
  10. "Navbar!",
  11. theme = main_theme,
  12. tabPanel("Plot",
  13. sidebarLayout(
  14. sidebarPanel(radioButtons(
  15. "plotType", "Plot type",
  16. c("Scatter" = "p", "Line" = "l")
  17. )),
  18. mainPanel(plotOutput("plot"))
  19. )),
  20. navbarMenu(title = "Ressources",
  21. tabPanel(
  22. "Options d'interface",
  23. selectInput(
  24. "theme",
  25. "Themes :",
  26. c(
  27. "Custom" = "custom",
  28. "Dark" = "dark",
  29. "Light" = "light"
  30. )
  31. )
  32. ))
  33. )
  34. server = function(input, output, session) {
  35. light <- bs_theme()
  36. dark <- bslib::bs_theme(
  37. bg = "#222222",
  38. fg = "#FFFFFF",
  39. primary = "#FFFFFF",
  40. secondary = "#434343"
  41. )
  42. main_theme = bslib::bs_theme(
  43. bg = "#86cecb",
  44. fg = "#e12885",
  45. primary = "#89cff0",
  46. )
  47. observe(session$setCurrentTheme({
  48. if (input$theme == "dark")
  49. dark
  50. else if (input$theme == "light")
  51. light
  52. else if (input$theme == "custom")
  53. main_theme
  54. }))
  55. }
  56. if (interactive())
  57. shinyApp(ui, server)

答案1

得分: 3

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

  1. library(shiny)
  2. library(markdown)
  3. library(bslib)
  4. ui <- navbarPage(
  5. "导航栏!",
  6. theme = bslib::bs_theme(
  7. "navbar-bg" = "#86cecb",
  8. bg = "#222222",
  9. fg = "#e12885",
  10. primary = "#89cff0",
  11. ),
  12. tabPanel(
  13. "图表",
  14. sidebarLayout(
  15. sidebarPanel(radioButtons(
  16. "plotType", "图表类型",
  17. c("散点图" = "p", "折线图" = "l")
  18. )),
  19. mainPanel(plotOutput("plot"))
  20. )
  21. ),
  22. navbarMenu(
  23. title = "资源",
  24. tabPanel(
  25. "界面选项",
  26. selectInput(
  27. "theme",
  28. "主题:",
  29. c(
  30. "自定义" = "custom",
  31. "暗色" = "dark",
  32. "亮色" = "light"
  33. )
  34. )
  35. )
  36. )
  37. )
  38. server <- function(input, output, session) {
  39. }
  40. shinyApp(ui, server)
  41. #>
  42. #> 正在监听 http://127.0.0.1:5052

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

英文:

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

  1. library(shiny)
  2. library(markdown)
  3. library(bslib)
  4. ui &lt;- navbarPage(
  5. &quot;Navbar!&quot;,
  6. theme = bslib::bs_theme(
  7. &quot;navbar-bg&quot; = &quot;#86cecb&quot;,
  8. bg = &quot;#222222&quot;,
  9. fg = &quot;#e12885&quot;,
  10. primary = &quot;#89cff0&quot;,
  11. ),
  12. tabPanel(
  13. &quot;Plot&quot;,
  14. sidebarLayout(
  15. sidebarPanel(radioButtons(
  16. &quot;plotType&quot;, &quot;Plot type&quot;,
  17. c(&quot;Scatter&quot; = &quot;p&quot;, &quot;Line&quot; = &quot;l&quot;)
  18. )),
  19. mainPanel(plotOutput(&quot;plot&quot;))
  20. )
  21. ),
  22. navbarMenu(
  23. title = &quot;Ressources&quot;,
  24. tabPanel(
  25. &quot;Options d&#39;interface&quot;,
  26. selectInput(
  27. &quot;theme&quot;,
  28. &quot;Themes :&quot;,
  29. c(
  30. &quot;Custom&quot; = &quot;custom&quot;,
  31. &quot;Dark&quot; = &quot;dark&quot;,
  32. &quot;Light&quot; = &quot;light&quot;
  33. )
  34. )
  35. )
  36. )
  37. )
  38. server &lt;- function(input, output, session) {
  39. }
  40. shinyApp(ui, server)
  41. #&gt;
  42. #&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:

确定