我是闪亮的新手。当我制作我的项目时,我需要在服务器端隐藏dashboardHeader。
在 Shinydashboard 网站上,我找到了代码dashboardHeader(disable = TRUE)。我试过这个,但它不起作用。
但是,我尝试使用shinyjs 来解决问题。
<code>
library(shiny)
library(shinydashboard)
library(shinyjs)
ui <- dashboardPage(
dashboardHeader(
extendShinyjs(text = 'shinyjs.hidehead = function(params) {
$("header").addClass("sidebar-collapse") }'),
),
dashboardSidebar(),
dashboardBody(
actionButton("button","hide_header",width = 4 )
)
)
server <- function(input, output) {
observeEvent(input$button, {
js$hidehead()
})}
shinyApp(ui, server)</code>
Run Code Online (Sandbox Code Playgroud)
我想你已经知道了,它仍然没有奏效。
对我的情况有什么想法吗?
Shinyjs 是一个很棒的库。您的代码的问题在于您需要首先shinyjs使用shinyjs::useShinyjs()它进行初始化并将其放入dashboarBody函数中。此外,要隐藏/显示标题,您不需要添加"sidebar-collapse"实际上用于侧边栏的类。您只需要添加style="display:none"隐藏标题,并删除它以显示标题。下面是修改后的代码以隐藏/显示标题。使用的 JS 代码非常简单,它直接从js$hidehead()函数中接收要添加的参数。
library(shiny)
library(shinydashboard)
library(shinyjs)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
# initialize shinyjs
shinyjs::useShinyjs(),
# add custom JS code
extendShinyjs(text = "shinyjs.hidehead = function(parm){
$('header').css('display', parm);
}"),
actionButton("button","hide header"),
actionButton("button2","show header")
)
)
server <- function(input, output) {
observeEvent(input$button, {
js$hidehead('none')
})
observeEvent(input$button2, {
js$hidehead('')
})
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2518 次 |
| 最近记录: |