简体   繁体   English

R在脚本中将FlexTable另存为html文件

[英]R save FlexTable as html file in script

I have a FlexTable produced with the ReporteRs package which I would like to export as .html. 我有一个随ReporteRs包一起生产的FlexTable,我想将其导出为.html。

When I print the table to the viewer in RStudio I can do this by clicking on 'Export' and selecting 'Save as webpage'. 当我在RStudio中将表格打印到查看器时,可以通过单击“导出”并选择“另存为网页”来完成。

How would I replicate this action in my script? 如何在脚本中复制此操作?

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). 我现在还不想编织到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). 我已经尝试过as.html函数,但确实会生成一个.html文件,但是缺少所有格式设置(纯文本格式)。

Here is a MWE: 这是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). 上面演示的两种方法的问题在于它们输出的表格没有任何格式(无边框等)。

However, manually selecting 'export' and 'save as webpage' in RStudio renders the FlexTable to a html file with full formatting. 但是,在RStudio中手动选择“导出”和“另存为网页”会将FlexTable呈现为具有完整格式的html文件。 Why is this? 为什么是这样?

这对我有用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM