Broken RMarkdown html TOC with leaflet::addMeasure and DT::datatable
I recently stumbled upon a strange error that I cannot really find a cause for. Under very specific conditions, the table of content of an RMarkdown html document is unfavorably altered.
When I add an addMeasure() to a leaflet()-map and call a DT::datatable() afterwards, the floating table of contents in an RMarkdown-html-document has two additional entries "Measure distances and areas" while the following subchapter disappears from the TOC.
First I thought it might be a bug in mapview() and asked @tim-salabim how to solve this (see here). However, the problem also occurs when a map is built directly with leaflet().
Here is a reproducible example that produces the unwanted behaviour both under R 3.6.1 on a Windows machine and R 4.0.2 on a Mac. Note, that I had to "escape" the two code chunk closing tripple-GraveAccent lines ("!```") to be able to provide an RMarkdown codeblock here:
---
title: 'ReprEx: addMeasure, DT::datatable'
output:
html_document:
toc: yes
toc_float:
collapsed: no
---
# Chapter 1
```{r}
library(leaflet)
library(DT)
leaflet() %>%
addMeasure()
!```
```{r, eval = TRUE}
datatable(data.frame(a = 1))
!```
## subchapter that disappears in TOC
## visible subchapter
Thanks for your great work for the geospatial community!!