【问题标题】:Stripe effect in formattable(), shadow in light gray every other row. Rformattable() 中的条纹效果,每隔一行显示浅灰色阴影。 R
【发布时间】:2021-11-12 08:53:00
【问题描述】:

我想知道,有人知道如何实现 kableExtra 中的“条纹”样式吗?那是在浅灰色的奇数行而不是偶数行中阴影?如下图所示:

鉴于我使用formattable的代码,我打算将表格导出为pdf文档,所以我想知道我是否可以达到那个效果?可能吗?我尝试修改 td's、tr's 但实际单元格的阴影非常奇怪,从而产生了不希望的效果。这是我得到的当前代码及其输出:

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)
      #Remove row height!
      w <- htmlwidgets::prependContent(w, tags$style("td { padding: 0px  !Important;}"))
      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 r r-markdown htmlwidgets formattable


    【解决方案1】:

    您可以在 table.attr 参数内使用 .table-striped 将斑马条纹添加到任何表格行。

    代码

    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 = 'class=\"table table-striped\" style="font-size: 18px; font-family: Calibri"')
    

    表格

    【讨论】:

    • 感谢您的回答!跟进问题。如何将默认背景颜色从这种超浅灰色更改为红色或蓝色或任何其他十六进制颜色?谢谢!
    猜你喜欢
    • 2015-02-17
    • 2017-03-12
    • 1970-01-01
    • 2015-05-22
    • 1970-01-01
    • 1970-01-01
    • 2015-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多