将 table 行边框更改为类似于 kable LaTeX 的内容

Changing table line borders to something similar to kable LaTeX

我有以下 table 我想知道是否有任何方法可以更改 table 格式,使分隔线不仅可以水平显示,而且可以作为与 excel 中一样的单元格网格,或者可能与 kable、kableExtra 中一样。然而,我认为我看到的唯一示例以某种 HTML 方式呈现 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)))

FT <- formattable(DF, list(
  Name=formatter("span", 
                 style = x ~ ifelse(x == "Technology", style(font.weight = "bold"), NA)), 
  #Value = color_tile("white", "orange"),
  Value = color_bar("orange" , fun = unit.scale
                    ),
  Change = formatter("span", 
                     style = x ~ style(color = ifelse(x < 0 , "red", "green")), 
                     x ~ icontext(ifelse(x < 0, "arrow-down", "arrow-up"), x))) )

FT

当前table:

我想使用的样式:

万一网格方格样式不可行,你认为至少可以将分割线从原来的灰色改为黑色或红色线条吗?

谢谢!

您可以像这样将 css 添加到最终的 HTML 小部件中:

library(webshot)
library(formattable)
library(htmltools)
library(htmlwidgets)

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

formattable(DF) %>%
  as.htmlwidget() %>%
  prependContent(tags$style("table,td,tr,th { border: 1px solid black !important;}"))