Sharif Amlani
Sharif Amlani

Reputation: 1288

How to export htmlTable from the viewer to a word document in R

I'm working with the htmlTable function. While the table renders perfectly in the R studio viewer, I am unable to save, copy and paste, or screenshot it, such that it looks equally nice in the word document I am writing. I was wondering if there was a way for me to export or save the image, such that the table show up just as nice in the word document.

Here is an example table.

output <- 
  matrix(paste("Example", LETTERS[1:16]), 
         ncol=4, byrow = TRUE)

library(htmlTable)
htmlTable(output)

Upvotes: 1

Views: 3496

Answers (2)

Brent
Brent

Reputation: 4906

It is ideal to import the table into your word processor in an editable form, as opposed to a screenshot or an image. In order to achieve this you need to:

  1. Asign the output of htmlTable to a new variable, then print that variable with the useViewer argument.
tb1 <- htmlTable(output)
print(tb1, useViewer = utils::browseURL)
  1. This will open the table in your default browser.
    1. Most browsers will have a menu option for 'Save page as'. Subsequently you can import the html file into your Word document.
    2. Alternatively you can copy and paste the table from the browser window into the Word processor.

As an aside, many people prefer importing and/or pasting HTML tables into Libre office as the layout and formatting is often more similar to the table rendered by the browser. After doing this in Libre office, you may wish to save the document in MS Word format.

Upvotes: 0

mukund
mukund

Reputation: 633

You can display your table as grid graphics using grid.table function from gridExtra library. And save it as image using ggsave function from ggplot library.

library(ggplot2)
library(gridExtra)

ggsave(grid.table(output), filename = "~DirectoryName/imageName.png")

Upvotes: 2

Related Questions