gpt4 book ai didi

html - 将 html 输出转换为图像

转载 作者:行者123 更新时间:2023-12-04 03:11:02 25 4
gpt4 key购买 nike

我正在使用 R 可格式化包来呈现一些数据帧,但输出是 html(它在我运行脚本后打开浏览器)。

问题是我正在尝试在接受 R 脚本但需要输出为图像(如 ggplot)而不是 html 的 PowerBI 下呈现这些表。但我不知道该怎么做。

我研究了 R2HTML 和 htmlwidgets 包,但我仍然没有找到解决方案。 (我可能犯了一些错误)。

这是我正在使用的虚拟代码:

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)))
DF
## Ticker Name Value Change
## 1 Dow Jones 15,988.08 -2.39%
## 2 S&P 500 1,880.33 -2.16%
## 3 Technology NA 2.10%
## 4 IBM IBM 130.00 -2.19%
## 5 AAPL Apple 97.05 -2.48%
## 6 MSFT Microsoft 50.99 -3.99%
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))))

最佳答案

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)))
) -> w

htmlwidgets::saveWidget(as.htmlwidget(w), "/some/dir/table.html", selfcontained = TRUE)

webshot::webshot(url = "/some/dir/table.html", file = "/some/dir/table.png",
vwidth = 1000, vheight = 275)

enter image description here

宽度/高度不一定会像指定的那样精确显示,您需要对其进行一些手动猜测(或加载 magick 并查看是否可以自动剪辑使用它)。

这依赖于 phantomjs,您可能无法让您的 IT 和/或安全团队启用它。

关于html - 将 html 输出转换为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45298144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com