R Markdown HTML output - Hide a section of the document of an HTML RMarkdown output
Asked Answered
L

3

5

I'm trying to knit an HTML document from RMarkdown, and hide the section from both the Table of Contents (ToC) and body of the output (HTML file), while while still knitting (executing) all of the code and contents.

I'm not looking to hide just the code chunk - this can be done through echo = FALSE.

I tried using the {.hidden} option meant for flexdashboards but it only partially solves the problem, i.e. the whole section (called # The section I want to hide in the example below) disappears from the HMTL output itself, but not the table of contents.

Code example:

---
output: 
  html_document: 
    df_print: kable
    fig_height: 2
    fig_width: 2
    number_sections: yes
    toc: yes
    toc_float: yes
editor_options: 
  chunk_output_type: console
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE,
                      warning = FALSE, 
                      message = FALSE,
                      cache = FALSE,
                      dpi = 75,
                      comment = '')
```


# R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
summary(cars)
```

# Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}
plot(pressure)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

# The section I want to hide {.hidden}

Super-secret text (section content I don't want to show).

```{r}
sumCars <- summary(cars)
```


# This the section I want to show again

```{r}
sumCars[1,]
```


Output:

HTML output


How can I hide the document section while still knitting?

Loggins answered 20/12, 2019 at 17:36 Comment(1)
It's not entirely clear to me: is it text in between code chunks you want to hide, or an actual code chunk? Or both? The code chunk you can simply hide by using include = FALSE as chunk option. It still gets evaluated.Capitoline
L
3

SOLVED by using .unlisted .unnumbered

# The section I want to hide {.hidden .unlisted .unnumbered}

Loggins answered 6/3, 2024 at 15:0 Comment(0)
C
2

The unlisted class ensures that a heading is not included in the table of contents, and hidden will hide a section in HTML output. Therefore, the following should work:

# The section I want to hide {.hidden .unlisted}
Cranford answered 17/11, 2022 at 18:40 Comment(1)
Doesn't work currently (knitr 1.45) - any other suggestions? :)Loggins
C
1

You can use arbitrary css in markdown when exporting to html. Here is an example:


---
title: "Untitled"
output: html_document
---

# One

Hello!

# Two

<style>
 #two {
   display: none;
 }
</style>

```{r}
x <- 1:10
```

# Three

```{r}
plot(x)
```

Charla answered 20/12, 2019 at 18:6 Comment(4)
An associated question: How to hide the title section?Milliner
@LazarusThurston You can hide the title the same way -- use CSS to set display: none;Charla
Your solution doesn't hide the section from the ToC.Loggins
@Loggins you can use my approach to hide the toc entry too, you just have to find the css identifier and set display to noneCharla

© 2022 - 2025 — McMap. All rights reserved.