我有一个 shiny 应用程序,它通过
我不知道如何有条件地改变背景颜色。我对 CSS 还比较新手。
到目前为止,我已经能够设置背景颜色,但无法通过以下代码在 ui.R 中更改它(其中 dist 是用于显示区域的 htmlOutput 标签):
以下是一个可复制的示例:
htmlOutput
显示一个区域的名称。这些区域对应着 A/B/C 三个类别,基于类别是 A、B 还是 C,我想将 htmlOutput
的背景颜色设为 'red'、'blue' 或者 'green'。我不知道如何有条件地改变背景颜色。我对 CSS 还比较新手。
到目前为止,我已经能够设置背景颜色,但无法通过以下代码在 ui.R 中更改它(其中 dist 是用于显示区域的 htmlOutput 标签):
HTML('
#dist{
background-color: rgba(255,0,255,0.9);
}
')
以下是一个可复制的示例:
请见以下代码:
library(shiny)
ui <- fluidPage(
titlePanel("Test App"),
selectInput("yours", choices = c("India", "Malaysia","Russia","Poland", "Hungary"), label = "Select Country:"),
absolutePanel(id = "controls", class = "panel panel-default", fixed = TRUE,
style="padding-left: 8px; padding-right: 8px; padding-top: 8px; padding-bottom: 8px",
draggable = TRUE, top = 126, left = "auto", right = 20, bottom = "auto",
width = 250, height = "auto",
htmlOutput("sel"), br(),htmlOutput("sel2")
)
)
server <- function(input, output){
catg<- c("A","A","B","C","A")
country <- c("India", "Malaysia","Russia","Poland", "Hungary")
countries <- data.frame(catg,country)
output$sel <- renderText({
paste0("Change my background color and of the text to my right based on variable catg:",input$yours,"-", countries$catg[countries$country==input$yours])
})
output$sel2 <- renderText({
paste0("DON'T change my background color:", countries$catg[countries$country==input$yours])
})
}
# Run the application
shinyApp(ui = ui, server = server)
这里有两个输出变量 - sel,sel2。我想根据变量"catg"改变sel的背景颜色,即如果catg=="A",则为#sel背景颜色为红色,如果catg == "B",则为背景颜色为蓝色等。