我正试图根据用户所在的选项卡更改shinydashboard框的颜色。为此,我将从tabsetPanel获取输入值,并尝试使用shinyjs更改box-header类的css。不幸的是,我的试验都没有成功。下面是一个可复制的示例(颜色还不适合选项卡,但我将在第二部分中这样做)
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的第一个和第二个调用之间的所有组合,但都失败了。
发布于 2021-03-16 14:07:29
您只是在useShinyjs()中丢失了ui。尝尝这个
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)发布于 2022-02-02 09:32:00
https://rstudio.github.io/shinydashboard/appearance.html#css
## ui.R ##
dashboardPage(
dashboardHeader(title = "Custom font"),
dashboardSidebar(),
dashboardBody(
tags$head(
tags$link(rel = "stylesheet", type = "text/css", href = "custom.css")
)
)
)https://stackoverflow.com/questions/66656292
复制相似问题