day*_*yne 16 r r-markdown
我非常欣赏RMarkdown中的"code_folding"功能.但是,我真正需要的是让代码始终显示并切换输出上的显示.
---
title: "test file"
author: "dayne"
date: "June 10, 2016"
output:
html_document:
code_folding: hide
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Here is a basic example.
```{r}
3 + 4
```
Run Code Online (Sandbox Code Playgroud)
有没有办法切换输出而不是代码?我想到的最好(但不是理想的)解决方案是添加collapse=TRUE到块中,但代码和输出仍然同时显示.
链接到已编译的文档:http://rpubs.com/daynefiler/188408
Mar*_*zer 36
TOC:
完全控制应折叠哪些块
折叠包含多行代码/输出的所有块
我也想拥有相同的功能并执行以下操作:
我创建了一个看起来如下的JavaScript:
$(document).ready(function() {
$chunks = $('.fold');
$chunks.each(function () {
// add button to source code chunks
if ( $(this).hasClass('s') ) {
$('pre.r', this).prepend("<div class=\"showopt\">Show Source</div><br style=\"line-height:22px;\"/>");
$('pre.r', this).children('code').attr('class', 'folded');
}
// add button to output chunks
if ( $(this).hasClass('o') ) {
$('pre:not(.r)', this).has('code').prepend("<div class=\"showopt\">Show Output</div><br style=\"line-height:22px;\"/>");
$('pre:not(.r)', this).children('code:not(r)').addClass('folded');
// add button to plots
$(this).find('img').wrap('<pre class=\"plot\"></pre>');
$('pre.plot', this).prepend("<div class=\"showopt\">Show Plot</div><br style=\"line-height:22px;\"/>");
$('pre.plot', this).children('img').addClass('folded');
}
});
// hide all chunks when document is loaded
$('.folded').css('display', 'none')
// function to toggle the visibility
$('.showopt').click(function() {
var label = $(this).html();
if (label.indexOf("Show") >= 0) {
$(this).html(label.replace("Show", "Hide"));
} else {
$(this).html(label.replace("Hide", "Show"));
}
$(this).siblings('code, img').slideToggle('fast', 'swing');
});
});
Run Code Online (Sandbox Code Playgroud)
因为我不是JS破解它可能不完美,但它做了它应该做的事情.将其包含在您的Rmd文件中:
<script src="js/hideOutput.js"></script>
Run Code Online (Sandbox Code Playgroud)
我还写了一些CSS定义来设置按钮的样式:
.showopt {
background-color: #004c93;
color: #FFFFFF;
width: 100px;
height: 20px;
text-align: center;
vertical-align: middle !important;
float: right;
font-family: sans-serif;
border-radius: 8px;
}
.showopt:hover {
background-color: #dfe4f2;
color: #004c93;
}
pre.plot {
background-color: white !important;
}
Run Code Online (Sandbox Code Playgroud)
在包含JS文件和样式表之后,您可以通过使用以下类之一包装div容器来隐藏块:
仅隐藏输出
<div class="fold o">
```{r}
...
```
</div>
Run Code Online (Sandbox Code Playgroud)
隐藏源代码
<div class="fold s">
```{r}
...
```
</div>
Run Code Online (Sandbox Code Playgroud)
隐藏两者
<div class="fold s o">
```{r}
...
```
</div>
Run Code Online (Sandbox Code Playgroud)
该脚本检测每个块的类型(例如源代码,文本输出或绘图输出)并相应地标记按钮.
结果如下:
以下是脚本的一个版本,它将折叠功能添加到长于一行的所有块中:
$(document).ready(function() {
$plots = $('img.plot');
$chunks = $('pre').has('code');
$chunks = $chunks.filter(function(idx) {
return $(this).children('code').outerHeight(false) > parseInt($(this).css('line-height'));
});
$chunks.each(function () {
if($(this).hasClass('r')) {
$(this).append("<div class=\"showopt\">Show Source</div><br style=\"line-height:22px;\"/>");
} else {
$(this).append("<div class=\"showopt\">Show Output</div><br style=\"line-height:22px;\"/>");
}
});
$plots.each(function () {
$(this).wrap('<pre class=\"plot\"></pre>');
$(this).parent('pre.plot').prepend("<div class=\"showopt\">Show Plot</div><br style=\"line-height:22px;\"/>");
});
// hide all chunks when document is loaded
$chunks.children('code').toggle();
$('pre.plot').children('img').toggle();
// function to toggle the visibility
$('.showopt').click(function() {
var label = $(this).html();
if (label.indexOf("Show") >= 0) {
$(this).html(label.replace("Show", "Hide"));
} else {
$(this).html(label.replace("Hide", "Show"));
}
$(this).siblings('code, img').slideToggle('fast', 'swing');
});
});
Run Code Online (Sandbox Code Playgroud)
只需包含它,<script src="js/hideAll.js"></script>您就不需要在代码块周围包装div容器.您必须在Rmd文档中添加的一件事是以下全局块选项:
```{r, echo = F}
knitr::opts_chunk$set(out.extra = 'class="plot"')
```
Run Code Online (Sandbox Code Playgroud)
需要识别图形输出.
Cle*_*ide 17
这个低保真解决方案怎么样?
<details><summary>Click here</summary>
Some text
```{r code}
# even code
print("Hello world!")
```
</details>
Run Code Online (Sandbox Code Playgroud)
不是我的,但我喜欢。
一种快速切换部分(不一定是代码)的方法:
<div class="toggle"><button>TOGGLE_TEXT</button>将要使用和切换的部分括<\div>在 .Rmd 文件中
1. How many users are in the second, but not the first, user table?
<div class="toggle"><button>Solution</button>
```{r}
setdiff(user2, user) %>% nrow()
```
</div>
Run Code Online (Sandbox Code Playgroud)
将其放在 .Rmd 文件的底部(或者最好放在链接到所有页面的 .js 文件中)。
<script>
$(".toggle").click(function() {
$(this).toggleClass("open");
});
</script>
Run Code Online (Sandbox Code Playgroud)
将其放入您的 .css 文件中(您必须调整按钮的高度)。
.toggle {
height: 1.55em;
overflow-y: hidden;
}
.toggle.open {
height: auto;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10842 次 |
| 最近记录: |