Skip to content

How to combine xtable and minipage with Sweave ?

11 messages · Ptit_Bleu, Dieter Menne, Paul Johnson +1 more

#
Hello,

I'm trying to put a dynamic table and a dynamic graph side by side in a pdf
document using Sweave.
The data.frame used to generate the table is called rg (rg.txt):

"Date"; "Code"; "Data1"; "Data2"
"2009-03-10";"1";1958;147
"2009-03-10";"2";302;144
"2009-03-10";"3";4;141
"2009-03-10";"4";4;144
"2009-03-10";"5";217;145
"2009-03-10";"6";133;147
"2009-03-10";"7";431;144
"2009-03-10";"8";177;142
"2009-03-10";"9";146;143
"2009-03-10";"10";123;142
"2009-03-10";"11";308;143
"2009-03-10";"12";115;144
"2009-03-10";"13";146;142
"2009-03-10";"14";124;143
"2009-03-10";"15";176;142
"2009-03-10";"16";177;143

The Sweave script to test, saved as test_minipage_sweave.rnw, is the
following one (I'm a newbie in Latex and R so I pasted some commands I found
here and there) :
------------------------------------------------------------------------------------------------------

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[frenchb]{babel}
\usepackage{geometry}
\usepackage{color, pdfcolmk}
\usepackage[mediumqspace]{SIunits}


\geometry{a4paper,left=1cm,right=1cm,top=1cm,bottom=1.5cm}
\date{}

\begin{document}

\DefineVerbatimEnvironment{Sinput}{Verbatim}{formatcom = {\color[rgb]{0, 0,
0.56}}}
\DefineVerbatimEnvironment{Soutput}{Verbatim}{formatcom = {\color[rgb]{0.56,
0, 0}}}
\setkeys{Gin}{width=\textwidth}

<<echo=FALSE, results=TEX>>=

rg<-read.table(file="d:/RWork/rg.txt", sep=";", dec=".", header=T, as.is=T)
@

\begin{figure}[ht]
\begin{minipage}[b]{0.5\linewidth}
\centering
<<RsingleA,echo=F,fig=T,width=2.5,height=2.5>>=
plot(1:10)
@
\caption{First figure}
\label{fig:figure1}
\end{minipage}
\end{figure}

\hspace{0.5cm}

\begin{minipage}{0.7\textwidth}

  <<echo=FALSE, results=TEX>>=
  library(xtable)
  print(xtable(kw), include.rownames=F)
  @

\end{minipage}

\end{document}
------------------------------------------------------------------------------------------------------

The following commands :
     Sweave("D:/RWork/test_minipage_sweave.rnw")
     shell("pdflatex D:/RWork/test_minipage_sweave.tex")
give me a .pdf file containing the graph but instead of the table I have
this text, below the graph :
echo=FALSE, results=TEX = library(xtable) print(xtable(kw),
include.rownames=F) @

It means that it doesn't recognize a R command but I don't kno why. And it
is a problem with minipage because I managed to create a pdf file with only
the table.

Could someone give me a solution or at least a working example which I can
modify ?
Thanks in advance,
Have a nice week-end,
Ptit Bleu.
#
Ptit_Bleu <ptit_bleu <at> yahoo.fr> writes:
...
1) There were typos in your code. It should be

  print(xtable(rg), include.rownames=F)

and it should be results=tex, not TEX

2) <<>> must start at column 0, this is the first error

3) Check about the rules of minipage and floats. See the sequence
for the figure in the tex code; this gives the typical "Not in outer
mode" error

Dieter
#
Hello Dieter,

And thank you for the corrections.
Concerning the point 3, I'm a bit lost. Is it a problem of place to put the
table and the graph side by side (my english is quite as low as my skills in
Latex) ?
I tried with \begin{minipage}{0.45\textwidth} instead of 0.7 and I put
"//tiny" but no success.

