R将FlexTable另存为脚本中的html文件 [英] R save FlexTable as html file in script

查看:193
本文介绍了R将FlexTable另存为脚本中的html文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个随ReporteRs包一起生产的FlexTable,我想将其导出为.html.

I have a FlexTable produced with the ReporteRs package which I would like to export as .html.

当我在RStudio中将表格打印到查看器时,可以通过单击导出"并选择另存为网页"来完成此操作.

When I print the table to the viewer in RStudio I can do this by clicking on 'Export' and selecting 'Save as webpage'.

如何在脚本中复制此操作?

How would I replicate this action in my script?

我现在还不想编织到html文档或生成报告,因为我现在只想为我的每个草稿表提供单独的文件,可以与合作者共享(但格式很不错,因此易于阅读) ).

I don't want to knit to a html document or produce a report just yet as at present I just want separate files for each of my draft tables which I can share with collaborators (but nicely formatted so they are easy to read).

我尝试了as.html函数,该函数确实会生成一个.html文件,但是缺少所有格式(纯文本格式).

I have tried the as.html function and that does produce a .html file but all the formatting is missing (it is just plain text).

这是MWE:

# load libraries:
library(data.table)
library(ReporteRs)
library(rtable)

# Create dummy table:
mydt <- data.table(id = c(1,2,3), name = c("a", "b", "c"), fruit = c("apple", "orange", "banana"))

# Convert to FlexTable:
myflex <- vanilla.table(mydt)

# Attempt to export to html in script:
sink('MyFlexTable.html')
print(as.html(myflex))
sink()

# Alternately:
sink('MyFlexTable.html')
knit_print(myflex)
sink()

上述两种方法的问题在于它们输出的表格没有任何格式(无边框等).

The problem with both methods demonstrated above is that they output the table without any formatting (no borders etc).

但是,在RStudio中手动选择导出"和另存为网页"会将FlexTable呈现为具有完整格式的html文件.为什么会这样?

However, manually selecting 'export' and 'save as webpage' in RStudio renders the FlexTable to a html file with full formatting. Why is this?

推荐答案

这对我有用:

writeLines(as.html(myflex), "MyFlexTable.html")

这篇关于R将FlexTable另存为脚本中的html文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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