I use R and Knitr a lot to produce long html reports. The reports contain headers by using the markdown #
syntax. These headers give good orientation for the reader's navigation...
... but the reports sometimes get very long. Scrolling from beginning to the end take very long time. Readers of the reports get annoyed seeing all the report content before reaching the relevant parts.
Is there a way to implement in Knitr a collapsing and expanding header element?
R is the most popular language in the world of Data Science. It is heavily used in analyzing data that is both structured and unstructured. This has made R, the standard language for performing statistical operations. R allows various features that set it apart from other Data Science languages.
R language is used by more than 2 million statisticians and data scientists across the world, and with the wider adoption of R language for business applications, the usage of this statistical software is increasing exponentially.
R is known for being hard to learn. This is in large part because R is so different from many programming languages. The syntax of R, unlike languages like Python, is very difficult to read. Basic operations like selecting, naming, and renaming variables are more confusing in R than they are in other languages.
R is a free software environment for statistical computing and graphics. It compiles and runs on a wide variety of UNIX platforms, Windows and MacOS. To download R, please choose your preferred CRAN mirror.
You can make elements collapse using Javascript. The jQuery
JavaScript framework makes this reasonably easy via the hide
and show
methods.
In the folder that contains your Rmd
template, create a subfolder named script
and save the jQuery file in it. (Doesn't have to be there, but that's a reasonably standard location.)
Add this code near the top of your Rmd
file.
<script type="text/javascript" language="javascript" src="script/jquery-1.10.2.min.js">
</script>
For Markdown, the closing tag needs to be on a separate line.
Alternatively, if your report is mostly going to be read on machines where there is internet access, you can should use a Google-hosted version of jQuery
.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" >
</script>
Then add another script block for your collapsing logic. The exact implementation is up to you; there are lots of examples on the internet.
The key to making your collapsing/expanding logic simple is to make sure that the elements you are manipulating have a consistent class (or a pattern to their ids).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With