更改颜色actionButton闪亮R [英] change color actionButton Shiny R

查看:121
本文介绍了更改颜色actionButton闪亮R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种方法来获取一个actionButton来更改颜色(以及其他样式元素)。
目标非常笼统,在整个我的许多按钮响应中都将使用它复杂的ShinyDashboard应用程序。简而言之:如果单击了按钮,或者更改了链接到该按钮的特定元素(即,inputslider),则该按钮应更改颜色。

i'm trying to figure out a way to get an actionButton to change color (and possibly other style elements as well.) The objective is very general and I'll use this for many button responses throughout my complex shinyDashboard App. Simply said: if a button is clicked, or a specific element (i.e. inputslider) changes that is linked to a button, the button should change color.

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

I usually code my buttons in Shiny like this one:

  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 = ...。

I've been browsing my ass off, but no luck to something that works. I have been looking at shinyjs by @Dean Attali to do this, but I can only get it to work to change the background of the printed text as in the example below. The part using ToggleClass or AddClass doesn't work for actionButton("x", "y") it seems. I've also tried to use updateActionButton from the shiny package itself, but this does not seem to allow style = "..." to be included in the command.

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的演示模型位于此处: ht tp://deanattali.com/shinyjs/overview#demo ,它看起来好像toggleClass在那里为ID为 btn的按钮工作,但是没有直接找到代码示例,而间接示例来自Shinyjs页面的另一部分似乎不起作用。

The demo model of shinyjs is found here: http://deanattali.com/shinyjs/overview#demo and it looks as if the toggleClass works there for the button with id "btn" but there is no code example directly to be found, and the indirect example from another section of the shinyjs page doesn't seem to work.

选项3我试图让style = background-color:.... read R variable部分访问包含颜色名称(即mycolor)的R变量<-红色,但这也不起作用,而且我对javascript的了解太有限,无法弄清楚如何使它起作用。

Option 3 I tried to have the style = "background-color: ....read R variable" part access an R variable that contains the color name, i.e. mycolor <- "red", but that doesn't work either, and my knowledge of javascript is too limited to figure out how to make that work.

因此,....如果有人对如何使用Shinyjs或上面编码的按钮有效的颜色切换有一个想法,我会非常感谢,并向您永恒的感谢。 (此问题在此问题上已经停留了数周)

So, .... if anyone has an idea how to make the color switching work with shinyjs, or with another method that works with my button coded above, I would highly appreciate it, and grant you my eternal gratitude. (been stuck on this for weeks now with this issue)

Mark

p.s。我也研究了bsButton,但是对于默认类选项,它们对于我想要的功能来说太有限了。

p.s. i've looked into bsButtons as well but they are too limited for what I want with the default class options.

推荐答案

在纯净的闪亮效果中应该可行:
使用 renderUI()创建按钮并插入条件以检查按钮是否已单击。

That should be doable in "pure" shiny: Use renderUI() to create the button and insert conditions to check if the button was clicked already.

我决定存储信息(是否单击了按钮)如您所写,您打算在reactVariable中触发颜色更改如果单击按钮,链接到按钮的特定元素(即,inputslider)发生更改。因此,您还可以更改其他输入中的 global $ clicked

I decided to store the information (whether a button was clicked) within a reactiveVariable as you wrote that you plan to trigger the color change "if a button is clicked, or a specific element (i.e. inputslider) changes that is linked to a button". So you could also change the global$clicked from other inputs.

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"))
      }

    })
  }
)

这篇关于更改颜色actionButton闪亮R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