exporting images of tables
Closed this issue · 2 comments
I have looked into the button-click option using htmltools::html_print()
. There seems to be a problem on Mac, which always saves a blank page. If it works (e.g. on Ubuntu), you need to adjust height and width, which can be cumbersome. Another disadvantage is that you cannot export images in code, which would be a good feature for PDF/Word export in Markdown.
This is my proposed solution for exporting tableHTML as an image, using the webshot
package. It uses the css selector to only export the table. Also, you can adjust the resolution.
library(tableHTML)
library(webshot)
export_tableHTML <- function(tableHTML,
file = 'tableHTML_export.png',
selector = 'table',
...) {
temp_file <- tempfile(pattern = 'tableHTML',
fileext = '.html')
write_tableHTML(tableHTML,
file = temp_file,
complete_html = TRUE)
webshot(temp_file, selector = selector, ...)
invisible(NULL)
}
mtcars %>%
tableHTML() %>%
export_tableHTML()
Note: If you export a jpeg, you need to set the background of the table to white, otherwise it will be all black:
mtcars %>%
tableHTML() %>%
add_css_table(css = list('background-color', 'white')) %>%
export_tableHTML(file = 'tableHTML_export.jpeg')
New function tableHTML_to_image()
was implemented to export a tableHTML to image.