解决问题的第一部分:
df <- data.frame(
id = 1:10,
A = sample(20:80, 10),
B = sample(1:1000, 10),
C = sample(1:10, 10),
D = percent(runif(10, 0, 1), format = "d"),
E = percent(runif(10, 0, 1), format = "d"),
stringsAsFactors = FALSE)
formattable(df, list(
A = color_tile("black", 0.2),
B = color_tile("pink", 0.2),
C = color_tile("orange", "gray"),
D = color_tile("blue", 0.2),
E = color_tile("green", 0.2)))
有关更多信息,请参阅文档:https ://github.com/renkun-ken/formattable
关于 pdf 渲染 - 您始终可以将您的表格设为“htmlwidget”,然后将其设为 pdf 打印屏幕。在 R 中,您可以尝试此功能(来源):
#' Export a Formattable as PNG, PDF, or JPEG
#'
#' @param f A formattable.
#' @param file Export path with extension .png, .pdf, or .jpeg.
#' @param width Width specification of the html widget being exported.
#' @param height Height specification of the html widget being exported.
#' @param background Background color specification.
#' @param delay Time to wait before taking webshot, in seconds.
#'
#' @importFrom formattable as.htmlwidget
#' @importFrom htmltools html_print
#' @importFrom webshot webshot
#'
#' @export
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)
}