xtable 输出看起来不像 table

xtable output doesn't look like a table

我想使用 xtable 包来制作一份整洁的 table 报告。

使用 http://users.stat.umn.edu/~geyer/Sweave/foo.pdf 第 7 页上的代码,我得到了一些输出,但看起来与他们得到的 table 完全不同。

这是我得到的:

% 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}

感谢您的帮助!

# 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")

我运行你的代码并编译了.tex文件:

\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}

我明白了 table:

看来它正在工作。