11

单击选项卡时尝试更新flexdashboard中的侧边栏。无法让它工作。

---
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() 一起工作正常。任何建议表示赞赏。

4

1 回答 1

3

几点评论:

  • 您只需要调用useShinyjs()一次,无需在每个代码块中调用它

  • onclick()获取元素的 ID。不是选择器 - ID。这意味着你打电话onclick("element")而不是onclick("#element")

  • 如果您查看仪表板生成的 HTML,您会发现没有 ID 为“section-tab-2”的元素,因此您尝试执行的操作将不起作用

  • 在常规闪亮的应用程序中,当我使用 a 时tabsetPanel(),我做你想做的事情的方式是使用所选选项卡的值。Whenever a tab is selected, there's an input value that gives you the value of what is selected. 我没有广泛使用 flexdashboard,所以我不确定是否有类似的方法可以在 flexdashboard 中获取所选选项卡的值。

于 2017-01-06T22:20:39.990 回答