htmlrmarkupsections

How to exclude the not evaluated R markup sections from the ToC of the resulting HTML document?


I am writing an R/Shiny code that generates HTML reports for my users' data using an R markup template. I want to give the user the option to omit certain sections from the report. Which is easy to do for the sections themselves, but not obvious for the sections' headlines and their ToC entries. Any advice would be greatly appreciated!

My code passes the list of sections to include in the report via the params variable of the rmarkdown::render function. And the omitted sections of the R markup file are indeed not evaluated. However, the ToC entries and headlines for those (empty) sections still end up in the resulting HTML file. I browsed the manual but could not find any way to use the markup code parameters to exclude desired sections of the markup file.


    ---
    output:
      html_document:
        theme: yeti
        df_print: kable
        toc: true
        toc_float:
          collapsed: true
          smooth_scroll: false
    params:
      ExpData: data.frame()
      set_title: "My Data Report"
      Sections_to_include: list()
    title: "`r params$set_title`"
    ---

    .....................................
    .....................................
    .....................................

    # ANOVA 

    ```{r , eval = ( "ANOVA" %in% params$Sections_to_include)}

    # Here be the code that generates my ANOVA table
    # lme_model is calculated from params$ExpData in the preceding
    # code chunks

    kable(ANOVA(lme_model))
    ```

If the params$Sections_to_include list does not include the element "ANOVA" then the code chunk above will not be evaluated and the ANOVA table won't be printed. However, the section headline ANOVA will still be included in the HTML document preceding an empty section, and the ToC will contain a link to that headline. Which is of course what the

# ANOVA

line instructs R markup to do. I wonder if there is any parameter that would allow me to instruct the R markup renderer to ignore that line.

One solution I can see is to change the workflow to (i) generate a customized R markup document from the basic template according to the options desired (e.g., using UNIX command line tools), and (ii) then render the customized markup file into an HTML doc. But I wonder if R markup / knitr provide a simpler built-in solution?

Thank you all!


Solution

  • You can do it using the "asis" engine. It's likely helpful to write a little function to decide on whether something should be included. Here's your example modified to leave out "ANOVA", but include two other sections.

    ---
    output:
      html_document:
        theme: yeti
        df_print: kable
        toc: true
        toc_float:
          collapsed: true
          smooth_scroll: false
    params:
      ExpData: data.frame()
      set_title: "My Data Report"
      Sections_to_include: ["Other", "More"]
    title: "`r params$set_title`"
    ---
    
    .....................................
    .....................................
    .....................................
    
    ```{r include=FALSE}
    library(knitr)
    include <- function(section) {
      section %in% params$Sections_to_include
    }
    ```
    
    
    ```{asis , echo=include("Other")}
    # Other section
    
    This is a section that I do want to include, this time.
    ```
    
    ```{asis echo=include("ANOVA")}
    # ANOVA 
    ```
    
    ```{r , echo = include("ANOVA"), eval = include("ANOVA")}
    
    # Here be the code that generates my ANOVA table
    # lme_model is calculated from params$ExpData in the preceding
    # code chunks
    
    kable(ANOVA(lme_model))
    ```
    
    ```{asis , echo=include("More")}
    # More
    
    So is this one.
    ```
    

    I'm not sure how to programmatically generate that vector of section names in Sections_to_include from within YAML, but you can always pass parameter values in your call to rmarkdown::render.