Search code examples
rshinyflexdashboard

Flexdashboard - hidden navbar tab using reactive values


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.

enter image description here

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())`


Solution

  • 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())`