Jep*_* S. 3 r shiny shinydashboard shinyjs
我试图找到一种方法来检查 Shiny Dashboard Box 是折叠还是展开。
通过阅读@daattali 在如何在闪亮的仪表板中手动折叠框的精彩回复,我知道可以使用 Shinyjs 包从服务器端折叠框,如下面的代码所示
library(shiny)
library(shinydashboard)
library(shinyjs)
jscode <- "
shinyjs.collapse = function(boxid) {
$('#' + boxid).closest('.box').find('[data-widget=collapse]').click();
}
"
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
useShinyjs(),
extendShinyjs(text = jscode),
actionButton("bt1", "Collapse box1"),
actionButton("bt2", "Collapse box2"),
br(), br(),
box(id = "box1", collapsible = TRUE, p("Box 1")),
box(id = "box2", collapsible = TRUE, p("Box 2"))
)
)
server <- function(input, output) {
observeEvent(input$bt1, {
js$collapse("box1")
})
observeEvent(input$bt2, {
js$collapse("box2")
})
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)
通过检查 UI HTML,我发现可以通过访问 icon 类来解决我的问题的答案(查看它是 fa fa-plus 还是 fa fa-minus),但我不知道该怎么做。
任何帮助将不胜感激。
干杯
您可以创建一个新输入,在用户折叠框时触发,如下所示:
collapseInput <- function(inputId, boxId) {
tags$script(
sprintf(
"$('#%s').closest('.box').on('hidden.bs.collapse', function () {Shiny.onInputChange('%s', true);})",
boxId, inputId
),
sprintf(
"$('#%s').closest('.box').on('shown.bs.collapse', function () {Shiny.onInputChange('%s', false);})",
boxId, inputId
)
)
}
Run Code Online (Sandbox Code Playgroud)
以你为例:
library(shiny)
library(shinydashboard)
library(shinyjs)
jscode <- "
shinyjs.collapse = function(boxid) {
$('#' + boxid).closest('.box').find('[data-widget=collapse]').click();
}
"
collapseInput <- function(inputId, boxId) {
tags$script(
sprintf(
"$('#%s').closest('.box').on('hidden.bs.collapse', function () {Shiny.onInputChange('%s', true);})",
boxId, inputId
),
sprintf(
"$('#%s').closest('.box').on('shown.bs.collapse', function () {Shiny.onInputChange('%s', false);})",
boxId, inputId
)
)
}
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
useShinyjs(),
extendShinyjs(text = jscode),
actionButton("bt1", "Collapse box1"),
actionButton("bt2", "Collapse box2"),
br(), br(),
box(id = "box1", collapsible = TRUE, p("Box 1")),
box(id = "box2", collapsible = TRUE, p("Box 2")),
collapseInput(inputId = "iscollapsebox1", boxId = "box1"),
verbatimTextOutput(outputId = "res")
)
)
server <- function(input, output) {
observeEvent(input$bt1, {
js$collapse("box1")
})
observeEvent(input$bt2, {
js$collapse("box2")
})
output$res <- renderPrint({
input$iscollapsebox1
})
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)
如果需要,您可以在调用时更改true/ falseby 'collapse'/ 'expanded'in 函数。collapseInputShiny.onInputChange
| 归档时间: |
|
| 查看次数: |
1432 次 |
| 最近记录: |