https://cran.r-project.org/web/packages/formattable/formattable.pdf
i've been using formattable package make nice looking tables in r. i'm trying save tables images (or file format) can't find command works. using jpeg/png function or dev.copy creates blank documents. ideally i'd able save these tables in loop. know how might done?
data:
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, na, 130.00, 97.05, 50.99)), change=percent(c(-0.0239, -0.0216, 0.021, -0.0219, -0.0248, -0.0399))) formattable(df, list( name=formatter( "span", style = x ~ ifelse(x == "technology", style(font.weight = "bold"), na)), value = color_tile("white", "orange"), change = formatter( "span", style = x ~ style(color = ifelse(x < 0 , "red", "green")), x ~ icontext(ifelse(x < 0, "arrow-down", "arrow-up"), x))) )
to save formattable can use 'as.htmlwidget' , printscreen it. first run next function:
library("htmltools") library("webshot") 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) }
(source: https://github.com/renkun-ken/formattable/issues/26)
then in code assing formattable variable , use function save it.
ft <- formattable(df, list( name=formatter("span", style = x ~ ifelse(x == "technology", style(font.weight = "bold"), na)), value = color_tile("white", "orange"), change = formatter("span", style = x ~ style(color = ifelse(x < 0 , "red", "green")), x ~ icontext(ifelse(x < 0, "arrow-down", "arrow-up"), x))) ) export_formattable(ft,"ft.png")
best regards.
Comments
Post a Comment