R Formattable -> 导出的 .png 和导出的 html 小部件不匹配

问题描述

我正在使用此代码导出格式化表格的 .png 和 HTML 版本。

.png 出来的很好 (What the table should look like),但 HTML 导出看起来像这样,即使它在查看器窗格中很好:https://rpubs.com/Drewnonymous/739404

第二个不太重要的问题是我也真的很想摆脱 NA,但我所做的每一种方式都搞砸了制作表格或删除了数字格式。

在 20 多年没有编码(从大学开始)之后,我对这一切仍然很陌生,所以请保持温和。我仍然处于“从别人那里借用代码并试图弄清楚它是如何以及为什么工作的阶段”。

非常感谢任何和所有帮助。

percent_change_formatter1 <- 
  formatter("span",style = x ~ formattable::style(
              font.weight = ifelse(x>0 | x<0,"bold",""),color = ifelse(x > 0,customGreen,ifelse(x < 0,customred,"black"))),x ~ icontext(ifelse(x>0,"arrow-up","arrow-down"),x)
  )
percent_change_formatter2 <- 
  formatter("span",color = ifelse(x < 0,ifelse(x > 0,x)
  )


i_tcp <- formattable(i_tcp,align =c("c","r","l","l"),list(
  `7-day averages for USA` = formatter("span",style = ~ style(color = "grey",font.weight = "bold")),`Tests`= color_bar(customGreen0,proportion,1),` % chng.`= percent_change_formatter1,`New cases`= color_bar(CustomOrange0,`% chng. `= percent_change_formatter2,`% positive`= color_bar(CustomPink0,`% chng.`= percent_change_formatter2
))

export_formattable <- function(f,file,width = 690,height = 330,background = "",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)
}

dir.create(paste(today),recursive = T)
setwd(paste(today))
export_formattable(i_tcp,paste(today,"_us_tcp_table.png"))
saveWidget(as.htmlwidget(i_tcp,height = 330),"_us_tcp_table.htm"),selfcontained = F,libdir = "libdir")
setwd("..")

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...