R格式化设置行高
R formattable setting row height
我正在使用格式table,然后生成 table 并将其作为 html 小部件粘贴到 pdf 文档,如您从代码中看到的那样。但是我想知道是否有任何方法可以更改 table!
的行高
library("htmltools")
library("webshot")
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, 50,
130.00, 97.05, 50.99)),
Change=percent(c(-0.0239, -0.0216, 0.021,
-0.0219, -0.0248, -0.0399)))
################################## FUNCTIONS ##################################
unit.scale = function(x) (x - min(x)) / (max(x) - min(x))
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)
}
###############################################################################
FT <- formattable(DF, align =c("l","c","r","c"), list(
Name=formatter("span",
style = x ~ ifelse(x == "Technology", style(font.weight = "bold"), NA)), #NOT APPLIED when we output to PNG with the function!
#Value = color_tile("white", "orange"),
Value = color_bar("orange" , fun = unit.scale
),
Change = formatter("span",
style = x ~ style(color = ifelse(x < 0 , "red", "green"), "font.size" = "18px"),
x ~ icontext(ifelse(x < 0, "arrow-down", "arrow-up"), x)
)),
table.attr = 'style="font-size: 18px; font-family: Calibri";\"')
FT
#OUTPUT the table in the document as an image!
export_formattable(FT,"/outputpath/FT.png")
如您所见,行之间有相当大的间隙,这在 html 文档中是可以的,但在 pdf 中看起来很奇怪。我已经检查了文档和网络,没有直接的答案。
我怎样才能减少所有行的 table 高度,即使我不想设置任何 space 并且让行内容碰撞“接触”分隔行线。
您可以删除填充以使行高尽可能紧凑:
library(tidyverse)
library(formattable)
library(htmlwidgets)
scores <- data.frame(id = 1:5,
prev_score = c(10, 8, 6, 8, 8),
cur_score = c(8, 9, 7, 8, 9),
change = c(-2, 1, 1, 0, 1))
formattable(scores) %>%
as.htmlwidget() %>%
prependContent(tags$style("td { padding: 0px !important;}"))
我正在使用格式table,然后生成 table 并将其作为 html 小部件粘贴到 pdf 文档,如您从代码中看到的那样。但是我想知道是否有任何方法可以更改 table!
的行高library("htmltools")
library("webshot")
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, 50,
130.00, 97.05, 50.99)),
Change=percent(c(-0.0239, -0.0216, 0.021,
-0.0219, -0.0248, -0.0399)))
################################## FUNCTIONS ##################################
unit.scale = function(x) (x - min(x)) / (max(x) - min(x))
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)
}
###############################################################################
FT <- formattable(DF, align =c("l","c","r","c"), list(
Name=formatter("span",
style = x ~ ifelse(x == "Technology", style(font.weight = "bold"), NA)), #NOT APPLIED when we output to PNG with the function!
#Value = color_tile("white", "orange"),
Value = color_bar("orange" , fun = unit.scale
),
Change = formatter("span",
style = x ~ style(color = ifelse(x < 0 , "red", "green"), "font.size" = "18px"),
x ~ icontext(ifelse(x < 0, "arrow-down", "arrow-up"), x)
)),
table.attr = 'style="font-size: 18px; font-family: Calibri";\"')
FT
#OUTPUT the table in the document as an image!
export_formattable(FT,"/outputpath/FT.png")
如您所见,行之间有相当大的间隙,这在 html 文档中是可以的,但在 pdf 中看起来很奇怪。我已经检查了文档和网络,没有直接的答案。
我怎样才能减少所有行的 table 高度,即使我不想设置任何 space 并且让行内容碰撞“接触”分隔行线。
您可以删除填充以使行高尽可能紧凑:
library(tidyverse)
library(formattable)
library(htmlwidgets)
scores <- data.frame(id = 1:5,
prev_score = c(10, 8, 6, 8, 8),
cur_score = c(8, 9, 7, 8, 9),
change = c(-2, 1, 1, 0, 1))
formattable(scores) %>%
as.htmlwidget() %>%
prependContent(tags$style("td { padding: 0px !important;}"))