Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show or hide R Markdown text depending on inputs of Shiny R

I have developed a Shiny App with some sections: A, B, C and D. In each one the user can make some plots. There is an extra section of the Shiny App that allows the user, throughout a downloadHandler function, to download all the plots done in each section in format html. So the user clicks the button "download" and an html is generated with the plots the user has done.

This is done with an other file, an R markdown file which receives the inputs of the shiny app. In the R markdown each plot is followed with some text outside the chunk.

So the R markdown has a structure like that:

Section A

(plot of the section A, comes from a chunk)
(text of the section A, is outside the chunk)

Section B

(plot of the section B, comes from a chunk)
(text of the section B, is outside the chunk)

Section C

(plot of the section C, comes from a chunk)
(text of the section C, is outside the chunk)

Section D

(plot of the section D, comes from a chunk)
(text of the section D, is outside the chunk)

But what if the user is not interested in section B? What if in the shiny app the user decided not to choose any input to make the plot? In this case, when clicking the download button in the shiny app, it will produce an error and no HTML output will be produced.

So here is the question: Is there a way to allow the user to select (using a checkboxInput for example) which sections want to be in the HTML output? So if the user checks A and C the HTML produced will be:

Section A

(plot of the section A, comes from a chunk)
(text of the section A, is outside the chunk)

Section C

(plot of the section C, comes from a chunk)
(text of the section C, is outside the chunk)

Of course, I guess there will be some code needed inside the R markdown file, some kind of ifelse statement to show or hide sections. I know how to do it for chunk code, it's quite easy following R language, but what about text? What about the title "Section X" and the text of the section? How to dynamically include or exclude in function of a checkboxInput?

like image 873
Miquel Avatar asked Jan 18 '26 12:01

Miquel


1 Answers

One approach to achieve your desired result would be to add the text via code chunks too use e.g. cat and chunk option results='axis'. In that case you could use a parametrized report and depending on the parameters dynamically include the text as desired:


    ---
    title: "Untitled"
    output: html_document
    params:
      section: !r c("SectionA")
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = FALSE)
    ```
    
    ```{r include="SectionA" %in% params$section, results='asis'}
    cat("## Section A",
                 "Lorem Ipsum",
                 sep = "\n")
    ```
    
    ```{r include="SectionB" %in% params$section, results='asis'}
    cat("## Section B",
                 "Lorem Ipsum",
                 sep = "\n")
    ```
    
    ```{r include="SectionC" %in% params$section, results='asis'}
    cat("## Section C",
                 "Lorem Ipsum",
                 sep = "\n")
    ```

From within your shiny app you then render the Rmd file like so:

rmarkdown::render("test.Rmd", params = list(section = c("SectionA", "SectionC"))

enter image description here

like image 189
stefan Avatar answered Jan 20 '26 02:01

stefan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!