2014-12-02 37 views
14

我知道這個問題類似於this之一。但我無法在那裏找到解決方案,因此再次在此發佈。如何在命令行中複製針織HTML?

我想通過點擊「編織HTML」,但通過命令獲得與我相同的輸出。我tryied使用knit2html但它與格式食堂,不包括標題,kable不工作等

例子:

這是我test.Rmd文件,

--- 
title: "test" 
output: html_document 
--- 

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>. 

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this: 

```{r} 
library(knitr,quietly=T) 
kable(summary(cars)) 
``` 

You can also embed plots, for example: 

```{r, echo=FALSE} 
plot(cars) 
``` 

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot. 

輸出:

針織HTML

enter image description here

knit2html

enter image description here

+0

FWIW我喜歡命令行輸出。 – 2014-12-02 10:18:53

+0

@KonradRudolph:它不適合我。這只是我提出的示例代碼。我的實際使用案例有一個8列10行的大表格。命令行輸出看起來很醜。 – Avinash 2014-12-02 10:20:08

+1

這主要是因爲兩個選項(!)在默認情況下嚴格格式化表。查看我的答案以獲得改進的選項。 – 2014-12-02 10:35:31

回答

13

documentation告訴我們:

如果你不使用RStudio那麼你只需要調用rmarkdown::render功能,例如:

rmarkdown::render("input.Rmd") 

請注意,在使用RStudio中的「編織」按鈕的基本機制是相同的(RStudio在引擎蓋下調用rmarkdown::render功能)。

從本質上說,rmarkdown::render可以做更多的設置比knitr::knit2html,雖然我沒有的所有差異的詳盡清單。

渲染輸出的最靈活的方式是,無論如何,根據您的意願提供自己的樣式表來格式化輸出。

請注意,您需要在命令行上使用set up Pandoc manuallyrmarkdown::render一起使用。


也就是說,這裏有兩個的話,將提高knitr::knit2hmtl輸出,以及優於在我看來,使用rmarkdown::render

  • 要包括標題,使用減價標題標籤,而不是一個YAML標籤:

    # My title 
    
  • 要格式化表格,不要使用原始kable功能。實際上,在使用rmarkdown::render時也是如此:表格單元的對齊完全關閉。 Rmarkdown顯然使用居中作爲默認對齊方式,但這個選項幾乎從不正確。相反,您應該左對齊文本和(通常)右對齊數字。在撰寫本文時,Knitr不能這樣自動(據我所知),但它很容易包括過濾器爲你這樣做:

    ```{r echo=FALSE} 
    library(pander) 
    
    # Use this option if you don’t want tables to be split 
    panderOptions('table.split.table', Inf) 
    
    # Auto-adjust the table column alignment depending on data type. 
    alignment = function (...) UseMethod('alignment') 
    alignment.default = function (...) 'left' 
    alignment.integer = function (...) 'right' 
    alignment.numeric = function (...) 'right' 
    
    # Enable automatic table reformatting. 
    opts_chunk$set(render = function (object, ...) { 
        if (is.data.frame(object) || 
         is.matrix(object)) { 
         # Replicate pander’s behaviour concerning row names 
         rn = rownames(object) 
         justify = c(if (is.null(rn) || length(rn) == 0 || 
             (rn == 1 : nrow(object))) NULL else 'left', 
            sapply(object, alignment)) 
         pander(object, style = 'rmarkdown', justify = justify) 
        } 
        else if (isS4(object)) 
         show(object) 
        else 
         print(object) 
    }) 
    ``` 
    
+0

現在,我只是使用渲染功能,並得到它的工作。但我明白你的意思是這是糟糕的設計。我使用kable的對齊選項來爲每列提供我的對齊方式。對於你的解決方案,我把它作爲我的第一個代碼塊,我應該設置正確的? – Avinash 2014-12-02 12:19:55

+1

是的,確切地說。當然,使用kable的對齊選項原則上也適用。我使用pander的原因是它通常非常強大。 – 2014-12-02 16:23:54

+2

爲了更好地使用** pander **進行默認列對齊,請參閱panderOptions('table.alignment.default')',例如。 [這裏](http://stackoverflow.com/a/27014481/980833)。 – 2014-12-03 05:09:29