单击R Shiny后更改动作按钮的bg颜色

时间:2017-01-27 15:51:45

标签: javascript css r shiny

点击/按下后,我需要更改R Shiny中动作按钮的bg颜色。我可以在悬停时更改默认颜色和颜色,但在点击后更改颜色时出现问题。 当按钮未单击时 - 蓝色, 在悬停 - 浅蓝色, 点击后 - 绿色。寻找有关此事的工作。

这是我的代码 -

ui <- shinyUI(
dashboardPage (
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
  tags$head(tags$style(HTML("
                            .btn {
                            color:rgb(255,255,255);
                            text-align: left;
                            #border-color:rgb(0,144,197);
                            background-color:rgb(0,144,197);}

                            # #gobutton:active {
                            # background: green;
                            # }

                             .btn:hover{
                                      #border-color:rgb(232,245,251);
                            background-color: rgb(232,245,251);color:   rgb(0,144,197);font-weight: bold;
                            }
                            "))),



          actionButton("gobutton","Go"),
          bsModal("formExample", "form", "gobutton", size = "small", #    Enables Pop up Screen

          # Different Shiny Widgets

          textInput("first.name", "First Name", ""),
          textInput("last.name", "Last Name",""),
          dateInput('date',label = 'Date of Birth: yyyy-mm-dd',value = ""),
          sliderInput("age", "Age in Yrs", 0, 100, 25, ticks = FALSE),
          radioButtons("gender","Gender",choices = list("Male" = "Male",  "Female" = "Female"),selected = "Male")

   )
   )))

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

shinyApp(ui,server)

2 个答案:

答案 0 :(得分:4)

我能够通过在CSS代码中添加一些CSS来实现:

ui <- shinyUI(
  dashboardPage (
    dashboardHeader(),
    dashboardSidebar(),
    dashboardBody(
      tags$head(tags$style(HTML("
                                .btn {
                                color:rgb(255,255,255);
                                text-align: left;
                                #border-color:rgb(0,144,197);
                                background-color:rgb(0,144,197);}

                                # #gobutton:active {
                                # background: green;
                                # }

                                .btn:hover{
                                #border-color:rgb(232,245,251);
                                background-color: rgb(232,245,251);color:   rgb(0,144,197);font-weight: bold;
                                }
                                .btn:focus{
                                background-color:green;
                                }

                                "))),



      actionButton("gobutton","Go"),
      bsModal("formExample", "form", "gobutton", size = "small", #    Enables Pop up Screen

              # Different Shiny Widgets

              textInput("first.name", "First Name", ""),
              textInput("last.name", "Last Name",""),
              dateInput('date',label = 'Date of Birth: yyyy-mm-dd',value = ""),
              sliderInput("age", "Age in Yrs", 0, 100, 25, ticks = FALSE),
              radioButtons("gender","Gender",choices = list("Male" = "Male",  "Female" = "Female"),selected = "Male")

      )
      )))

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

shinyApp(ui,server)

以上似乎工作正常:

enter image description here

我添加的CSS是:

.btn:focus{
  background-color:green;
}

答案 1 :(得分:2)

您始终可以使用shinyBS包。我是否可以建议在悬停时向您的按钮添加工具提示

#rm(list = ls())
library(shiny)
library(shinydashboard)
library(shinyBS)
ui <- dashboardPage(
  dashboardHeader(title = 'My Change Button Color'),
  dashboardSidebar(sidebarMenu()),
  dashboardBody(
    fluidRow(
      bsButton("button1", label = "Click Me", block = F, style="danger"),
      bsTooltip(id = "button1", title = "Button 1 Explanation", placement = "right", trigger = "hover")
    )
  )
)

server <- (function(input, output, session) {  
  observeEvent(input$button1,{                                                                                                                                 
    updateButton(session, "button1",label = "Click Me", block = F, style = "success")                                                                                                                           
  })                                                                                                                                                            
})

shinyApp(ui, server)

enter image description here enter image description here