我有一个带有多个selectInputs的Shiny Dashabord。现在,如果单击默认值以外的值,我想更改selectInput的背景颜色。
这是一个代码段:
dbHeader <- dashboardHeader(
title = "Test",
titleWidth = "400px"
)
ui <- dashboardPage(
dbHeader,
dashboardSidebar(
uiOutput("filter_head")
),
dashboardBody(
#some content
)
)
server <- function(input, output, session) {
output$filter_head <- renderUI({
column(
12,
selectInput("select_1", h3("Select1"),
c(
"Default", "A", "B",
"C",
"D"
), selected = "Default", selectize = FALSE, multiple = F)
),
selectInput("select_2", h3("Select2"),
c(
"Default", "A", "B",
"C",
"D"
), selected = "Default", selectize = FALSE, multiple = F)
)
)
})
}
shinyApp(ui, server)
因此,如果在select_1上选择了非默认值,则select_1的背景应为红色,否则应为白色。但仅适用于select_1输入。这可能吗?
答案 0 :(得分:0)
好吧,我明白了。带有uiOutput和if else语句。
我创建一个新的uiOutput元素,并在render方法中使用该语句:
uiOutput("style_select_1")
output$style_select_1<- renderUI({
if(is.null(input$select_1)){
return()
}
else if(input$select_1!= 'Default'){
return(tags$style("#select_1{background-color:red;color:white;}"))
}
})