问题描述
我有一个knitr_kable输出,我想将其保存为R中的HTML文档.我需要它能够从R脚本中自动运行,而无需人工干预.例如:
I have a knitr_kable output that I want to save as an HTML document from R. I need this to run automatically from my R script with no human involvement. For example:
dt <- mtcars[1:5, 1:6]
kable(dt, "html") %>% kable_styling(bootstrap_options = c("striped", "hover"))
这具有html输出,但是类为knitr_kable
,所以我无法将其写到表或html文件中,因为不能将其强制转换为数据框.
This has html output but the class is knitr_kable
so I can't write it to a table or html file because it cannot be coerced to a dataframe.
class(kable(dt, "html"))
[1] "knitr_kable"
有人可以将其中的一个电缆保存为html文件吗?
Does anyone have a method for saving one of these kables as an html file?
我尝试过:
library(xml2)
options(knitr.table.format = "html")
write_html(kable(dt, "html"), "df.html")))
这有错误:
我的猜测是,必须先将knitr_kable对象强制转换为html对象,然后再将其另存为html文件.但是我不确定该怎么做.
My guess would be that the knitr_kable object must first be coerced to an html object and then saved as html file. But I'm not sure how to do that.
推荐答案
cat
函数将满足您的需求.
The cat
function will do what you need.
library(knitr)
library(kableExtra)
library(magrittr)
dt <- mtcars[1:5, 1:6]
kable(dt, "html") %>%
kable_styling(bootstrap_options = c("striped", "hover")) %>%
cat(., file = "df.html")
结果表如下:
这篇关于将knitr :: kable()输出保存到html文件R中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!