11

Is there a way to print, from within R, a LaTeX table directly to an image file (for inclusion in another document/webpage). Basically, I'd like to supply LaTeX code to a function that saves it as an image to the working directory.

Pipe dreams?

Brandon Bertelsen
  • 43,807
  • 34
  • 160
  • 255

5 Answers5

7

There are various LaTeX-to-Image converter scripts, designed to do things like convert equations into images for including on web pages.

If you can find one of those (dvipng perhaps?) then you can go from a table in R to LaTeX easy enough and then from LaTeX to png.

If you have dvipng, you can leverage Hmisc's latex conversions to make a neater function to do it:

dvipng.dvi <-
  function (object, file, ...) 
{
  cmd <- if (missing(file)) 
    paste("dvipng -T tight", shQuote(object$file))
  else paste("dvipng -T tight", "-o", file, shQuote(object$file))
  invisible(sys(cmd))
}

And then you can do:

> tt   # here is a table
   y
x    1  2  3
  1  9 12 11
  2 18  9 10
  3 10  7 14
> dvipng.dvi(dvi.latex(latex(tt)))

And that will produce a png file with a random name in the working directory. The -T tight option will crop all the whitespace from round it.

That's about as direct as I can think it possible.

Linux or Windows or Mac or Atari?

Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • Well if Hmisc works on your system and dvipng is included in your TeX then my solution could work... – Spacedman Feb 15 '12 at 20:10
  • I see a DVI file in the directory (not familiar with that format) but no PNG. – Brandon Bertelsen Feb 15 '12 at 20:36
  • And I double checked, I do have the dvipng installed with my miktex dist. – Brandon Bertelsen Feb 15 '12 at 20:53
  • There's some weirdness going on with the path on my system but this concept works overall (if I adjust object$file, to where the file actually is) – Brandon Bertelsen Feb 15 '12 at 21:36
  • 2
    FYI, dvi is the original TeX output format. In those days, every printer company had its own print control language, so TeX produced dvi, and all you needed was a dvi-to-whatever for each printer. Its less important since pdflatex which generated pdf without it. – Spacedman Feb 16 '12 at 08:19
  • Maybe `dvi.latex` was exported at the time of the post but it's not anymore so the solution fails. So either use `Hmisc:::dvi.latex` or just `dvi`. I can't test the result as I don't have `yap` nor `dvipng` and can't install them. – moodymudskipper Feb 21 '18 at 12:46
  • @Spacedman would you have a solution leveraging what comes with rstudio and R packages only ? See my latest question: https://stackoverflow.com/questions/48903882/plot-xtable-or-save-it-as-image?noredirect=1#comment84814518_48903882 – moodymudskipper Feb 21 '18 at 12:48
5

xtable provides the option to output either latex or html markup. You could put the html in directly.

Having said that I too would like to be able to go directly from knit or sweave to png or svg. I was trying to do this just last week. I am building an inkscape infographic (svg) and have been looking for a way to insert a linked image of a table that updates by running R code.

In initial testing I combined the use of xtable and Spacedman's (+1) code to get some nice png output (Ubuntu). -D option allows controlling of resolution.

I'm looking into a dvi->svg converter which is more like what I am after. http://dvisvg.sourceforge.net/

dvipng.dvi <- function (object, file, res=600)
{
    if (missing(file)){
        invisible(sys(
                paste("dvipng -T tight", "-D", res, shQuote(object$file)))
            )
}
    else{
        invisible(sys(
                paste("dvipng -T tight", "-D", res, "-o", file, shQuote(object$file)))
            )
    }
}

tt <- head(iris)

dvipng.dvi(dvi.latex(latex(xtable(tt))), file='iris.png')
Brent
  • 131
  • 1
  • 3
3

With Spaceman's answer, I was able to come up with a solution that does not rely on latex from the Hmisc package as latex was causing some path problems for me:

table.png <- function(obj, name) { 
first <- name
name <- paste(name,".tex",sep="")
sink(file=name)
cat('
\\documentclass{report}
\\usepackage[paperwidth=5.5in,paperheight=7in,noheadfoot,margin=0in]{geometry}
\\begin{document}\\pagestyle{empty}
')
print(xtable(obj))
cat('
\\end{document}
')
sink()
texi2dvi(file=name)
cmd <- paste("dvipng -T tight", shQuote(paste(first,".dvi",sep="")))
invisible(sys(cmd))
cleaner <- c(".tex",".aux",".log",".dvi")
invisible(file.remove(paste(first,cleaner,sep="")))
}
Brandon Bertelsen
  • 43,807
  • 34
  • 160
  • 255
1

The kableExtra package allows to make nice and customized tables via LaTeX or HTML. It also has a function to export such standalone tables to, for example, PNG or PDF via webshot. Here is an example:

library(kableExtra)
knitr::kable(mtcars[1:6, 1:5], "latex", booktabs = TRUE, linesep = "") %>% 
    kable_styling(full_width = TRUE, font_size = 12) %>% 
    column_spec(1, width = "4cm") %>% 
    save_kable(file = "table.png")

Export table to png using kableExtra

hplieninger
  • 3,214
  • 27
  • 32
-2

I don't have a good answer that includes using R, but if you were desperate, I suppose a 'print screen' and a copy to Paint or other such program and finally saving it would at least get you the image in a storable format.

I've created 'step by step' user documentation this way when other options weren't available.