How to change text colour of links in navbar header AND links in nav pills (in shiny app)?

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

How to change text colour of links in navbar header AND links in nav pills (in shiny app)?

问题

这是我的shiny应用程序的删减版本:

library(shiny)
library(bslib)

ui <- tagList(
  fluidPage(
    titlePanel(""),
    tags$head(tags$style(HTML(
      "
        .navbar-default {
            color: red !important;
        }
        .navbar-default .navbar-nav > .active > a,
        .navbar-default .navbar-nav > li > a:hover {
            color: red !important;
        }
        .navbar-default .navbar-nav > li > a {
            color: pink !important;
        }
        "
    ))),
    
    navbarPage(
      windowTitle = "App Name",
      theme = bs_theme(bootswatch = "flatly",
                       base_font = font_google("Lato"),
                       primary = "#333F50",
                       bg = "white",
                       fg = "#D67540"),
      title = "I am the title",
      selected = "Main Tab 1",
      tabPanel(title = "Main Tab 1",
               fluidPage(
                 sidebarLayout(
                   sidebarPanel(textInput(inputId = "text_input", label = "Enter text:")),
                   mainPanel(textOutput(outputId = "text_output"))
                 )
               )
               ),
      tabPanel(title = "Main Tab 2",
               fluidPage(
                   fluidRow(
                     column(7,
                            navlistPanel(
                              tabPanel("Tab 1"),
                              tabPanel("Tab 2"),
                              tabPanel("Tab 3"),
                              widths = c(2, 10),
                              well = FALSE)
                            )))
               )
      )
    )
  )

server <- function(input, output){
  output$text_output <- renderText(input$text_input)
}

shinyApp(ui, server)

目前,“Main Tab 1”和“Main Tab 2”的文本在悬停/选定时为粉红色和红色。

我还想对“Tab 1”、“Tab 2”和“Tab 3”应用类似的更改 - 对于这些标签,我希望在悬停/选定时将文本颜色从深蓝变为黑色,从白色变为橙色。

我理解color值旁边的!important会强制所有链接继承相同的属性,但我希望导航栏标题中的链接和导航选项卡中的链接具有不同的颜色。

我在这里提出了相关问题:
https://stackoverflow.com/questions/75488188/how-to-change-text-colour-of-navbarpage-links-when-hovered-on-in-shiny-app

如何进行这种更改?

非常感谢任何帮助 How to change text colour of links in navbar header AND links in nav pills (in shiny app)?

注意:颜色意在不美观(只是为了区分,因为我还在学习)

英文:

Here is a redacted version of my shiny app:

library(shiny)
library(bslib)

ui <- tagList(
  fluidPage(
    titlePanel(""),
    tags$head(tags$style(HTML(
      "
        .navbar-default {
            color: red !important;'
        }
        .navbar-default .navbar-nav > .active > a,
        .navbar-default .navbar-nav > li > a:hover {
            color: red !important;
        }
        .navbar-default .navbar-nav > li > a {
            color: pink !important;
        }
        "
    ))),
    
    navbarPage(
      windowTitle = "App Name",
      theme = bs_theme(bootswatch = "flatly",
                       base_font = font_google("Lato"),
                       primary = "#333F50",
                       bg = "white",
                       fg = "#D67540"),
      title = "I am the title",
      selected = "Main Tab 1",
      tabPanel(title = "Main Tab 1",
               fluidPage(
                 sidebarLayout(
                   sidebarPanel(textInput(inputId = "text_input", label = "Enter text:")),
                   mainPanel(textOutput(outputId = "text_output"))
                 )
               )
               ),
      tabPanel(title = "Main Tab 2",
               fluidPage(
                   fluidRow(
                     column(7,
                            navlistPanel(
                              tabPanel("Tab 1"),
                              tabPanel("Tab 2"),
                              tabPanel("Tab 3"),
                              widths = c(2, 10),
                              well = FALSE)
                            )))
               )
      )
    )
  )

server <- function(input, output){
  output$text_output <- renderText(input$text_input)
}

shinyApp(ui, server)

Currently the text for "Main Tab 1" and "Main Tab 2" is pink and red when hovered/selected.

How to change text colour of links in navbar header AND links in nav pills (in shiny app)?

I would also like to apply a similar change to "Tab 1", "Tab 2" and "Tab 3" - for these I want the text colour to change from teal to black and from white to orange when hovered/selected.

My understanding is that the !important next to the color value forces all links to inherit the same properties, but I would like different colours for the links in the navbar header and the links in the nav-pills.

