是否可以将ShinyWidgets中progressBar()的颜色更改为自定义颜色?

时间:2018-03-26 15:15:02

标签: css r shiny

我想将shinyBar()的colorsBar()颜色更改为自定义颜色。我知道可以使用 status 更改许多默认颜色,但我想将其更改为不可用的颜色。

我是CSS的新手,所以我检查了git存储库本身的here以查看是否可以覆盖CSS。我试图覆盖CSS(在progressBar()之前添加它):

tagss$style('.progress-bar-status{color: #25c484;
                background-color: #25c484;}')

但它没有用。 有谁知道如何做到这一点?感谢。

2 个答案:

答案 0 :(得分:3)

您可以使用自定义status并定义相应的CSS,如下所示:

library("shiny")
library("shinyWidgets")

ui <- fluidPage(
  column(
    width = 7,
    tags$b("Other options"), br(),
    progressBar(
      id = "pb2",
      value = 0,
      total = 100,
      title = "",
      display_pct = TRUE, 
      status = "custom"
    ),
    tags$style(".progress-bar-custom {background-color: #25c484;}"),
    actionButton(
      inputId = "go",
      label = "Launch calculation"
    )
  )
)

server <- function(input, output, session) {

  observeEvent(input$go, {
    for (i in 1:100) {
      updateProgressBar(
        session = session,
        id = "pb2",
        value = i, total = 100
      )
      Sys.sleep(0.1)
    }
  })
}

shinyApp(ui = ui, server = server)

答案 1 :(得分:2)

您的解决方案几乎可以使用,但是您应该修改progress-bar类的属性,例如:

tags$head(tags$style(HTML('.progress-bar {background-color: red;}')))

我通过从here获取示例并将Sys.sleep语句修改为60秒来找到这一点,以便我们可以更轻松地检查页面。

下面给出了一个工作示例,希望这有帮助!

enter image description here

server <- function(input, output) {
  output$plot <- renderPlot({
    input$goPlot # Re-run when button is clicked

    # Create 0-row data frame which will be used to store data
    dat <- data.frame(x = numeric(0), y = numeric(0))

    withProgress(message = 'Making plot', value = 0, {
      # Number of times we'll go through the loop
      n <- 10

      for (i in 1:n) {
        # Each time through the loop, add another row of data. This is
        # a stand-in for a long-running computation.
        dat <- rbind(dat, data.frame(x = rnorm(1), y = rnorm(1)))

        # Increment the progress bar, and update the detail text.
        incProgress(1/n, detail = paste("Doing part", i))

        # Pause for 0.1 seconds to simulate a long computation.
        Sys.sleep(0.5)
      }
    })

    plot(dat$x, dat$y)
  })
}

ui <- shinyUI(basicPage(
  tags$head(tags$style(HTML('.progress-bar {background-color: red;}'))),
  plotOutput('plot', width = "300px", height = "300px"),
  actionButton('goPlot', 'Go plot')
))

shinyApp(ui = ui, server = server)