Command for exporting/saving table made with Formattable package in R

后端 未结 1 541
遥遥无期
遥遥无期 2020-12-18 01:24

https://cran.r-project.org/web/packages/formattable/formattable.pdf

I\'ve been using the Formattable package to make some nice looking tables in R. I\'m trying to sa

相关标签:
1条回答
  • 2020-12-18 02:00

    To save you formattable you can use 'as.htmlwidget' and then printscreen it. First run the 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 your code assing the formattable to a variable and use the function to 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.

    0 讨论(0)
提交回复
热议问题