8

I would like to create a tex table using xtable(). Here's my minimal example which worked when I used the same R version on Win 7.

\documentclass[a4paper,12pt,twoside]{article}

\begin{document}

<<load-packages,include=TRUE,echo=TRUE>>=
library(xtable)
@

<<testing-xtable,echo=TRUE,cache=FALSE,include=TRUE>>=
tab <- matrix(1:50,nrow=10)
rownames(tab) <- letters[1:10]

print(
  xtable(
    x=tab,
    caption="A table",
    label="tab",
    align=rep("c",times=6),
    digits=3,
    display=rep("f",times=6)
    ), 
  sanitize.colnames.function=identity,
  include.rownames=FALSE,
  table.placement="h"
  )
@
\end{document}

Instead of a nice table I get the verbatim code output of xlatex() in the pdf document.

Here's the output of knitr:

> grDevices::pdf.options(useDingbats = FALSE); require(knitr); opts_knit$set(concordance     = TRUE); knit('xtable.Rnw', encoding='UTF-8')
Loading required package: knitr


processing file: xtable.Rnw
  |.............                                                    |  20%
  ordinary text without R code

  |..........................                                       |  40%
label: load-packages (with options) 
List of 2
 $ include: logi TRUE
 $ echo   : logi TRUE

  |.......................................                          |  60%
  ordinary text without R code

  |....................................................             |  80%
label: testing-xtable (with options) 
List of 3
 $ echo   : logi TRUE
 $ cache  : logi FALSE
 $ include: logi TRUE

  |.................................................................| 100%
  ordinary text without R code


output file: xtable.tex

[1] "xtable.tex"
> 
> 
Running pdflatex on xtable.tex...completed

Created PDF:             ~/Dropbox/intern/sandbox(coding)/tex/chapter/chapter/sandbox/xtable/xtable.pdf

Issues: 2 badboxes

The code chunk testing-xtable should be echoed in the final document but it isn't. This is the pdf output. I'm suspicious about the message Ordinary text without R code. Is this normal?

Any help would be greatly appreciated.

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
Gerome Bochmann
  • 496
  • 6
  • 19
  • Strange. Could you try writing `cache=FALSE` in the options of your first code chunk? What is nhtm.results.tab? – Brandon Bertelsen Jan 31 '14 at 14:49
  • 2
    Have you installed the `xtable` package on your Ubuntu machine? `require` wont throw an error for missing packages like `library` does. – James Jan 31 '14 at 15:11
  • Oh, my fault. I did `chache=FALSE` and changed `nhtm.results.tab` to `tab`. After that I fixed two other error messages and all of a sudden the error message disappeared. However, now the output is verbatim output of `xtable()` even if `echo=FALSE`. What makes it even more strange now is that the original document where I used this code now works flawlessly and I haven't changed a single bit of the code chunks o.O @BrandonBertelsen – Gerome Bochmann Jan 31 '14 at 15:13
  • @James Yes, it's installed and also up to date. – Gerome Bochmann Jan 31 '14 at 15:14
  • It sounds like "something" is getting cached and causing some strange behaviour. – Brandon Bertelsen Jan 31 '14 at 15:46
  • Hm, deleting the knitr cache doesn't change anything. – Gerome Bochmann Jan 31 '14 at 17:25
  • As @James said, use `library()` instead of `require()`; the latter is abused by too many R users. For debugging purposes, remove `include=FALSE` in the first chunk to see what is really going on there. I'm almost sure that `library(xtable)` just gives you an error that `xtable` is not found. – Yihui Xie Feb 07 '14 at 02:27
  • I used `library()` and `include=TRUE`, @Yihui. No error messages, though. The ouput of the chunk `testing-table` is still only the original output from `xtable()` i.e. the table as LaTeX-code with double `%` in front of every line. I deleted everything in the folder except the .Rnw file. This led to an error message saying that LaTeX package "framed" was missing. To me, this hints at a broken texlive-, R- or knitr installation. Since knitr is currently working fine in my original document, I'll delete this post and try a reinstall of all the components once I'm done with this project. – Gerome Bochmann Feb 07 '14 at 15:23
  • Can you show the full LaTeX output of the Rnw document after you (1) change `require()` to `library()` in the first chunk (2) remove `cache=TRUE` in the second chunk? The missing `framed` package is not big deal, although you can `sudo apt-get install texlive-latex-extra` – Yihui Xie Feb 07 '14 at 20:13
  • @Yihui, I did as you requested. Even though I'm not sure if setting an option to `FALSE` is the same removal and I'm also not sure if you meant this by output. I also added a link to the pdf. Unfortunately, this has started happening to some of my other (more important) tables as well. However, I can still paste those manually into the file, so everything is cool. It's just less luxury ;) – Gerome Bochmann Feb 13 '14 at 11:21

1 Answers1

10

Use the chunk option results='asis'.

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419