在闪亮的仪表板中为滑块添加不同的静态颜色

时间:2018-05-16 05:42:38

标签: r shiny shinydashboard

我是新手。我想为滑动条提供静态颜色,而不管在闪亮的仪表板中选择的范围。我希望滑块有不同的颜色如下,例如:0到40 - 红色,40到60 - 蓝色,60到100 - 绿色。 请帮我解决这个问题。 我的代码,

library(shiny)
library(shinydashboard)

ui <- dashboardPage(skin = "black",
                    dashboardHeader(title = "test"),

                  dashboardSidebar(
                    sidebarMenu(
                      menuItem("Complete", tabName = "comp"))),

                    dashboardBody(
                      tabItems(
                        tabItem(tabName = "comp",
                          fluidRow(
                              sliderInput("range_var", "", value = c(90,100), min = 0, max = 100, width = '200%'))))))

server <- function(input, output, session) { 
  observe({
    updateSliderInput(session, "range_var", label = "", value = c(90, 100), min = 0, max = 100)
  })
}
shinyApp(ui, server)

由于 巴拉吉

2 个答案:

答案 0 :(得分:3)

哦,然后我误解了你的问题。您也可以使用css-commands和正确的选择器来实现这一目标:

library(shiny)
library(shinydashboard)
library(shinyjs)

ui <- dashboardPage(skin = "black",
                    dashboardHeader(title = "test"),
                    dashboardSidebar(
                      sidebarMenu(
                        menuItem("Complete", tabName = "comp"))),
                    dashboardBody(
                      inlineCSS(".irs-line-left { background-color: red; width: 40%;}
                                 .irs-line-mid { background-color: blue; width: 20%; left: 40%;}
                                 .irs-line-right { background-color: green; width: 40%; left: 60%;}
                                "
                                ),

                      shinyjs::useShinyjs(),
                      tabItems(
                        tabItem(tabName = "comp",
                                fluidRow(
                                  sliderInput("range_var", "", value = c(90,100), min = 0, max = 100, width = '200%'))))))

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

shinyApp(ui, server)

答案 1 :(得分:0)

你有没看过这个包 - shinyFeedback

您可以看到一些示例here

要使用多个反馈,您应该在一个observeEvent中编写所有条件 - 尽管我没有设法使多个反馈有效。

以下是该页面中针对多个反馈的代码示例:

library(shiny)
library(shinyFeedback)

ui <- fluidPage(
  useShinyFeedback(), # include shinyFeedback

  numericInput(
    "multiFeedbacks",
    "1 is scary 2 is dangerous", 
    value = 1
  )
)

server <- function(input, output) {
  observeEvent(input$multiFeedbacks, {
    feedbackWarning(
      inputId = "multiFeedbacks",
      condition = input$multiFeedbacks >= 1,
      text = "Warning 1 is a lonely number"
    )
    feedbackDanger(
      inputId = "multiFeedbacks",
      condition = input$multiFeedbacks >= 2,
      text = "2+ is danger"
    )
  })
}

shinyApp(ui, server)

另一种选择是使用 shinyjs 包,您可以在其中运行java-script并将css-code发送到浏览器。您必须将dashShinyjs()放在dashboardBody中。班级&#34; irs​​-bar&#34;用于所有闪亮的滑块,因此如果您只想在某个滑块上执行此操作,则必须调整css选择器(.irs-bar)。 (见下一个例子)。 这是一个小例子哦你如何实现所期望的行为:

library(shiny)
library(shinydashboard)
library(shinyjs)

ui <- dashboardPage(skin = "black",
                    dashboardHeader(title = "test"),
                    dashboardSidebar(
                      sidebarMenu(
                        menuItem("Complete", tabName = "comp"))),
                    dashboardBody(
                      shinyjs::useShinyjs(),
                      tabItems(
                        tabItem(tabName = "comp",
                                fluidRow(
                                  sliderInput("range_var", "", value = c(90,100), min = 0, max = 100, width = '200%'))))))

server <- function(input, output, session) { 
  observeEvent(input$range_var, {
    if (input$range_var[1] <= 40) {
      runjs(paste0('$(".irs-bar").css("background-color"," red")'))
    }
    if (input$range_var[1] > 40 & input$range_var[1] < 60) {
      runjs(paste0('$(".irs-bar").css("background-color"," blue")'))
    }
    if (input$range_var[1] > 60 & input$range_var[1] < 100) {
      runjs(paste0('$(".irs-bar").css("background-color"," green")'))
    }
  })
}

shinyApp(ui, server)

以下示例显示如何仅设置一个特定sliderInput的样式。 sliderInputs放在带有id的2个div中。在runjs函数中,css选择器仅适用于第一个sliderInput。

的样式
library(shiny)
library(shinydashboard)
library(shinyjs)

ui <- dashboardPage(skin = "black",
                    dashboardHeader(title = "test"),
                    dashboardSidebar(
                      sidebarMenu(
                        menuItem("Complete", tabName = "comp"))),
                    dashboardBody(
                      shinyjs::useShinyjs(),
                      tabItems(
                        tabItem(tabName = "comp",
                                fluidRow(
                                  div(id="range_var_css",
                                    sliderInput("range_var", "", value = c(90,100), min = 0, max = 100, width = '200%')
                                    ),
                                  div(id="range_var_css1",
                                    sliderInput("range_var1", "", value = c(90,100), min = 0, max = 100, width = '200%')
                                    )

                                  ))))
)

server <- function(input, output, session) { 
  observeEvent(input$range_var, {
    if (input$range_var[1] <= 40) {
      runjs(paste0('$("#range_var_css .irs-bar").css("background-color"," red")'))
    }
    if (input$range_var[1] > 40 & input$range_var[1] < 60) {
      runjs(paste0('$("#range_var_css .irs-bar").css("background-color"," blue")'))
    }
    if (input$range_var[1] > 60 & input$range_var[1] < 100) {
      runjs(paste0('$("#range_var_css .irs-bar").css("background-color"," green")'))
    }
  })
}

要将sliderInput完全设置为所需颜色的样式,还必须将滑块的border-bottom和border-top的css更改为:

    if (input$range_var[1] <= 40) {
      runjs(paste0('$("#range_var_css .irs-bar").css({
"background-color": "red", 
"border-top": "1px solid red", 
"border-bottom": "1px solid red"})'))
}