2014-12-02 19 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