1
library(xtable)
library(magrittr)
library(moonBook)
options(ztable.type="html")
a=mytable(head(iris))
a
x = xtable(a)
x

i cannot make xtable, just use mytable... want to use xtable, but output like this.. what is problem?? i installed latex progrem.. help me i cannot find good result in googling..

     Descriptive Statistics    
-------------------------------- 
                  N     Total   
-------------------------------- 
 Sepal.Length    6   5.0 ± 0.3 
 Sepal.Width     6   3.4 ± 0.3 
 Petal.Length    6              
   - 1.3             1  (16.7%) 
   - 1.4             3  (50.0%) 
   - 1.5             1  (16.7%) 
   - 1.7             1  (16.7%) 
 Petal.Width     6              
   - 0.2             5  (83.3%) 
   - 0.4             1  (16.7%) 
 Species         6              
   - setosa         6  (100.0%) 
   - versicolor       0  (0.0%) 
   - virginica        0  (0.0%) 
-------------------------------- 
% latex table generated in R 3.6.1 by xtable 1.8-4 package
% Fri Sep 27 14:45:14 2019
\begin{table}[ht]
\centering
\begin{tabular}{rllll}
  \hline
 & name & N & stats & class \\ 
  \hline
1 & Sepal.Length   & 6 & 5.0 ± 0.3 & continuous \\ 
  2 & Sepal.Width    & 6 & 3.4 ± 0.3 & continuous \\ 
  3 & Petal.Length   & 6 &    & categorical \\ 
  4 &   - 1.3        &  & 1  (16.7\%) &  \\ 
  5 &   - 1.4        &  & 3  (50.0\%) &  \\ 
  6 &   - 1.5        &  & 1  (16.7\%) &  \\ 
  7 &   - 1.7        &  & 1  (16.7\%) &  \\ 
  8 & Petal.Width    & 6 &    & categorical \\ 
  9 &   - 0.2        &  & 5  (83.3\%) &  \\ 
  10 &   - 0.4        &  & 1  (16.7\%) &  \\ 
  11 & Species        & 6 &    & categorical \\ 
  12 &   - setosa     &  & 6  (100.0\%) &  \\ 
  13 &   - versicolor &  & 0  (0.0\%) &  \\ 
  14 &   - virginica  &  & 0  (0.0\%) &  \\ 
   \hline
\end{tabular}
\end{table}


1 Answer 1

2

xtable() is working perfectly well in your example. The output since from % latex table ... to \end{table} is the LaTeX code that make a LaTeX table, but the R output is not a LaTeX document, is only a text which only in part is a chunk of LaTeX code that is not compilable "as is".

So you have two options:

1) Copy & paste that code in a complete LaTeX (.tex) document:

\documentclass{article}
\begin{document}

% paste your LaTeX table code here

\end{document}

That you must compile with pdflatex, xelatex or lualatex (e.g.: pdflatex mytable.tex)

2) Make a Sweave (.Rnw) document, that is nearly the same LaTeX document, but instead of paste the LaTeX table, you include the R code to obtain that table, but supplying also some output options:

\documentclass{article}
\begin{document}

<<mytable,echo=FALSE,results="asis">>=
# paste your R code here
@

\end{document}

When edited with Rstudio, simply click the "Compile PDF" button to obtain the same result.

Compilable working example:

\documentclass{article}
\begin{document}
<<mytable,echo=FALSE,results='asis'>>=
library(xtable)
a <- matrix(c(1,2,3,4,5,6,7,8,9),nrow=3)
xtable(a)
@
\end{document}

mwe

2
  • An additional possibility would be a Rmarkdown document. Might be easier for people not used to LaTeX syntax. Sep 27, 2019 at 9:27
  • Thanks Fran, i did and i satisfies your pretty answer, I'm a beginner for use R, but It's good luck to meet you with this site. i `m S korean, asked the Korea site but there was no answer. and I bookmarked this site, and i will remember you Fran. thank you and you are really really good person!! Sep 28, 2019 at 0:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .