首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用shinyjs改变shinydashboard盒的样式

用shinyjs改变shinydashboard盒的样式
EN

Stack Overflow用户
提问于 2021-03-16 13:35:19
回答 2查看 406关注 0票数 0

我正试图根据用户所在的选项卡更改shinydashboard框的颜色。为此,我将从tabsetPanel获取输入值,并尝试使用shinyjs更改box-header类的css。不幸的是,我的试验都没有成功。下面是一个可复制的示例(颜色还不适合选项卡,但我将在第二部分中这样做)

代码语言:javascript
复制
library(shiny)
ui <- fluidPage(
  shinyWidgets::useShinydashboard(),
  tabsetPanel(
    id = "mytab",
    tabPanel("First",
             shinydashboard::box(status = "primary",
                                 title = "mybox",
                                 solidHeader = TRUE, collapsible = TRUE,
                                 sliderInput("orders", "Orders", min = 1, max = 2000, value = 650)
                                 )),
  tabPanel("Second", p("Second tab"))))

server <- function(input, output) {
  observeEvent(input$mytab,{
    shinyjs::runjs("$('.box-header').css('background', 'red');")
    shinyjs::runjs("$('.box.box-solid.box-primary > .box-header').attr('style', 'background:red !important');")
  })
}
shinyApp(ui = ui, server = server)

我尝试了对runjs的第一个和第二个调用之间的所有组合,但都失败了。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-03-16 14:07:29

您只是在useShinyjs()中丢失了ui。尝尝这个

代码语言:javascript
复制
library(shiny)
ui <- fluidPage(
  useShinyjs(),
  shinyWidgets::useShinydashboard(),
  #tabBox( id = "mytab", width=6, title = "My Test Plot",
  tabsetPanel(id = "mytab",
    tabPanel("First", value="tab1",
             shinydashboard::box(status = "primary",
                                 title = "mybox",
                                 solidHeader = TRUE, collapsible = TRUE,
                                 sliderInput("orders", "Orders", min = 1, max = 2000, value = 650)
             )),
    tabPanel("Second", value="tab2", p("Second tab"),
             shinydashboard::box(status = "warning",
                                 title = "mybox",
                                 solidHeader = TRUE, collapsible = TRUE,
                                 sliderInput("orders2", "Orders", min = 1, max = 2000, value = 950)
             ))
    )
  )

server <- function(input, output) {
  observeEvent(input$mytab,{
    if (input$mytab=="tab1"){
      shinyjs::runjs("$('.box-header').css('background', 'red');")
      shinyjs::runjs("$('.box.box-solid.box-primary > .box-header').attr('style', 'background:red !important');")
    } else if (input$mytab=="tab2"){
      shinyjs::runjs("$('.box-header').css('background', 'blue');")
      shinyjs::runjs("$('.box.box-solid.box-primary > .box-header').attr('style', 'background:blue !important');")
    }
    
  })
}
shinyApp(ui = ui, server = server)
票数 2
EN

Stack Overflow用户

发布于 2022-02-02 09:32:00

https://rstudio.github.io/shinydashboard/appearance.html#css

代码语言:javascript
复制
    ## ui.R ##
dashboardPage(
  dashboardHeader(title = "Custom font"),
  dashboardSidebar(),
  dashboardBody(
    tags$head(
      tags$link(rel = "stylesheet", type = "text/css", href = "custom.css")
    )
  )
)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66656292

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档