Flexdashboard - hidden navbar tab using reactive values

﹥>﹥吖頭↗ 提交于 2019-12-13 16:16:34

问题


I can hide a navbar page using the static value of show_hide but I cannot figure out how to do it with the reactive value r_show_hide(). I have also tried using isolate(ifelse...) and then r_show_hide (no parentheses) as well as reactiveVal() to no avail.

There is also an extraneous "> that shows up. Any help would be appreciated.

Update: I created an issue https://github.com/rstudio/flexdashboard/issues/229


    ---
    title: "-"
    output: flexdashboard::flex_dashboard
    runtime: shiny
    ---

    ```{r setup, include=FALSE}
    library(flexdashboard)
    library(shiny)

    show_hide <- "show" # "hidden"

    r_show_hide <- reactive(ifelse(session$clientData$url_hostname == "127.0.0.1", "hidden", "show"))
    ```

    Does work {.`r show_hide`}
    =============================

    ### Should be `r show_hide`


    Doesn't work {.`r reactive(r_show_hide())`}
    ===============================

    ### Should be `r renderText(r_show_hide())`


回答1:


Ok, this took me a while figuring out.

The fundamental problem is that r chunks in curly brackets of the flexdashboard navbar evaluate in a non-reactive context, compared to the r chucks that build the content of each page, which are evaluated in a reactive context. For this reason you cannot use a reactive such as r_show_hide() to trigger the argument hidden/show of the navbar page, but you can use r_show_hide() in a renderText() function within the page.

So the actual question is, how to access a reactive value from a non-reactive context. The answer is isolate() and is explained here.

Below I provide an example using your code.

---
title: "-"
output: flexdashboard::flex_dashboard
runtime: shiny
---

```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)

show_hide <- "show" # "hidden"

r_show_hide <- reactive(ifelse(session$clientData$url_hostname == "127.0.0.1", "hidden", "show"))
```

Does work {.`r show_hide`}
=============================

### Should be `r show_hide`


Doesn't work {.`r isolate(r_show_hide())`}
===============================

### Should be `r renderText(r_show_hide())`


来源:https://stackoverflow.com/questions/57280199/flexdashboard-hidden-navbar-tab-using-reactive-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!