2015-04-18 21 views
6

我生成HTML和PDF格式的筆記本電腦。有時我使用嵌套列表並將它們與代碼塊混合使用。以下是使用和knitr塊選項的示例。但是,列表項(a)後的所有代碼也將被標記爲代碼(例如(b))。但是我想實現的是將(b)標記爲普通文本,並使用與(a)相同的縮進。是否有可能做到這一點?Rmarkdown的render()+ knitr的旋():如何使用的功能<code>render()</code>和<code>knitr</code>的功能<code>spin()</code><code>R</code> -scripts混合代碼塊和嵌套項目

回答

3

有一個內部塊選項indent可以將縮進添加到塊輸出。就你而言,你可以指定四個空格,例如

#+ echo = TRUE, eval = TRUE, indent = ' ' 
3

你必須使用什麼文檔中稱爲The four-space rulehttp://rmarkdown.rstudio.com/authoring_pandoc_markdown.html#the-four-space-rule

所以,下面的代碼工作

(1) This is normal text. 

    Continued. 

    (a) This is normal text but indented. 

     ```{r, echo = TRUE, eval = TRUE} 
     summary(cars) 
     ``` 

    (a) This is normal text with the same indentation as (a). 

注:

  • 2空間盈的(1)
  • 盈每4個空格的(a)
  • 8空間盈代碼塊

在所得的: enter image description here

我跑它使用rmarkdown::render("test.Rmd"),這是我的會話信息

R version 3.1.1 (2014-07-10) 
Platform: x86_64-w64-mingw32/x64 (64-bit) 

locale: 
[1] LC_COLLATE=German_Germany.1252 LC_CTYPE=German_Germany.1252 LC_MONETARY=German_Germany.1252 
[4] LC_NUMERIC=C     LC_TIME=German_Germany.1252  

attached base packages: 
[1] stats  graphics grDevices utils  datasets methods base  

loaded via a namespace (and not attached): 
[1] digest_0.6.8 evaluate_0.5.5 formatR_1.0  htmltools_0.2.6 knitr_1.9  rmarkdown_0.5.1 
[7] stringr_0.6.2 tools_3.1.1  XML_3.98-1.1 yaml_2.1.13  
+0

謝謝。當使用render(「test.R」,output_format =「pdf_document」)'直接編譯'R'腳本時,我無法使它工作。 –

+0

你的文件應該被命名爲'* .Rmd',而不是'* .R',但是除此之外:奇怪。對我來說,它是完美的: 產生http://www.filedropper.com/test_50 – Rentrop

+0

'render()'也可以用於純粹的普通'R'腳本,而不僅僅是'* .Rmd'文件到筆記本。我的意思是像[this]這樣的腳本(https://drive.google.com/open?id=0B_UAut69TSAic0ZvZC1qUlZDY28&authuser=0)。它基於'spin()',它將編譯純的'R'腳本轉換爲'* .Rmd'文件,這就是我正在談論的。可以在這裏找到解釋(https://github.com/yihui/knitr/blob/master/inst/examples/knitr-spin.R)。 @易輝,你能幫忙嗎? –