我知道这个问题类似于 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
knit2html
最佳答案
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::render
比 knitr::knit2html
做更多的设置,虽然我没有所有差异的详尽 list 。无论如何,呈现输出的最灵活方式是提供您自己的样式表,以根据您的意愿格式化输出。
请注意您需要set up Pandoc manually与
rmarkdown::render
一起工作在命令行上。也就是说 ,这里有两个评论可以改善
knitr::knit2hmtl
输出,并且优于使用 rmarkdown::render
在我看来:# 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/