Search code examples
rr-markdownflexdashboard

How can I create a conditional flexdashboard layout


The aim is to create a flexdashboard layout in an markdown file. File is in rows orientation and then the layout contains multiple rows one under another. The goal is to have one of the layout section and accompanying chunk not executed/displayed based on a predefined boolean condition. I was able to incorporate a boolean to not include the chunk code in output but can't find any results on a conditional layout. Things to note the final result is a standalone file so shiny solutions are not possible. AFAIK Current Result and needed result

What I've come up with so far just keeps the layout there with the title instead of removing everything. the variable series35 is what is being used as a boolean to make the chunk not produce results. How can the

   `row`
   `--------------------------------------`

lines also be conditionalised (if that's a word) aka not create a new layout section when series35 is FALSE

    row
    -------------------------------------

    ###`r Title 1`

    ```{r, echo=FALSE, results='asis'}
       chunk code
    ```

    row
    -------------------------------------

    ###`r Title 2`

    ```{r, echo=FALSE, results='asis', eval = series35}
        chunk code (suppressed when series35 is FALSE)
    ```

    row
    -------------------------------------

    ###`r Title 3`

    ```{r, echo=FALSE, results='asis'}
        chunk code
    ```

    row
    -------------------------------------

    ###`r Title 4`

    ```{r, echo=FALSE, results='asis'}
        chunk code        
    ```

    row {data-height=50}
    -------------------------------------

Solution

  • I ended up after much more searching finding a knitr question that gave me a solution

    ```{r, eval = series35}
    asis_output("row")
    asis_output("-------------------------------------")
    asis_output("###`Title 2`")
    ```
    ```{r, echo=FALSE, results='asis', eval = series35}
        chunk code (suppressed when series35 is FALSE)
    ```
    

    It worked nicely as a quick addition. I didn't try joshpk's method as yet but it does seem like a potentially clean option.