[英]Command for exporting/saving table made with Formattable package in R
https://cran.r-project.org/web/packages/formattable/formattable.pdf
我一直在使用Formattable包在R中制作一些漂亮的表。我試圖將表保存為圖像(或實際上是任何文件格式),但找不到有效的命令。 使用jpeg / png函數或dev.copy會創建空白文檔。 理想情況下,我希望能夠將這些表保存在循環中。 有誰知道如何做到這一點?
數據:
library(formattable)
DF <- data.frame(Ticker=c("", "", "", "IBM", "AAPL", "MSFT"),
Name=c("Dow Jones", "S&P 500", "Technology",
"IBM", "Apple", "Microsoft"),
Value=accounting(c(15988.08, 1880.33, NA,
130.00, 97.05, 50.99)),
Change=percent(c(-0.0239, -0.0216, 0.021,
-0.0219, -0.0248, -0.0399)))
formattable(DF, list(
Name=formatter(
"span",
style = x ~ ifelse(x == "Technology",
style(font.weight = "bold"), NA)),
Value = color_tile("white", "orange"),
Change = formatter(
"span",
style = x ~ style(color = ifelse(x < 0 , "red", "green")),
x ~ icontext(ifelse(x < 0, "arrow-down", "arrow-up"), x)))
)
要保存格式化表格,您可以使用“as.htmlwidget”然后將其打印出來。 首先運行下一個功能:
library("htmltools")
library("webshot")
export_formattable <- function(f, file, width = "100%", height = NULL,
background = "white", delay = 0.2)
{
w <- as.htmlwidget(f, width = width, height = height)
path <- html_print(w, background = background, viewer = NULL)
url <- paste0("file:///", gsub("\\\\", "/", normalizePath(path)))
webshot(url,
file = file,
selector = ".formattable_widget",
delay = delay)
}
(來源: https : //github.com/renkun-ken/formattable/issues/26 )
然后在您的代碼中將formattable分配給變量並使用該函數來保存它。
FT <- formattable(DF, list(
Name=formatter("span",
style = x ~ ifelse(x == "Technology", style(font.weight = "bold"), NA)),
Value = color_tile("white", "orange"),
Change = formatter("span",
style = x ~ style(color = ifelse(x < 0 , "red", "green")),
x ~ icontext(ifelse(x < 0, "arrow-down", "arrow-up"), x))) )
export_formattable(FT,"FT.png")
最好的祝福。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.