微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?