【问题标题】:How to conditionally change Shiny wellPanel() background colour depending on selectInput()?如何根据 selectInput() 有条件地更改 Shiny wellPanel() 背景颜色?
【发布时间】: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或者使用条件面板

标签: r shiny


【解决方案1】:

这样的东西可以解决你的问题

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,uiOutput("well_panel_server"))
      )
    )
  )
)


server <- function(input, output) {

  filtered_data <-  reactive ({
    data <- my_data %>% 
      filter(name %in% input$company)
    data
  })

  color_panel <- reactive(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==""){
      "Select Company"
    }
    else(
      filtered_data() %>%
        pull(price) %>% 
        as.character()
    )
  })  


  output$well_panel_server <- renderUI({
      wellPanel(textOutput("price"),style = color_panel())
    })
}

shinyApp(ui, server)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-04
    • 1970-01-01
    • 2011-04-23
    • 1970-01-01
    • 2023-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多