单击选项卡时尝试更新 flexdashboard 中的 Sidebar。无法正常工作。
---
title: "Test Sidebar"
output:
flexdashboard::flex_dashboard:
orientation: rows
runtime: shiny
---
```{r setup}
library(flexdashboard)
library(shiny)
library(shinyjs)
useShinyjs(rmd = TRUE)
```
Sidebar {.sidebar data-width=250}
=======================================================================
```{r}
div(id = "one", selectInput("input1",label= "Show Always",choices=c("a","b","c")))
div(id = "two",selectInput("input2",label = "Show only on Tab 1", choices=c("d","e","f")))
```
<!-- Update the sidebar based on the tab chosen. Generated HTML code shown for tabs-->
Tab 1 <!-- <a href="#section-tab-1" aria-expanded="true" data-toggle="tab"> -->
=======================================================================
```{r}
useShinyjs(rmd = TRUE)
shinyjs::onclick("#section-tab-2",shinyjs::hide(id = "two"))
shinyjs::onclick("#section-tab-1",shinyjs::show(id = "two"))
```
Tab 2 <!-- <a href="#section-tab-2" aria-expanded="true" data-toggle="tab"> -->
=======================================================================
```{r}
useShinyjs(rmd = TRUE)
actionButton("hideSlider", "Hide Input Two")
observeEvent(input$hideSlider, {
shinyjs::toggle(id = "two", anim = TRUE)
})
```
可以与 actionButton() 和 observerEvent() 配合使用。任何建议表示赞赏。
最佳答案
一些评论:
你只需要调用一次
useShinyjs()
,不需要在每个代码块中调用它onclick()
获取元素的 ID。不是选择器 - ID。这意味着您调用onclick("element")
而不是onclick("#element")
如果您查看仪表板生成的 HTML,您会发现不存在 ID 为“section-tab-2”的元素,因此您尝试执行的操作无效
在常规的 Shiny 应用程序中,当我使用
tabsetPanel()
时,我执行您想要执行的操作的方式是使用所选选项卡的值。无论何时选择一个选项卡,都会有一个输入值为您提供所选内容的值。我没有广泛使用 flexdashboard,所以我不确定是否有类似的方法来获取 flexdashboard 中所选选项卡的值。
关于javascript - 使用 shinyjs 和 flexdashbord 动态显示/隐藏输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41467747/