【问题标题】:How to set shiny widget css on server side?如何在服务器端设置闪亮的小部件 css?
【发布时间】:2020-10-24 16:57:24
【问题描述】:

我的目标是通过每次点击来更改 actionButton css,有没有一种方法可以在不使用 shinyBS 和 shinyjs 的情况下实现?

请参阅下面的最小可重现示例。

library(shiny)
shinyApp(
  ui = fluidPage(
    tags$style("#action{color:black;}"),
    actionButton("action", label = "Action")
  ),
  server = function(input, output) {
    observeEvent(input$action, {
      message(input$action %% 2)
      if (input$action %% 2) {
        tags$style("#action{color:red;}")
      } else {
        tags$style("#action{color:black;}")
      }
    })
  }
)

点击 1 个红色

点击2黑色

点击3红色

点击4黑色

点击5红色

...

【问题讨论】:

    标签: javascript css r shiny shinydashboard


    【解决方案1】:

    一种 JavaScript 方式:

    js <- "
    function changeColor(button){
      var currentColor = button.style.color;
      var newColor = currentColor === 'red' ? 'black' : 'red';
      button.style.color = newColor;
    }
    "
    
    ui <- basicPage(
      tags$head(tags$script(HTML(js))),
      actionButton(
        "btn", "Click me", style = "color: red;", 
        onclick = "changeColor(this)"
      )
    )
    
    server <- function(input, output, session){}
    
    shinyApp(ui, server)
    

    【讨论】:

    • 有没有办法可以使用十六进制代码代替颜色?
    猜你喜欢
    • 2021-03-10
    • 2016-08-06
    • 1970-01-01
    • 2014-07-26
    • 2016-12-28
    • 1970-01-01
    • 2014-05-07
    • 2016-12-09
    • 2015-10-22
    相关资源
    最近更新 更多