R& Knitr html输出:创建折叠和展开标题 [英] R & Knitr html output: Create collapsing and expanding header
问题描述
使用案例
我使用R和中使用的机制(参见上图) )。
您可以使用Javascript折叠元素。 jQuery
JavaScript框架通过 hide
和 show $ c $ c>方法。
在包含 Rmd
模板的文件夹中,创建一个名为 script
并保存 jQuery文件。
在 Rmd $顶部附近添加此代码c $ c>文件。
< script type =text / javascriptlanguage =javascriptsrc = jquery-1.10.2.min.js>
< / script>
对于Markdown,结束标记需要在另一行。
或者,如果您的报告大多是读的可以上网的机器,您可以 应使用Google托管的 jQuery
版本。
< script src =// ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js>
< / script>
然后为您的折叠逻辑添加另一个脚本块。确切的实现取决于你;有是 lot of 互联网上的示例。
使你的折叠/展开逻辑简单的关键是确保你操作的元素有一个一致的类(或者它们的ids的模式)。
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?
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).
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).
这篇关于R& Knitr html输出:创建折叠和展开标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!