【问题标题】:Make the first element of a selectInput in R shiny appear bold使 R 中 selectInput 的第一个元素闪亮显示为粗体
【发布时间】:2018-03-07 21:10:47
【问题描述】:

我希望将 selectInput 的第一个元素“1”设置为粗体。请帮忙。

ui <- fluidPage(
selectInput(
"select",
label = h3("Select box"),
choices = c(1,2,3,4)
))
server <- function(input, output) {
}
shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: css r shiny shinydashboard


    【解决方案1】:

    看看shinyWidgets 包,它的pickerInput 有很多很酷的功能

    rm(list = ls())
    library(shiny)
    library(shinyWidgets)
    
    ui <- fluidPage(
      pickerInput(inputId = "Id069", 
                  label = "Style individual options with HTML", 
                  choices = c("steelblue 150%", 
                              "right align + red", "bold", 
                              "background color"), choicesOpt = list(style = c("color: steelblue; font-size: 150%;", 
                                                                               "color: firebrick; text-align: right;", 
                                                                               "font-weight: bold;", "background: forestgreen; color: white;")))
    
      )
    server <- function(input, output) {
    }
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 谢谢,但我对 selectInput 有进一步的依赖,请帮助我只将 selectInput 的第一个元素设为粗体,谢谢。
    • shinyWidgets 包有你需要的功能,而不是使用selectInput 使用pickerInput。如果我给你答案,你什么都学不到
    • 多年来我一直在寻找解决方案,我不使用pickerInput是有原因的,因为这个要求是更大要求的一部分,还需要检查外观和感觉透视图,检查这个链接,该解决方案在 html 中有一个修复,但是我希望在 R stackoverflow.com/questions/48514038/… 中隐藏相同的内容。请帮忙。
    【解决方案2】:

    您可以在闪亮的应用中添加 @Nitin Shinde 建议的样式,如下所示:

    ui <- fluidPage(
    
      tags$head(tags$style(".option:first-child{
        font-weight:bold;
        //color:#ff0000;
      }")),
      selectInput(
        "select",
        label = h3("Select box"),
        choices = c(1,2,3,4)
      ))
    server <- function(input, output) {
    }
    shinyApp(ui = ui, server = server)
    

    输出会是这样的:

    【讨论】:

    • 这很好,但是如果有多个 selectInputs,那么我看到所有 selectInputs 的第一个元素颜色为黑色,你能帮我将更改应用到第一个特定的 selectInput 吗?请检查编辑
    • 因为 first-child 属性引用了 selectInput 中的第一个元素,类似地,它将引用第二个或第三个 selectInput 的第一个值,请帮忙?
    • 我做了一些研究,并想出了这个 css 标签,它实际上适用于 html 选择标签,select:nth-child(4) option:first-child{ font-weight:bold;颜色:#000000; } 请帮我把它放在 R 中。
    【解决方案3】:

    你可以在 CSS 中使用伪元素

    <style>
        option:first-child{
        font-weight:bold;
        color:#ff0000;
        }
    </style>
    

    【讨论】:

    • 感谢您在这里回复,但我需要将其嵌入到 R 中,请帮忙?
    • 如果我有多个 selectInputs,那么我应该如何访问特定输入的第一个子项?
    【解决方案4】:

    您可以使用下面的方法并将每个 selectInput 嵌套在 div 中,其中 class= "test" 用于您希望第一个项目加粗的每个。

    ui <- fluidPage(
    
      tags$head(tags$style(".test .option:first-child{
                           font-weight:bold;
                           //color:#ff0000;
                           }")),
      div(class = "test",selectInput(
        "select",
        label = h3("Select box"),
        choices = c(1,2,3,4)
      )),
      selectInput(
        "select2",
        label = h3("Select box"),
        choices = c(1,2,3,4)
      )
    
      )
    server <- function(input, output) {
    }
    shinyApp(ui = ui, server = server)
    

    您可以将 div 的类设置为您喜欢的任何内容,只需确保相应地更改 CSS 的 .test 部分。

    更新“//颜色:#ff0000;”到“颜色:#ff0000;”会将颜色更改为红色,只需将十六进制代码更新为您想要使用的任何颜色即可。

    【讨论】:

      猜你喜欢
      • 2015-05-07
      • 2018-07-07
      • 2016-01-28
      • 1970-01-01
      • 2016-08-12
      • 2018-09-21
      • 2016-01-24
      • 2016-07-28
      • 2018-07-07
      相关资源
      最近更新 更多