R:将 jQuery 小部件转换为普通 html 或 png
R: transform jQuery widget as plain html or png
我使用 formattable
创建了一个小部件
library(formattable)
products <- data.frame(id = 1:5,
price = c(10, 15, 12, 8, 9),
rating = c(5, 4, 4, 3, 4),
market_share = percent(c(0.1, 0.12, 0.05, 0.03, 0.14)),
revenue = accounting(c(55000, 36400, 12000, -25000, 98100)),
profit = accounting(c(25300, 11500, -8200, -46000, 65000)))
sign_formatter <- formatter("span",
style = x ~ style(color = ifelse(x > 0, "green",
ifelse(x < 0, "red", "black"))))
f = formattable(products, list(profit = sign_formatter))
f
显示效果很好。但是我需要将结果作为电子邮件的正文,因此不能使用外部 jQuery 库。有没有办法把它变成普通的 html,而不丢失颜色?
或者,有没有办法以编程方式从格式化表创建 png?
谢谢
编辑:第二个例子更高级一点,在应用 as.htmlwidget
时是 'simplified'
f = formattable(products, list(
price = color_tile("transparent", "lightpink"),
rating = color_bar("lightgreen"),
market_share = color_bar("lightblue"),
revenue = sign_formatter,
profit = sign_formatter))
f
这取决于您希望 HTML 有多简单。您可以执行以下操作:
f <- formattable(products, list(profit = sign_formatter))
h <- as.htmlwidget(f)
cat(h$x$html)
这会将 table 的 HTML 输出到控制台 - 当然,您可以根据需要将其分配给变量。
值得注意的是,这保留了您的 formatter
分配的布局(green/red 文本),但去除了 R 的标准 table 设计的 table。对于其标准布局,R 使用 Bootstrap 库,您也可以找到 CSS 文件 here.
# You need these
library(webshot)
library(htmlwidgets)
library(magick)
# it's not a widget by default so make it one
f_w <- as.htmlwidget(f)
# save it out to a file
saveWidget(f_w, "out.html")
# it's not a shiny app
webshot("out.html", file = "out.png")
# you have what you want but there's alot of whitespace
image_read("out.png")
# so we can deal with that
img <- image_read("out.png")
img <- image_trim(img)
image_write(img, "out2.png")
我使用 formattable
library(formattable)
products <- data.frame(id = 1:5,
price = c(10, 15, 12, 8, 9),
rating = c(5, 4, 4, 3, 4),
market_share = percent(c(0.1, 0.12, 0.05, 0.03, 0.14)),
revenue = accounting(c(55000, 36400, 12000, -25000, 98100)),
profit = accounting(c(25300, 11500, -8200, -46000, 65000)))
sign_formatter <- formatter("span",
style = x ~ style(color = ifelse(x > 0, "green",
ifelse(x < 0, "red", "black"))))
f = formattable(products, list(profit = sign_formatter))
f
显示效果很好。但是我需要将结果作为电子邮件的正文,因此不能使用外部 jQuery 库。有没有办法把它变成普通的 html,而不丢失颜色?
或者,有没有办法以编程方式从格式化表创建 png?
谢谢
编辑:第二个例子更高级一点,在应用 as.htmlwidget
f = formattable(products, list(
price = color_tile("transparent", "lightpink"),
rating = color_bar("lightgreen"),
market_share = color_bar("lightblue"),
revenue = sign_formatter,
profit = sign_formatter))
f
这取决于您希望 HTML 有多简单。您可以执行以下操作:
f <- formattable(products, list(profit = sign_formatter))
h <- as.htmlwidget(f)
cat(h$x$html)
这会将 table 的 HTML 输出到控制台 - 当然,您可以根据需要将其分配给变量。
值得注意的是,这保留了您的 formatter
分配的布局(green/red 文本),但去除了 R 的标准 table 设计的 table。对于其标准布局,R 使用 Bootstrap 库,您也可以找到 CSS 文件 here.
# You need these
library(webshot)
library(htmlwidgets)
library(magick)
# it's not a widget by default so make it one
f_w <- as.htmlwidget(f)
# save it out to a file
saveWidget(f_w, "out.html")
# it's not a shiny app
webshot("out.html", file = "out.png")
# you have what you want but there's alot of whitespace
image_read("out.png")
# so we can deal with that
img <- image_read("out.png")
img <- image_trim(img)
image_write(img, "out2.png")