I tried to add some \begin{table} before \begin{minipage}[b]{0.45\linewidth}
as I saw here :
http://texblog.wordpress.com/2007/08/01/placing-figurestables-side-by-side-minipage/
http://texblog.wordpress.com/2007/08/01/placing-figurestables-side-by-side-minipage/ 
Again ! LaTeX Error: Not in outer par mode.

Could you give me a last clue so that I can spend a nice week-end.
Thanks in advance,
Ptiti Bleu.

last script :
------------------------------------------------

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[frenchb]{babel}
\usepackage{geometry}
\usepackage{color, pdfcolmk}
\usepackage[mediumqspace]{SIunits}


\geometry{a4paper,left=1cm,right=1cm,top=1cm,bottom=1.5cm}
\date{}

\begin{document}

\DefineVerbatimEnvironment{Sinput}{Verbatim}{formatcom = {\color[rgb]{0, 0,
0.56}}}
\DefineVerbatimEnvironment{Soutput}{Verbatim}{formatcom = {\color[rgb]{0.56,
0, 0}}}
\setkeys{Gin}{width=\textwidth}

<<echo=FALSE, results=tex>>=
rg<-read.table(file="d:/RWork/rg.txt", sep=";", dec=".", header=T, as.is=T)
@

\begin{figure}[ht]
\begin{minipage}[b]{0.45\linewidth}
\centering
<<RsingleA,echo=F,fig=T,width=2.5,height=2.5>>=
plot(1:10)
@
\caption{First figure}
\label{fig:figure1}
\end{minipage}
\end{figure}

\hspace{0.5cm}

\begin{minipage}{0.45\textwidth}
<<echo=FALSE, results=tex>>=
library(xtable)
print(xtable(rg), include.rownames=F, size="\\tiny")
@
\end{minipage}

\end{document}
#
Ptit_Bleu <ptit_bleu <at> yahoo.fr> writes:
There is an example in the German latex FAQ

http://www.faqs.org/faqs/de-tex-faq/part6/

and I am sure there is the same in the englisch/french. For further details,
use the tex output produce by rnw, massage it (not rnw) until it works in
latex.

Detailed queries should be posted on the latex forum, because this is 
definitively not an R problem. And do not forget to boil it down to the
absolute minimum self-consistency before you post it on the 
latex forum, the people there are even more picky if the example is not
minimal than here.

Cetero censeo: on the latex forum, pruning the quoted text to the minimum
required and assuming people can handle a thread reader is a nice habit
not followed here.

Dieter
#
Thanks Dieter for the link.

In fact it may be a problem with R.
The .tex created with R for the table put \begin{table}[ht] and \end{table}\
between \begin{minipage} and \end{minipage} (see below)

If I manually change these positions, according to your link, there is no
more error ... but the table is below the graph and not side by side.

Any idea ? 
Have a nice week-end,
Ptit bleu.

File .tex created by Sweave : 

\begin{minipage}{0.45\textwidth}
% latex table generated in R 2.7.2 by xtable 1.5-4 package
% Fri Mar 13 17:00:06 2009
\begin{table}[ht]
\begin{center}
{\tiny
\begin{tabular}{lrr}
  \hline
Date & kWh\_m2 & Nbremesures \\
  \hline
2009-03-08 & 4.53 & 142 \\
  2009-03-09 & 1.70 & 142 \\
  2009-03-10 & 1.96 & 147 \\
   \hline
\end{tabular}
}
\end{center}
\end{table}\end{minipage}
#
On 3/13/2009 12:07 PM, Ptit_Bleu wrote:
You can use \includegraphics explicitly yourself, and avoid the 
automatic code generated by Sweave.  For example,

<<testfn, fig=true, include=false>>=
curve(f, from = 1, to = 5)
@

That generates the figure, but doesn't include it; then

\begin{figure}
\begin{center}
\includegraphics{-testfn}
\caption{The function $f(x) = |x-3.5| + (x-2)^2$.\label{fig:testfn}}
\end{center}
\end{figure}

