0

Following this previous question, I thought I could first convert the output of stargazer (latex code for summary statistics table) to png, using R commands (like dvi, dvips, ...) inside an R chunk or, in the worst case, invoking system commands (see this post), and then import the produced png into my Rmd file, using a command like

![alt text](summary_lm.png)

Do you think this is possible? Could you show me how to do it, because I had no luck?

Community
  • 1
  • 1
gd047
  • 29,749
  • 18
  • 107
  • 146
  • 4
    I'm really not sure that converting a table to png is a good idea. You will get not reflowable layout, not searchable content, big file size, no possibility to zoom in without pixelisation.... – juba Feb 12 '13 at 07:58
  • I agree. Can you suggest a better way to include a table like this http://www.r-statistics.com/wp-content/uploads/2013/01/stargazer_regression.jpg in my (knit2html) page? – gd047 Feb 12 '13 at 08:12
  • Well, as @Yihui pointed out in your previous question, ideally you would produce your table as HTML. But I don't know if there is a package that can format these nicely as `stargazer` do, unfortunately... – juba Feb 12 '13 at 08:21
  • 1
    If you created a pdf and used `\includepdf` (http://texblog.org/tag/includepdf/), the table would be searchable, at least. – Mikko Feb 12 '13 at 08:39
  • @Largh The problem is that I am not creating a LaTeX document but an Rmd. – gd047 Feb 12 '13 at 08:54
  • You can use the HTML `object` tag to include a PDF file (containing just the table) in an HTML (or Markdown) file: ``. – Vincent Zoonekynd Feb 12 '13 at 10:31
  • @Vincent Zoonekynd Nice! The question now is how to automatically convert stargazer's latex code to pdf :-) – gd047 Feb 12 '13 at 11:40

1 Answers1

6

You can use the object tag to embed a PDF file in an HTML file (but it is not supported by all web browsers).

One tricky point is that the size of the PDF files generated by LaTeX has to be set in advance; but, to display a table, you may prefer if the PDF file had exactly the size of the table, to avoid large empty margins and/or scrolling bars. You can use GhostScript to crop the PDF file.

Here is an example.

# Sample data
library(stargazer)
example(stargazer)
code <- .Last.value$value

# Generate the LaTeX file
file <- tempfile("latex_", fileext = "")
tex_file  <- paste( file, "tex",  sep="." )
pdf_file  <- paste( file, "pdf",  sep="." )
pdf_file2 <- paste( file, "_cropped.pdf",  sep="" )
png_file  <- paste( file, "png",  sep="." )
html_file <- paste( file, "html", sep="." )
cat( 
  "\\documentclass{report}",
  # Unreasonably tall page size: we want everything on the same page
  "\\usepackage[paperwidth=10cm,paperheight=100cm,noheadfoot,margin=0in]{geometry}",
  "\\begin{document}",
  "\\pagestyle{empty}", 
  paste(code, collapse="\n"),
  "\\end{document}\n", 
  sep="\n",
  file = tex_file
)

# Generate the PDF file
old_wd <- getwd()
setwd( tempdir() )
system(paste( "pdflatex --interaction=nonstopmode", shQuote(tex_file) ))

# We need to crop the file.
# I will use Ghostscript, but you could also use
#   http://pdfcrop.sourceforge.net/
# or
#   http://www.ctan.org/tex-archive/support/pdfcrop
# First, find the dimensions 
bbox <- system(paste( "gs -sDEVICE=bbox -dNOPAUSE -dBATCH -f", pdf_file, "2>&1" ), intern=TRUE)
bbox <- bbox[ grep("%%BoundingBox", bbox) ]
bbox <- as.numeric( strsplit(bbox, " ")[[1]][-1] )
# Then, crop the file
cmd <- paste( 
  "gs -sDEVICE=pdfwrite",
  "-o", pdf_file2, 
  "-c \"[/CropBox [", paste(bbox, collapse=" "), "] /PAGES pdfmark\"",
  "-f", pdf_file 
)
system(cmd)

# Convert it to PNG, in case the browser cannot display inline PDF files.
# This assumes that ImageMagick is installed.
# You may want to play with the options to have a better quality and/or larger file.
system(paste( "convert", "-trim", "-density 400", pdf_file2, "-resize 50%", png_file ))

# You can now include it in an HTML file 
# (or a Markdown file, since you can include raw HTML).
cat(
  "<html><body><p>Here is an embedded PDF file.</p>\n",
  "<object width='100%' height='100%' type='application/pdf' data='", pdf_file2, "'>",
  "<img src='", png_file, "'/>",
  "</object>\n",
  "</body></html>",
  sep="",
  file=html_file
)

# Check that the result can be displayed in your browser
# (Chrome should be fine, but I have not had any success with the others.)
browseURL( html_file )
Vincent Zoonekynd
  • 31,893
  • 5
  • 69
  • 78
  • Thank you. Could you add an extra step? Convert the cropped pdf to jpg, to be sure that the result will be displayed in any browser – gd047 Feb 13 '13 at 08:19
  • JPEG, though good for photographs, is a very bad idea for text or diagrams: it looks blurry even if you do not try to enlarge it. PNG is preferable. I have updated my answer accordingly. – Vincent Zoonekynd Feb 13 '13 at 09:33
  • installed ghostscript from http://www.ghostscript.com/download/ , but I still get a 'gs' not found error – gd047 Feb 13 '13 at 09:59
  • It may be called differently on Windows (gswin64c.exe?). You may also have to add it to your PATH, manually, or use the full path to the executable. – Vincent Zoonekynd Feb 13 '13 at 10:12
  • There might be two executables, an interactive, graphical one (gswin32?) and a command-line one (gswin32c?). You want the latter. – Vincent Zoonekynd Feb 13 '13 at 10:54
  • Ok two more problems now. 1) `convert` terminates with status 4, when run from within R, while has no problem when I run it from command line. And 2) the resulting png has width=788 pixels and height=7874pixels !!! (while pdf is perfectly cropped) ?!?! (thanks for your patience, again) – gd047 Feb 13 '13 at 11:25
  • 1. Is there an error message? 2. This could happen if the PDF file was not cropped properly, or if the uncropped PDF file was used. If the cause of the problem is not obvious, just add the `-trim` option to `convert`: that will crop the resulting PNG file. – Vincent Zoonekynd Feb 13 '13 at 11:56
  • 1) Invalid Parameter - 400 Warning message: running command 'convert -density 400 -trim C:\...\latex_12e0593f133a_cropped.pdf -resize 50% C:\...\latex_12e0593f133a.png' had status 4 2) -trim worked perfectly. The result will be as expected, if I overcome problem 1 – gd047 Feb 13 '13 at 12:15
  • 1
    There might be a completely unrelated `convert` command, that appears before ImageMagick in your PATH: try to specify the full path to the executable. – Vincent Zoonekynd Feb 13 '13 at 12:40
  • I'm sure you're the best person to provide an answer to this question: http://stackoverflow.com/questions/14870400/different-results-when-r-script-is-automated – gd047 Feb 14 '13 at 08:25