Search code examples
rxtable

xtable output doesn't look like a table


I'd like to use the xtable package to make a neat table for a report.

Using the code on pg 7 of http://users.stat.umn.edu/~geyer/Sweave/foo.pdf, I get some output but it doesn't look anything like the table they get.

This is what I get:

% latex table generated in R 3.3.2 by xtable 1.8-2 package
% Sat Jul 14 20:49:29 2018
\begin{table}[tbp]
\centering
\caption{MyIris Table} 
\label{tab:one}
\begin{tabular}{rrrrr}
  \hline
 & Sepal.Length & Sepal.Width & Petal.Length & Petal.Width \\ 
  \hline
1 & 5.1 & 3.5 & 1.4 & 0.2 \\ 
  2 & 4.9 & 3.0 & 1.4 & 0.2 \\ 
  3 & 4.7 & 3.2 & 1.3 & 0.2 \\ 
  4 & 4.6 & 3.1 & 1.5 & 0.2 \\ 
  5 & 5.0 & 3.6 & 1.4 & 0.2 \\ 
  6 & 5.4 & 3.9 & 1.7 & 0.4 \\ 
   \hline
\end{tabular}
\end{table}

Thanks for any help!

# Select toy data 
iris
my_iris <- iris[1:6,1:4]

# Turn to matrix  
my_iris_mat <- as.matrix(my_iris)

# Try to make table 
<<label=tab1,echo=FALSE,results=tex>>=
library(xtable)
print(xtable(my_iris_mat, caption = "MyIris Table", label = "tab:one",
             digits = c(1, 1, 1, 1,1)), table.placement = "tbp",
      caption.placement = "top")

Solution

  • I ran your code and compiled the .tex file:

    \documentclass[12pt]{article}
    
    \begin{document}
    
    
    \begin{table}[tbp]
    \centering
    \caption{MyIris Table} 
    \label{tab:one}
    \begin{tabular}{rrrrr}
      \hline
     & Sepal.Length & Sepal.Width & Petal.Length & Petal.Width \\ 
      \hline
    1 & 5.1 & 3.5 & 1.4 & 0.2 \\ 
      2 & 4.9 & 3.0 & 1.4 & 0.2 \\ 
      3 & 4.7 & 3.2 & 1.3 & 0.2 \\ 
      4 & 4.6 & 3.1 & 1.5 & 0.2 \\ 
      5 & 5.0 & 3.6 & 1.4 & 0.2 \\ 
      6 & 5.4 & 3.9 & 1.7 & 0.4 \\ 
       \hline
    \end{tabular}
    \end{table}
    
    \end{document}
    

    I get this table:

    enter image description here

    It seems it is working.