r - 如何在命令行中复制 Knit HTML?

标签 r markdown rstudio knitr r-markdown

我知道这个问题类似于 this一。但是我在那里找不到解决方案,所以再次在这里发布。

我想通过单击“Knit HTML”但通过命令获得与我完全相同的输出。我尝试使用 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

最佳答案

documentation告诉我们:

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

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



本质上,rmarkdown::renderknitr::knit2html 做更多的设置,虽然我没有所有差异的详尽 list 。

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

请注意您需要set up Pandoc manuallyrmarkdown::render 一起工作在命令行上。

也就是说 ,这里有两个评论可以改善 knitr::knit2hmtl输出,并且优于使用 rmarkdown::render在我看来:
  • 要包含标题,请使用 Markdown 标题标签,而不是 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)
    })
    ```
    
  • 关于r - 如何在命令行中复制 Knit HTML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27246746/

    相关文章:

    r - 整个应用程序 Shiny 的覆盖滚动条中的 div

    r - 使用两个不同的状态值子集重复值

    javascript - 在 Next.js 中将 markdown 文件作为字符串导入

    r - 编织 PDF 时如何去除垃圾代码

    r - 将 ggerrorplot 中表示均值的点更改为一条线

    r - 在分组数据上使用工具提示时,ggvis 中的值消失

    html - Jekyll & KramDown - 如何显示表格边框

    generator - 有推荐的接受 Markdown 文档的静态站点生成器吗?

    r - 使用Shiny和RStudio导入数据

    r - 在 R Studio 中自动查找当前 R 项目的路径