问题描述
我正在使用 R formattable包来呈现一些数据帧,但是输出是html(运行脚本后它会打开浏览器).
I'm using R formattable package to render some data frames but the output is html ( it opens the browser after I run the script ).
问题是我试图在PowerBI下呈现那些表,该表接受 R 脚本,但需要输出的是图像(如ggplot)而不是html.但是我不知道该怎么办.
The thing is I'm trying to render those tables under PowerBI which accepts R scripts but need the output to be an image (like a ggplot) not html. But I don't know how I can do it.
我已经研究了R2HTML和htmlwidgets软件包,但仍然没有找到解决方案.(我可能犯了一些错误).
I've looked into R2HTML and htmlwidgets packages but I still didn't find a solution with those. ( I may have made some mistakes ).
这是我正在使用的伪代码:
Here is the dummy code I'm working with:
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)
宽度/高度不一定会精确到指定的高度,您需要对其进行一些手动猜测(或加载 magick
并查看是否可以自动裁剪)使用它.)
The width/height is not necessarily going to come out precisely as what's specified and you'll need to do some manual guessing for it (or load up magick
and see if you can auto-clip using it).
这依赖于 phantomjs
,您可能无法让您的IT和/或安全组启用它.
This relies on phantomjs
and you may not be able to get your IT and/or security groups to enable the use of it.
这篇关于将html输出转换为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!