如何在命令行中复制Knit HTML? [英] How to replicate Knit HTML in a command line?
问题描述
我知道这个问题类似于这个一个但是我在那里找不到解决方案,因此请再次将其发布在这里.
I know this question is similar to this one. But I couldn't get a solution there so posting it here again.
我想通过单击编织HTML"获得与输出完全相同的输出.我尝试使用knit2html,但格式混乱,不包含标题,kable不起作用等.
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.
示例:
这是我的test.Rmd文件,
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.
输出:
编织HTML
knit2html
推荐答案
文档告诉我们:
如果您不使用RStudio,则只需调用
rmarkdown::render
函数,例如:
rmarkdown::render("input.Rmd")
请注意,在RStudio中使用编织"按钮的情况下,基本机制是相同的(RStudio在幕后调用rmarkdown::render
函数).
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
多得多.
In essence, rmarkdown::render
does a lot more setup than knitr::knit2html
, although I don’t have an exhaustive list of all differences.
无论如何,呈现输出的最灵活的方法是提供自己的样式表以根据您的意愿格式化输出.
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.
请注意,您需要手动设置Pandoc 才能正常工作在命令行上使用rmarkdown::render
.
Please note that you need to set up Pandoc manually to work with rmarkdown::render
on the command line.
话虽如此,这里有两条话可以改善knitr::knit2hmtl
的输出,并且在我看来优于使用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:
-
要包含标题,请使用Markdown标题标签,而不要使用YAML标签:
To include the title, use a Markdown title tag, not a YAML tag:
# My title
要格式化表格,请不要使用原始的kable
函数.实际上,使用rmarkdown::render
时也是如此:表格单元格的对齐完全不对. Rmarkdown显然使用居中作为默认对齐方式,但是此选项几乎永远都不正确.相反,您应该使文本左对齐,并且(通常)使数字右对齐.截至撰写本文时,Knitr不能自动执行此操作(据我所知),但是包含过滤器可以很容易地为您执行此操作:
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:
```{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)
})
```
这篇关于如何在命令行中复制Knit HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!