2016-06-28 17 views
1

我創建自R降價的HTML頁面下面.rmd代碼之間休息:停機v降價從添加代碼和輸出

--- 
title: 'TITLE' 
author: "NAME" 
date: "DATE" 
output: 
    html_document: 
    keep_md: yes 
--- 

```{r setup, include=FALSE} 
knitr::opts_chunk$set(echo = TRUE) 
``` 

```{r} 
(n1 <- nrow(perf)) # Save the number of rows in 'perf' 
(m1 <- ncol(perf)) # Save the number of columns in 'perf' 
``` 

當我編我的網頁,它返回我的代碼和輸出單獨的塊,但我希望它們都被包含在嵌入到我的.md中的單個塊中。結果是這樣的:

Screen shot of .md output

但是,我想各部分之間的空間消失,並且代碼和輸出都被包含在一個單一的部分。這可能嗎?我打算把這個放在GitHub上,所以如果有任何使GitHub特定降價的工作會有幫助(我知道這不是我如何配置.md,但這是我的下一步) 。

回答

0

我有兩個可能的解決方案爲您提供:

1.結合每個源代碼塊用下面的輸出塊

你可以嘗試添加下面的JavaScript塊在RMarkdown腳本:

<script> 
$chunks = $('pre.r'); 
$chunks.each(function() { 
    if ($(this).next().is('pre:not(r)')) { 
     var newc = '<br>' + $(this).next('pre').children('code').html(); 
     var oldc = $(this).children('code').html(); 
     $(this).children('code').html(oldc.concat(newc)); 
     $(this).next('pre').remove(); 
    } 
}) 
</script> 

enter image description here

2.屬於一個R-塊到一個盒子把一切

或者如果你想屬於在一個盒子一樣RMarkdown塊都HTML塊嘗試

<script> 
$chunks = $('pre.r'); 
$chunks.each(function() { 
    while($(this).next().is('pre')) { 
     var newc = '<br>' + $(this).next('pre').children('code').html(); 
     var oldc = $(this).children('code').html(); 
     $(this).children('code').html(oldc.concat(newc)); 
     $(this).next('pre').remove(); 
    } 
}) 
</script> 

enter image description here

1

很簡單,在.rmd文件中的代碼塊的規範中包含規範collapse = TRUE

```{r, collapse = TRUE} 
(n1 <- nrow(perf)) # Save the number of rows in 'perf' 
(m1 <- ncol(perf)) # Save the number of columns in 'perf' 
```