Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R & Knitr html output: Create collapsing and expanding header

Tags:

html

css

r

knitr

Use case

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...

Problem

... 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.

Question

Is there a way to implement in Knitr a collapsing and expanding header element? Example

Requirements

  • By default the header shall be collapsed. Only by clicking the contents below the header shall expand. This would tremendously help to keep the reports small in appearance and facilitate easy and fast navigation.
  • In order to give the reader feedback of the state the header it shall represent it's state. I recommend something along the mechanism used in Wikipedia (see image above).
like image 919
user2030503 Avatar asked Dec 20 '13 08:12

user2030503


People also ask

Why R is so popular?

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.

Is R used anymore?

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.

Is R difficult to learn?

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.

Is R software free to download?

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.


1 Answers

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).

like image 118
Richie Cotton Avatar answered Sep 28 '22 17:09

Richie Cotton