Last active
August 10, 2016 13:46
-
-
Save magnusnissel/9b3ce0f9c869af7a8abb80633ef9180b to your computer and use it in GitHub Desktop.
A small helper function around webshot and knitr::kable to save the HTML representation of a dataframe as an image file (.png)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#This function depends on the knitr and webshot packages, uncomment lines 2-6 below if you need to install them first | |
#install.packages("knitr") | |
#install.packages("devtools") | |
#library(devtools) | |
#devtools::install_github("wch/webshot") | |
#webshot::install_phantomjs() | |
#Feel free to move the library() calls for knitr and webshot outside the function. | |
dataframe_as_image <- function(dfr, img_path) { | |
suppressPackageStartupMessages(library(knitr)) | |
suppressPackageStartupMessages(library(webshot)) | |
html <- kable(dfr, format="html", table.attr = "id=\"df\"") | |
tmp_path <- tempfile(pattern = "dfrasimg_", fileext = "html") | |
write(html, file=tmp_path) | |
webshot("test.html", img_path, selector="table#df") | |
file.remove(tmp_path) | |
return(img_path) | |
} | |
#Example usage: | |
dfr <- read.csv(file.choose()) | |
img_path <- dataframe_as_image(dfr, "my_dataframe.png") | |
#Released as public domain, or licensed under CC0 (https://creativecommons.org/publicdomain/zero/1.0/legalcode.txt), | |
#whichever gives you the most rights in your legislation. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment