Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have R formattable rendered to pdf output and how to have percents in the table

I´m starting using R formattablepackage and still facing some problems to have chunks with formattable() outputting pdf document properly.

First problem: how to have numbers in percent format after applying any of the color_* functions available for numeric class only?

See the table below from the code presented executed/ran in Renviroment.

a<-formattable(x=a,list(A=color_bar("orange", 0.2),B=color_bar("orange", 0.2),C=color_bar("orange", 0.2),D=color_bar("orange", 0.2),E=color_bar("orange", 0.2)))

Assume a as a csvfile I had input by read.csv().

enter image description here

I want to have the "%" pasted together the numbers and, at same time, the orange bars from formattable, but if I convert the numeric to percent either by percent() from scale or by paste(a,"%",sep="") formattable will not work acussing numeric is required.

Second problem: When rendering to pdf, such table from the chunk presented is not being properly created. I tried to use direct output from formattable(a,list...), by print(a)and by print(xtable(a)) but did not work in any way. Any hint?

like image 775
Fábio Salles Avatar asked Oct 19 '22 17:10

Fábio Salles


1 Answers

Solution to the first part of your question:

df <- data.frame(
  id = 1:10,
  A = sample(20:80, 10),
  B = sample(1:1000, 10),
  C = sample(1:10, 10),
  D = percent(runif(10, 0, 1), format = "d"), 
  E = percent(runif(10, 0, 1), format = "d"), 
  stringsAsFactors = FALSE)

formattable(df, list(
  A = color_tile("black", 0.2),
  B = color_tile("pink", 0.2),
  C = color_tile("orange", "gray"),
  D = color_tile("blue", 0.2),
  E = color_tile("green", 0.2)))

See documentation for further information: https://github.com/renkun-ken/formattable

Regarding pdf rendering - you can always make your table a 'htmlwidget' and then make pdf printscreen of it. In R you may try this function (source):

#' Export a Formattable as PNG, PDF, or JPEG
#'
#' @param f A formattable.
#' @param file Export path with extension .png, .pdf, or .jpeg.
#' @param width Width specification of the html widget being exported.
#' @param height Height specification of the html widget being exported.
#' @param background Background color specification.
#' @param delay Time to wait before taking webshot, in seconds.
#'
#' @importFrom formattable as.htmlwidget
#' @importFrom htmltools html_print
#' @importFrom webshot webshot
#'
#' @export
export_formattable <- function(f, file, width = "100%", height = NULL, 
                               background = "white", 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)
}
like image 189
Tomasz Mikolajczyk Avatar answered Dec 15 '22 17:12

Tomasz Mikolajczyk