will do the include.  You need to name the chunk that creates the figure 
for this to be workable, and if you have set a prefix in your 
SweaveOpts, you need to use it in the \includegraphics call.  (Actually 
I forget if the hyphen is needed; I stripped off a prefix from the real 
example to show you this.)

Duncan Murdoch
#
On Fri, Mar 13, 2009 at 12:17 PM, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
Dear Duncan:

Thanks for the tip. This is very useful to me.

Just to clear up the filename question, I worked up an example for
testing.  For me it is necessary to include the name of the Rnw/tex
file as a prefix in the includegraphics command.  Maybe that's just
me, or Ubuntu, or TexLive2007.

Here is a working file SweaveExample.Rnw.  In order to make this work,
you see the figure names in includegraphics.

\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}

\usepackage{babel}

\begin{document}
<<testfn, fig=true, include=false>>=
curve(sin, from = 1, to = 5)
@

<<testfn2, fig=true, include=false>>=
curve(sin, from = 1, to = 5)
@

%
\begin{figure}
\caption{My Figure}
\includegraphics{SweaveExample-testfn}
\end{figure}

%%works without float too:
\includegraphics{SweaveExample-testfn2}
\end{document}
2 days later
#
Dear Duncan,

Thanks for the tips, but I haven't found a way to modify your solution so
that I can include a table and not a graph (includegraphics works for
graphs, I need an 'includetable', I think).
I found scripts like yours explaining how to include graphs into table but
this is not my case:
My problem doesn't come from the graph but from the table ('maybe' my
english is not so clear).

Do I need to convert the table into a graph (and how) ?

Sorry again for such basic questions.
Thank you for your help.
Ptit Bleu.
#
Dear Duncan,

Thanks for the tips, but I haven't found a way to modify your solution so
that I can include a table and not a graph (includegraphics works for
graphs, I need an 'includetable', I think).
I found scripts like yours explaining how to include graphs into table but
this is not my case:
My problem doesn't come from the graph but from the table ('maybe' my
english is not so clear).

Do I need to convert the table into a graph (and how) ?

Sorry again for such basic questions.
Thank you for your help.
Ptit Bleu.
#
Ptit_Bleu wrote:
Sorry, I misread your request.  (I saw the letters t, a, b, l, e and 
read "figure".)  You need xtable to make a compatible header for your 
table.  I haven't read the whole thread, but I think the problem is in 
using \begin{table}, rather than just going straight into a tabular 
environment.   I believe you can avoid this by using the argument 
"floating=FALSE" when you print the xtable.   So the code in your Sweave 
file would look something like


print(xtable(rg), include.rownames=F, size="\\tiny", floating=FALSE)


See ?print.xtable for more options if this doesn't work.

Duncan Murdoch
#
Thanks to you all (and a bit to some websites dedicated to latex),
I finally managed to have a table and a graph side-by-side !!!

I'm not sure it will interested people, but here is the script working for
me 
(before the graph was below the table only because I have an empty line
between the 2 minipages !!! It's a bit tricky, isnt'it ?):

\begin{document}

\DefineVerbatimEnvironment{Sinput}{Verbatim}{formatcom = {\color[rgb]{0, 0,
0.56}}}
\DefineVerbatimEnvironment{Soutput}{Verbatim}{formatcom = {\color[rgb]{0.56,
0, 0}}}
\setkeys{Gin}{width=\textwidth}

<<echo=FALSE, results=tex>>=
rg<-read.table(file="d:/RWork/rg.txt", sep=";", dec=".", header=T, as.is=T)
@

\begin{table}[h]
\begin{minipage}[h]{0.49\linewidth}
<<echo=FALSE, results=tex>>=
library(xtable)
print(xtable(rg), include.rownames=F, size="\\small", floating=FALSE)
@
\end{minipage}
\hspace{0.5cm}
\begin{minipage}[h]{0.49\linewidth}
<<RsingleA,echo=F,fig=T,width=2.5,height=2.5>>=
plot(1:10)
@
\end{minipage}
\end{table}
\end{document}