【发布时间】:2020-10-07 08:33:21
【问题描述】:
我创建了可重现的代码来突出我遇到的问题。我知道要更改wellPanel() 的背景颜色,例如,我使用wellPanel(...,style = "background: green")。
但是,我希望根据从我的数据框中的公司选项中选择的公司 (selectInput()) 更改 wellPanel() 的背景颜色。所以,我在ui中调用了wellPanel(...,style=textOutput("colour_panel")),然后在服务器中定义了output$colour_panel,其值取决于选择的公司。
为什么背景颜色不会改变?
library(shiny)
library(dplyr)
name <- c("Company1","Company2","Company3")
price <- c("400","200","150")
my_data <- data.frame(name,price)
ui <- fluidPage(
h1("Shiny Template"),
sidebarLayout(
sidebarPanel(
selectInput("company", "Choose a Company:", choices = c(Choose="",levels(as.factor(my_data$name))))
),
mainPanel(
fluidRow(
column(4,
wellPanel(
textOutput("price"),
style=textOutput("colour_panel")
))
)
)
)
)
server <- function(input, output) {
filtered_data <- reactive ({
data <- my_data %>%
filter(name==input$company)
data
})
output$colour_panel <- renderText({
ifelse(input$company=='',
paste0("background: grey"),
ifelse(
input$company=="Company1" | input$company=="Company2",
paste0("background: green"),
paste0("background: red")))
})
output$price <- renderText({
if(input$company==""){
return()
}
else(
filtered_data() %>%
select(price) %>%
as.integer()
)
})
}
shinyApp(ui, server)
【问题讨论】:
-
shinyjs 可能是你的答案。 this 的变体几乎肯定会给你想要的。
-
你需要在服务端渲染UI或者使用条件面板