New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

formattable webshots #26

Open
smbache opened this Issue Sep 11, 2015 · 5 comments

Comments

Projects
None yet
6 participants
@smbache
Copy link

smbache commented Sep 11, 2015

Made this simple function to be able to put a formattable in a pdf report; not sure if something like this is useful in the package itself. It exports a formattable as e.g. png. Possibly something more generic would be useful in the webshot package (see also wch/webshot#1). Also it has the external dependency on phantomjs. But in any case, I thought I'd share it here:

#' 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)
}
@renkun-ken

This comment has been minimized.

Copy link
Owner

renkun-ken commented Sep 11, 2015

It looks really interesting and can be helpful to make it work in pdf. Thanks!

@timelyportfolio

This comment has been minimized.

Copy link
Collaborator

timelyportfolio commented Sep 11, 2015

Very nice, will try on my Windows machine. Related similar function wch/webshot#10. Would think add webshot as suggestion, so not required.

@kasal

This comment has been minimized.

Copy link

kasal commented Oct 26, 2016

Thank you, this enabled me to save the table so that it can be used in a report.
But I need pdf output and file='xxx.pdf' loses most of the formatting, because of issue #53.
So I had to modify this function: save the table as an image (png) and then convert it to pdf. (Using im.convert from package 'animation'.)

It would be nice if we had a solution for issue #53, to get a real pdf instead of a wrapped image.

@dlksmc

This comment has been minimized.

Copy link

dlksmc commented Oct 18, 2017

I'm using wkhtmltopdf to convert from HTML (generated with formattable and knitr) to PDF. Works pretty well after adding the CSS below when knitting. It aligns the column headers properly when the table spans multiple pages.

thead {
  display: table-header-group;
}

tr {
  page-break-inside: avoid;
}
@laresbernardo

This comment has been minimized.

Copy link

laresbernardo commented Nov 16, 2018

Any new update on this function? Why can't we simply run the following code?

jpeg(file='file.jpeg')
formattableobject
dev.off()

Please, add a function or an easy way to export the results into an image without having to install phantomjs and further stuff. Hope you can help us. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment