改变颜色actionButton Shiny R

时间:2017-04-26 17:41:50

标签: r button colors shiny click

我试图想办法让actionButton改变颜色(也可能是其他样式元素。) 目标是非常通用的,我将在复杂的闪亮仪表板应用程序中使用它来进行许多按钮响应。简单地说:如果单击一个按钮,或者一个特定元素(即inputlider)改变了链接到一个按钮,该按钮应该改变颜色。

我通常在Shiny中编码我的按钮,就像这样:

  actionButton(inputId= "RunFullModel", label =icon("tree-deciduous", lib = "glyphicon"), 
                       style = "color: white; 
                       background-color: #35e51d; 
                       position: relative; 
                       left: 3%;
                       height: 35px;
                       width: 35px;
                       text-align:center;
                       text-indent: -2px;
                       border-radius: 6px;
                       border-width: 2px")),

我一直在浏览我的屁股,但对于有效的方法却没有运气。 我一直在寻找@Dean Attali的shinyjs来做这件事,但我只能让它来改变印刷文本的背景,如下例所示。使用ToggleClass或AddClass的部分似乎不适用于actionButton(" x"," y")。 我也尝试过使用闪亮包本身的updateActionButton,但这似乎不允许使用style =" ..."要包含在命令中。

library(shiny)
library(shinyjs)
library(shinyBs)
if (interactive()) {
  shinyApp(
    ui = fluidPage(
      useShinyjs(),
      inlineCSS(list(.red = "background: red")),
      inlineCSS(list(.blue = "style = 'background-color: blue'")),
      actionButton("checkbox", "Make it red"),
      bsButton("checkbox2", "Make me blue"),
      p(id = "element", "Watch what happens to me")
    ),
    server = function(input, output) {
      observe({
        toggleClass(id = "element", class = "red",
                    condition = input$checkbox)
      })
      observe({
        toggleCssClass(id = "checkbox2", class = ".blue",
                    condition = input$checkbox)
      })
    }
  )
}

在这里可以找到shinyjs的演示模型:http://deanattali.com/shinyjs/overview#demo看起来好像toggleClass在那里用于id为" btn"的按钮。但是没有直接找到的代码示例,而来自shinyjs页面的另一部分的间接示例似乎不起作用。

选项3我尝试使用style =" background-color:....读取R变量" part访问一个包含颜色名称的R变量,即mycolor< - " red",但这也不起作用,而且我对javascript的了解太有限,无法弄清楚如何制作它工作。

所以,....如果有人知道如何使用shinyjs进行颜色切换,或者使用另一种方法与上面编码的按钮一起工作,我会非常感激,并且给予我永恒的感激之情。 (此问题已经持续数周了)

马克

P.S。我也研究了bsButtons,但它们对于我想要的默认类选项来说太有限了。

1 个答案:

答案 0 :(得分:5)

这应该是“纯”闪亮的: 使用renderUI()创建按钮并插入条件以检查按钮是否已被单击。

我决定在您写的计划触发颜色变化的情况下存储反应变量中的信息(点击按钮是否被点击)“如果点击按钮,特定元素(即inputslider)链接到按钮的更改“。因此,您也可以从其他输入中更改global$clicked

library(shiny)
shinyApp(
  ui = fluidPage(
    uiOutput("button")
  ),
  server = function(input, output) {
    global <- reactiveValues(clicked = FALSE)

    observe({
      if(length(input$RunFullModel)){
        if(input$RunFullModel) global$clicked <- TRUE
      } 
    })

    output$button <-  renderUI({
      if(!is.null(input$RunFullModel) & global$clicked){
        actionButton(inputId= "RunFullModel", label =icon("tree-deciduous", lib = "glyphicon"), 
                     style = "color: white; 
                     background-color: #35e51d; 
                     position: relative; 
                     left: 3%;
                     height: 35px;
                     width: 35px;
                     text-align:center;
                     text-indent: -2px;
                     border-radius: 6px;
                     border-width: 2px")        
      }else{
        actionButton(inputId= "RunFullModel", label =icon("tree-deciduous", lib = "glyphicon"))
      }

    })
  }
)