Rmarkdown - 为表格添加颜色条以获取pdf和html

时间:2015-12-29 15:31:17

标签: r latex knitr r-markdown sparklines

我正在尝试使用Rmarkdown向表格添加条形图。我让它适用于HTML。

HTML表格完美无缺

a <- c(1,2,3)
b <- c(4,5,6)
table <- as.data.frame(rbind(a,b)) #Demo table for this example
summary_column <- c(40,60) #A summary column, showing 40% and 60%.

#add the summary column using background-color
table[["summary_column"]] <- paste0( 
  "<span style='display: block; border-radius: 4px; padding-right: 4px; background-color: #00457C; color:white; width:",
  summary_column,"%;>", #Size of bar depends on the value of the vector summary_column
  summary_column,"%","</span>") #The bar has an overlaying valuelabel

library(pander)
pandoc.table(table) #convert the table to html table

也可以使用formattable包实现(我的代码几乎与他们的代码相同)。

Result HTML table

然而我的问题是
当我编写pdf(或Word就此问题)时,如何让这个条形图工作?我不想将表转换为图像,因为我需要表中的交互性。我认为使用<img> html标签可能适用于pdf,但它不适用(但在HTML中有效):

table[["summary_column"]] <- paste0( #add summary column
  "<img src='bar.bmp'>") #add the bar.bmp to the column

其中bar.bmp是一个5x10px img,我可以扩展到X% pdf不会显示背景颜色也不会显示img。

Resulting table when I knit to pdf

我应该为栏添加包含HTML代码的列,还为栏添加一些Tex代码?我对乳胶的了解有限。

有没有办法让乳胶引擎(xelatex)读取css或html标签?

谢谢

0 个答案:

没有答案