I have asked a related question here:
https://stackoverflow.com/questions/75488188/how-to-change-text-colour-of-navbarpage-links-when-hovered-on-in-shiny-app

How can I make this change?

Any help is much appreciated How to change text colour of links in navbar header AND links in nav pills (in shiny app)?

Note: Colours are intentionally not aesthetic (just using these for distinction as I'm still learning)

答案1

得分: 1

好的 - 我已经实现了您的请求,但请查看我的答案的两个部分:

部分1 - 解决方案

为了实现您的请求的样式,我做了两件事:

  1. 我将您的 navListPanel 更改为插入到具有类名 side_paneltags$div 中。我将这个 navlistPanel 放在一个带有自定义类的 div 中,以便您想要的面板样式不会影响到其他 .nav 类,包括主要的 .nav-header
tags$div(
  navlistPanel(
    tabPanel("标签 1"),
    tabPanel("标签 2"),
    tabPanel("标签 3"),
    widths = c(2, 10),
    well = FALSE),
  class = "side_panel"
)
  1. 然后,我在您的 tags$style 中添加了以下CSS,每行都以 .side_panel 开头,只影响我们刚刚创建的类:
.side_panel .nav-pills a:hover {
    color: black; 
    background: rgba(253,126,20, 0.8);
}

.side_panel .nav-pills .nav-link.active {
  color: black;
  background: #fd7e14; /* BS 橙色 */
}

实现了这个结果
(注意: "标签 2" 正在被悬停,截图没有捕捉到我的鼠标):

How to change text colour of links in navbar header AND links in nav pills (in shiny app)?

部分2 - 未来的样式

由于您似乎对您的应用程序的样式非常挑剔(不用担心,我也对这种类型的样式非常挑剔),您应该考虑两件事。

  1. 而不是在应用程序的开头插入所有CSS,考虑创建一个位于 app.R 文件相同目录中的 www 文件夹中的单独的 style.css 文件。然后,您可以使用 shiny::includeCSS("www/style.css") (参考链接) 将所有这些CSS注入到您的应用程序中。

  2. 为了学习如何在将来为您的Shiny应用程序自定义组件样式,您应该熟悉您浏览器的 Inspect 功能。我在我的应用程序中主要使用 bs4Dash,所以对于您的应用程序的HTML组件我不太熟悉。要找到对象和它们的CSS,我简单地检查了网页(右键单击 + 检查),然后使用 ctrl+shift+c 单击选项卡并查看它们的CSS。更进一步,您可以通过输入自定义值来实时更改CSS的样式。

祝你好运!

英文:

Ok - I've implemented your request, but see both parts of my answer:

Part 1 - The Solution

To implement your requested styles, I did two things:

  1. I changed your navListPanel by inserting it into a tags$div with the class name side_panel. I housed this navlistPanel in a div with a custom class so that the CSS you wanted for the panel didn't bleed into other .nav classes, including the main .nav-header.
tags$div(
  navlistPanel(
    tabPanel("Tab 1"),
    tabPanel("Tab 2"),
    tabPanel("Tab 3"),
    widths = c(2, 10),
    well = FALSE),
  class = "side_panel"
)
  1. I then added the following CSS to your tags$style, starting each line with .side_panel to only affect the class we just created:
.side_panel .nav-pills a:hover {
    color: black; 
    background: rgba(253,126,20, 0.8);
}

.side_panel .nav-pills .nav-link.active {
  color: black;
  background: #fd7e14; /* BS orange */
}

Achieving this result
(Note: "Tab 2" is being hovered over, the screenshot didn't capture my mouse):

How to change text colour of links in navbar header AND links in nav pills (in shiny app)?

Part 2 - Future Styling

As it seems like you're particular about the style of your app (don't worry, I'm obsessive about this type of styling too), you should consider two things.

  1. Rather than inserting all of your CSS at the beginning of your app, consider creating a separate style.css file located in a www folder. This www folder should be in the same directory as your app.R file. You can then inject all of this CSS into your app using shiny::includeCSS("www/style.css") (ref link).

  2. To learn how to stylize custom components of your Shiny apps in the future, you should familiarize yourself with the Inspect feature of your browser. I mostly use bs4Dash in my apps, so I wasn't as familiar with your app's html components. To find the objects and their CSS, I simply inspected the webpage (right click + inspect) and use ctrl+shift+c to click on the tabs and view their CSS. Going one step further, you can change the style of the CSS on the fly by typing in custom values.

Good luck!

huangapple
  • 本文由 发表于 2023年2月18日 03:50:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75488676.html
匿名

发表评论

匿名网友

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

确定