giordano
giordano

Reputation: 3152

R latex output table as jpg-file

I would like to output a latex-table as jpg from R. For example:

df <- data.frame(variable = c("a","b","c"), n=1:3)
xtable(df)

I could use knitr with:

\documentclass{article}
\usepackage[english,ngerman]{babel}

\begin{document}

<<echo=FALSE, results="asis">>=
xtable(xdf)
@

\end{document}

But this produces a pdf-file. I could screenshot the table and save it as jpg but I would like to avoid this work around.

My preference would be to output the latex-table from R into a windows

windows(width=20,height=15)

and then to save as jpg-file

savePlot("latex_table", type="jpg")

But I suppose that this is not possible since it has to be first interpreted by latex.

The reason why I want to do it in this way: I would like to produce regulary updated graphs and tables as jpg to insert them manually into a web-page. I want to use latex-table to have all the capacity of design to create nice tables.

Upvotes: 2

Views: 1452

Answers (1)

gagolews
gagolews

Reputation: 13056

You may use ImageMagick's convert tool (executed with the system2() function in R) to convert a PDF file to JPEG. Before that you might wish to crop the margins in your LaTeX document (see pdfcrop). I cannot help you with explaining how to install these tools on Windows but e.g. on Fedora Linux you just sudo yum install texlive-pdfcrop-bin ImageMagick. Then you call:

  1. knitr::knit2pdf(...)
  2. system2('pdfcrop', 'input.pdf output.pdf')
  3. system2('convert', 'output.pdf output.jpg')

Everything can be set up to run in batch mode, without any user interaction, so you might even think of trying to automate uploading the figure to a web server.

EDIT: You may also try with \documentclass[11pt]{standalone} in LaTeX to produce a PDF page with minimal margins.

Upvotes: 2

Related Questions