将knitr :: kable()输出保存到html文件R中 [英] Save knitr::kable() output to html file R

查看:327
本文介绍了将knitr :: kable()输出保存到html文件R中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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")))

这有错误:

UseMethod("write_html")中的错误:没有适用的方法 "write_html"应用于"knitr_kable"类的对象

Error in UseMethod("write_html") : no applicable method for 'write_html' applied to an object of class "knitr_kable"


我的猜测是,必须先将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中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