xtable输出看起来不像表格

时间:2018-07-14 19:19:10

标签: r xtable

我想使用xtable包为报告制作一个整洁的表。

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

这就是我得到的:

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

1 个答案:

答案 0 :(得分:0)

我运行了您的代码并编译了.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}

我得到这张桌子:

enter image description here

似乎正在运行。