如何在命令行中复制针织HTML ?

时间:2021-04-22 07:08:19

I know this question is similar to this one. But I couldn't get a solution there so posting it here again.

我知道这个问题和这个问题很相似。但我在那里找不到解决办法,所以又把它贴在这里了。

I want to get the exact same output as I get by clicking "Knit HTML" but via a command. I tryied using knit2html but it messes with the formatting and does not include the title, kable does not work etc.

我想通过点击“编织的HTML”得到与我得到的完全相同的输出,但是通过一个命令。我使用knit2html进行了测试,但是它会影响格式,不包含标题,“kable”不能工作等等。

Example:

例子:

This is my test.Rmd file,

这是我的测试。限制型心肌病文件,

---
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.

Output:

输出:

Knit HTML

针织的HTML

如何在命令行中复制针织HTML ?

knit2html

knit2html

如何在命令行中复制针织HTML ?

1 个解决方案

#1


14  

The documentation tells us:

文档告诉我们:

If you are not using RStudio then you simply need to call the rmarkdown::render function, for example:

如果不使用RStudio,那么只需调用rmarkdown::呈现函数,例如:

rmarkdown::render("input.Rmd")

Note that in the case using the “Knit” button in RStudio the basic mechanism is the same (RStudio calls the rmarkdown::render function under the hood).

注意,在RStudio中使用“针织”按钮的基本机制是相同的(RStudio调用rmarkdown::render函数在引擎盖下)。

In essence, rmarkdown::render does a lot more setup than knitr::knit2html, although I don’t have an exhaustive list of all differences.

在本质上,rmarkdown::render比knitr:::knit2html做了更多的设置,尽管我没有列出所有差异的完整列表。

The most flexible way of rendering the output is, at any rate, to supply your own stylesheet to format the output according to your wishes.

呈现输出的最灵活的方式,无论如何,是提供您自己的样式表来按照您的意愿格式化输出。

Please note that you need to set up Pandoc manually to work with rmarkdown::render on the command line.

请注意,您需要手动设置Pandoc,以便在命令行上使用rmarkdown::render。


That said, here are two remarks that would improve the knitr::knit2hmtl output, and that are superior to using rmarkdown::render in my opinion:

也就是说,这里有两点可以改进knitr:::knit2hmtl输出,并且优于使用rmarkdown::

  • To include the title, use a Markdown title tag, not a YAML tag:

    要包含标题,请使用Markdown标题标记,而不是YAML标记:

    # My title
    
  • To format tables, don’t use the raw kable function. In fact, this is also true when using rmarkdown::render: the alignment of the table cells is completely off. Rmarkdown apparently uses centering as the default alignment but this option is almost never correct. Instead, you should left-align text and (generally) right-align numbers. As of this writing, Knitr cannot do this automatically (as far as I know) but it’s fairly easy to include a filter to do this for you:

    要格式化表,不要使用原始的可选函数。事实上,当使用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)
    })
    ```
    

#1


14  

The documentation tells us:

文档告诉我们:

If you are not using RStudio then you simply need to call the rmarkdown::render function, for example:

如果不使用RStudio,那么只需调用rmarkdown::呈现函数,例如:

rmarkdown::render("input.Rmd")

Note that in the case using the “Knit” button in RStudio the basic mechanism is the same (RStudio calls the rmarkdown::render function under the hood).

注意,在RStudio中使用“针织”按钮的基本机制是相同的(RStudio调用rmarkdown::render函数在引擎盖下)。

In essence, rmarkdown::render does a lot more setup than knitr::knit2html, although I don’t have an exhaustive list of all differences.

在本质上,rmarkdown::render比knitr:::knit2html做了更多的设置,尽管我没有列出所有差异的完整列表。

The most flexible way of rendering the output is, at any rate, to supply your own stylesheet to format the output according to your wishes.

呈现输出的最灵活的方式,无论如何,是提供您自己的样式表来按照您的意愿格式化输出。

Please note that you need to set up Pandoc manually to work with rmarkdown::render on the command line.

请注意,您需要手动设置Pandoc,以便在命令行上使用rmarkdown::render。


That said, here are two remarks that would improve the knitr::knit2hmtl output, and that are superior to using rmarkdown::render in my opinion:

也就是说,这里有两点可以改进knitr:::knit2hmtl输出,并且优于使用rmarkdown::

  • To include the title, use a Markdown title tag, not a YAML tag:

    要包含标题,请使用Markdown标题标记,而不是YAML标记:

    # My title
    
  • To format tables, don’t use the raw kable function. In fact, this is also true when using rmarkdown::render: the alignment of the table cells is completely off. Rmarkdown apparently uses centering as the default alignment but this option is almost never correct. Instead, you should left-align text and (generally) right-align numbers. As of this writing, Knitr cannot do this automatically (as far as I know) but it’s fairly easy to include a filter to do this for you:

    要格式化表,不要使用原始的可选函数。事实上,当使用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)
    })
    ```