[R] Printing/Generating/Outputting a Table (Not Latex)

Henrik Bengtsson hb at biostat.ucsf.edu
Wed Dec 10 21:25:15 CET 2014


On Tue, Dec 9, 2014 at 3:11 PM, Henrik Bengtsson <hb at biostat.ucsf.edu> wrote:
> I'm surprised no one mentioned alternatives to LaTeX, which is not
> necessarily installed on all systems and it's also quite a
> heavy-weight setup (100's-1000's MBs).  An alternative is to output a
> table in Markdown or HTML and convert that to PDF.  The poor man's
> HTML-to-PDF is to manually open the HTML document in a modern web
> browser and simply "save as/print to" PDF.  For automatic conversion,
> one can use Pandoc [http://johnmacfarlane.net/pandoc/], which is quick
> to install.
>
> There are several options to output a table in Markdown or HTML from a
> data.frame, e.g. xtable, knitr, rmarkdown.  Here's how you can do it
> using R.rsp + knitr::kable:
>
> Create the following RSP-embedded Markdown file 'table.md.rsp' (- - -
> lines excluded):
> - - - BEGIN - - -
> <%
> R.utils::use("knitr")
> options(knitr.table.format="markdown")
> %>
>
> # A Table
> <%
> data <- head(datasets::iris)
> %>
>
> <%= kable(data) %>
>
> _Table: The first <%=nrow(data)%> entries of the iris dataset._
> - - - END - - -
>
> Then just run:
>
>> library("R.rsp")
>> html <- rfile("table.md.rsp")
>> !html
>
> and print/save as PDF in the browser. (*)
>
>
> If you've got Pandoc installed you can generate a PDF by making sure
> to use options(knitr.table.format="pandoc") and then:
>
>> library("R.rsp")
>> md <- rfile('table-pandoc.md.rsp', postprocess=FALSE)
>> pdf <- gsub("md$", "pdf", md)
>> system2("pandoc", args=c(normalizePath(md), "-o", pdf))
>> !pdf

I was wrong about going down the Pandoc path; Pandoc in turn relies on
LaTeX for generating PDFs so that didn't solve anything:

  "For PDF output, you’ll also need to install LaTeX"
[http://johnmacfarlane.net/pandoc/installing.html]

I've also confirmed that Pandoc fails if 'pdflatex' is not available.

If conversion to PDF from HTML can be done by other means (e.g.
manually in the browser), LaTeX can still be avoided.

Sorry about the noise.

/Henrik

>
> See also this Gist: https://gist.github.com/HenrikBengtsson/f4e8f6fe2af5d6ccbed6
>
> /Henrik
>
> (*) For the lazy, to test the above HTML example "on the fly" run:
> source("http://callr.org/rfile#https://gist.githubusercontent.com/HenrikBengtsson/f4e8f6fe2af5d6ccbed6/raw/table.md.rsp")
>
>
>
> On Tue, Dec 9, 2014 at 1:02 PM, Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:
>> On 09/12/2014 20:47, Richard M. Heiberger wrote:
>>>
>>> the last one is wrong.  That is the one for which I don't know the
>>> right answer on linux.
>>>
>>> 'xdvi' displays dvi files.  you need to display a pdf file.
>>> whatever is the right program on linux to display pdf files is what
>>> belongs there.
>>>
>>> On Macintosh we can avoid knowing by using 'open', which means use the
>>> system standard.
>>> I don't know what the linux equivalent is, either the exact program or
>>> the instruction to use the standard.
>>
>>
>> xdg-open (but like OS X it depends on having the right associations set).
>>
>>
>>>
>>> On Tue, Dec 9, 2014 at 3:36 PM, Kate Ignatius <kate.ignatius at gmail.com>
>>> wrote:
>>>>
>>>> I set these options:
>>>>
>>>> options(latexcmd='pdflatex')
>>>> options(dviExtension='pdf')
>>>> options(xdvicmd='xdvi')
>>>>
>>>> Maybe one too many?  I'm running in Linux.
>>>>
>>>>
>>>>
>>>> On Tue, Dec 9, 2014 at 3:24 PM, Richard M. Heiberger <rmh at temple.edu>
>>>> wrote:
>>>>>
>>>>> It looks like you skipped the step of setting the options.
>>>>> the latex function doesn't do pdflatex (by default it does regular
>>>>> latex) unless you set the options
>>>>> as I indicated.
>>>>>
>>>>> On Tue, Dec 9, 2014 at 3:11 PM, Kate Ignatius <kate.ignatius at gmail.com>
>>>>> wrote:
>>>>>>
>>>>>> Ah yes, you're right.
>>>>>>
>>>>>> The log has this error:
>>>>>>
>>>>>> ! LaTeX Error: Missing \begin{document}.
>>>>>>
>>>>>> Though can't really find much online on how to resolve it.
>>>>>>
>>>>>> On Tue, Dec 9, 2014 at 1:15 PM, Jeff Newmiller
>>>>>> <jdnewmil at dcn.davis.ca.us> wrote:
>>>>>>>
>>>>>>> pdflatex appears to have run, because it exited. You should look at
>>>>>>> the tex log file, the problem is more likely that the latex you sent out to
>>>>>>> pdflatex was incomplete.
>>>>>>>
>>>>>>> ---------------------------------------------------------------------------
>>>>>>> Jeff Newmiller                        The     .....       .....  Go
>>>>>>> Live...
>>>>>>> DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live
>>>>>>> Go...
>>>>>>>                                        Live:   OO#.. Dead: OO#..
>>>>>>> Playing
>>>>>>> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
>>>>>>> /Software/Embedded Controllers)               .OO#.       .OO#.
>>>>>>> rocks...1k
>>>>>>>
>>>>>>> ---------------------------------------------------------------------------
>>>>>>> Sent from my phone. Please excuse my brevity.
>>>>>>>
>>>>>>> On December 9, 2014 8:43:02 AM PST, Kate Ignatius
>>>>>>> <kate.ignatius at gmail.com> wrote:
>>>>>>>>
>>>>>>>> Thanks!  I do get several errors though when running on Linux.
>>>>>>>>
>>>>>>>> Running your code, I get this:
>>>>>>>>
>>>>>>>> Error in system(cmd, intern = TRUE, wait = TRUE) :
>>>>>>>> error in running command
>>>>>>>>
>>>>>>>> Fiddling around with the code and running this:
>>>>>>>>
>>>>>>>> tmp <- matrix(1:9,3,3)
>>>>>>>> tmp.tex <- latex(tmp, file='tmp.tex')
>>>>>>>> print.default(tmp.tex)
>>>>>>>> tmp.dvi <- dvi(tmp.tex)
>>>>>>>> tmp.dvi
>>>>>>>> tmp.tex
>>>>>>>> dvips(tmp.dvi)
>>>>>>>> dvips(tmp.tex)
>>>>>>>> library(tools)
>>>>>>>> texi2dvi(file='tmp.tex', pdf=TRUE, clean=TRUE)
>>>>>>>>
>>>>>>>> I get this:
>>>>>>>>
>>>>>>>> Error in texi2dvi(file="tmp.tex",,  :
>>>>>>>>   Running 'texi2dvi' on 'tmp.tex' failed.
>>>>>>>> Messages:
>>>>>>>> /usr/bin/texi2dvi: pdflatex exited with bad status, quitting.
>>>>>>>>
>>>>>>>> I've read that it may have something to do with the path of pdflatex.
>>>>>>>>
>>>>>>>> Sys.which('pdflatex')
>>>>>>>>
>>>>>>>>            pdflatex
>>>>>>>>
>>>>>>>> "/usr/bin/pdflatex"
>>>>>>>>
>>>>>>>>
>>>>>>>> Sys.which('texi2dvi')
>>>>>>>>
>>>>>>>>            texi2dvi
>>>>>>>>
>>>>>>>> "/usr/bin/texi2dvi"
>>>>>>>>
>>>>>>>>> file.exists(Sys.which('texi2dvi'))
>>>>>>>>
>>>>>>>>
>>>>>>>> [1] TRUE
>>>>>>>>
>>>>>>>>> file.exists(Sys.which('pdflatex'))
>>>>>>>>
>>>>>>>>
>>>>>>>> [1] TRUE
>>>>>>>>
>>>>>>>> Is there a specific path I should be giving with pdflatex and/or
>>>>>>>> 'texi2dvi to make this work?
>>>>>>>>
>>>>>>>> Thanks!
>>>>>>>>
>>>>>>>> On Mon, Dec 8, 2014 at 11:13 PM, Richard M. Heiberger
>>>>>>>> <rmh at temple.edu>
>>>>>>>> wrote:
>>>>>>>>>
>>>>>>>>> yes of course, and the answer is latex() in the Hmisc package.
>>>>>>>>> Why were you excluding it?
>>>>>>>>> Details follow
>>>>>>>>>
>>>>>>>>> Rich
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> The current release of the Hmisc package has this capability on
>>>>>>>>> Macintosh and Linux.
>>>>>>>>> For Windows, you need the next release 3.14-7 which is available now
>>>>>>>>
>>>>>>>> at github.
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> ## windows needs these lines until the new Hmisc version is on CRAN
>>>>>>>>> install.packages("devtools")
>>>>>>>>> devtools::install_github("Hmisc", "harrelfe")
>>>>>>>>>
>>>>>>>>> ## All operating systems
>>>>>>>>> options(latexcmd='pdflatex')
>>>>>>>>> options(dviExtension='pdf')
>>>>>>>>>
>>>>>>>>> ## Macintosh
>>>>>>>>> options(xdvicmd='open')
>>>>>>>>>
>>>>>>>>> ## Windows, one of the following
>>>>>>>>>
>>>>>>>>
>>>>>>>> options(xdvicmd='c:\\progra~1\\Adobe\\Reader~1.0\\Reader\\AcroRd32.exe')
>>>>>>>>>
>>>>>>>>> ## 32-bit windows
>>>>>>>>>
>>>>>>>>
>>>>>>>> options(xdvicmd='c:\\progra~2\\Adobe\\Reader~1.0\\Reader\\AcroRd32.exe')
>>>>>>>>>
>>>>>>>>> ## 64 bit windows
>>>>>>>>>
>>>>>>>>> ## Linux
>>>>>>>>> ## I don't know the xdvicmd value
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> ## this works on all R systems
>>>>>>>>> library(Hmisc)
>>>>>>>>> tmp <- matrix(1:9,3,3)
>>>>>>>>> tmp.dvi <- dvi(latex(tmp))
>>>>>>>>> print.default(tmp.dvi) ## prints filepath of the pdf file
>>>>>>>>> tmp.dvi  ## displays the pdf file on your screen
>>>>>>>>>
>>>>>>>>> On Mon, Dec 8, 2014 at 9:31 PM, Kate Ignatius
>>>>>>>>
>>>>>>>> <kate.ignatius at gmail.com> wrote:
>>>>>>>>>>
>>>>>>>>>> Hi,
>>>>>>>>>>
>>>>>>>>>> I have a simple question.  I know there are plenty of packages out
>>>>>>>>>> there that can provide code to generate a table in latex.  But I
>>>>>>>>>> was
>>>>>>>>>> wondering whether there was one out there where I can generate a
>>>>>>>>
>>>>>>>> table
>>>>>>>>>>
>>>>>>>>>> from my data (which ever way I please) then allow me to save it as
>>>>>>>>>> a
>>>>>>>>>> pdf?
>>>>>>>>>>
>>>>>>>>>> Thanks
>>>>>>>>>>
>>>>>>>>>> K.
>>>>>>>>>>
>>>>>>>>>> ______________________________________________
>>>>>>>>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>>>>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>>>>>>>> PLEASE do read the posting guide
>>>>>>>>
>>>>>>>> http://www.R-project.org/posting-guide.html
>>>>>>>>>>
>>>>>>>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>>>>>
>>>>>>>>
>>>>>>>> ______________________________________________
>>>>>>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>>>>>> PLEASE do read the posting guide
>>>>>>>> http://www.R-project.org/posting-guide.html
>>>>>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>>>>
>>>>>>>
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>
>> --
>> Brian D. Ripley,                  ripley at stats.ox.ac.uk
>> Emeritus Professor of Applied Statistics, University of Oxford
>> 1 South Parks Road, Oxford OX1 3TG, UK
>>
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list