Search code examples
rphantomjsr-markdowngtsummary

Phantomjs error preventing export of gtsummary tables for use in RMarkdown


I'm trying to save and export tables of summary statistics and regression output from an R Studio session so that I can open them all together in an RMarkdown document, knit that document to html, and share it with offline collaborators. I don't want to have all my code in the RMD file, just the tables (and graphs). The approach to generating tables that has worked the best for me is gtsummary, but when I try to save tables as a .png file that I can open in RMD, as described here, I get the error, "PhantomJS not found. You can install it with webshot::install_phantomjs()." I cannot do this because I am on a secure computer that cannot download packages from anything except CRAN. Is there a workaround for this? The one discussion I've seen about this on SO essentially seemed to say forget about gtsummary and use a different package, but I have spent a lot of time trying to find an approach to generating regression tables that works for my models, and I would love to be able to use gtsummary.


Solution

  • That's a bummer you are not able to install phantomjs. FYI, it's not another R package, it's a separate program used to convert an HTML page (in your case an HTML table) into an image. https://bitbucket.org/ariya/phantomjs/downloads/

    Here are a couple of things you could try:

    1. You don't need to save the gtsummary table as an image to later include it in an Rmd report without the raw code. My personal workflow is to keep all analysis separate from the report. In my analysis file, I create the gtsummary table and save it to file with save() or saveRDS(). In my separate report file, I load the file I saved with the gtsummary table, and print it with echo = FALSE in the code chunk (this avoids printing the R code).

    2. Instead of saving the table as a png, you can save the table as an html table with tbl_summary(trial) %>% as_gt() %>% gt::gtsave(filename = "my_gtsummary.html"). I've never tried injecting the HTML code for an entire table in my Rmd document, but I have done it with HTML code to create a 2 column document. It seems that something along these lines would work.

    Hopefully one of these will work for you! Happy Coding!