LyXincluding a Sweave/R code chunk in a LyX table cell

Information and discussion about LyX, a WYSIWYM editor, available for Linux, Windows and Mac OS X systems.
Post Reply
rrodriguezebiotic
Posts: 2
Joined: Sat May 08, 2010 9:23 pm

including a Sweave/R code chunk in a LyX table cell

Post by rrodriguezebiotic »

Hi all,

I've successfully installed R 2.11.0 and LyX 1.6.5 in a MacBook Pro running Mac OS X 10.5.8. Some examples including more or less complex R code chunks run without a glitch.

But I am not able to include a R code chuck within a table.

This works:

% Preview source code for paragraph 10

<<Normal1,echo=T>>=
qs=1
@

But this doesn't work:

% Preview source code for paragraph 11

\begin{tabular}{|c|}
\hline
<<Normal1,echo=T>>=
pot=2
@
\tabularnewline
\hline
\end{tabular}

The LyX literate errors window gives this information:

*****
Errors:

LaTeX Error: Something's wrong--perhaps a missing \item.
LaTeX Error: Something's wrong--perhaps a missing \item.
LaTeX Error: Something's wrong--perhaps a missing \item.
LaTeX Error: Something's wrong--perhaps a missing \item.

Description:

\begin{Sinput}

Try typing <return> to proceed.
If that doesn't work, type X <return> to quit.
*****

Please, where am I wrong? Is it not possible to include code chunks in LyX tables?

Thanks for your help!

Ricardo

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

including a Sweave/R code chunk in a LyX table cell

Post by gmedina »

Hi,

I have no idea of what "R" is and I do not use LyX, but perhaps this could be helpful:

Code: Select all

\documentclass{article}

\begin{document}

\begin{tabular}{|c|}
  \hline
  \verb+<<Normal1,echo=T>>=+
  \tabularnewline
  \verb+pot=2+
  \tabularnewline
  \verb+@+
  \tabularnewline
  \hline
\end{tabular}

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
rrodriguezebiotic
Posts: 2
Joined: Sat May 08, 2010 9:23 pm

including a Sweave/R code chunk in a LyX table cell

Post by rrodriguezebiotic »

Thanks for the reply!

Your proposal avoids the error, but it doesn't get the code evaluated.

A brief explanation about the environment. R is a free software environment for statistical computing and graphics. See http://www.r-project.org/. Sweave (http://www.statistik.lmu.de/~leisch/Sweave/) is a R library that delivers results of data analysis to a LaTeX document. You can include R code in a LaTeX document (code chuncks) and what you get is the result of that code processed by R instead of the code itself. There are a number of Sweave options controlling this output.

As you know, while working in LyX, you can include ERTs floatings with LaTeX code. This is a convenient way of including LaTeX code in a LyX document.

Well, while dealing with R code chunks, ERT work fine when you are in the body of the document, but it doesn't work when you are within table. This is really a problem for me as we are using extensively tables to show results of a number of calculations.

By using your proposal, LyX is able to avoid the error, but the output doesn't include the result of the code: simply pot=2 as echo option is set to True.

It seems to me that the markers, <<>>= and @, for the code chunk does work outside a table, but they don't work within it!

Any idea will be welcome!!

Thanks!
tormod.boe
Posts: 1
Joined: Tue Jul 06, 2010 10:02 am

including a Sweave/R code chunk in a LyX table cell

Post by tormod.boe »

Hi, you can use the

Code: Select all

Sexpr{}
command to include R output in a table:

Code: Select all

<<Normal1,echo=T>>=
qs=1
@
Stores value 1 in object qs (alternatively written as qs <- 1).
Using

Code: Select all

\Sexpr{qs}
(in plain tex or ERT in lyx) prints the value of qs. This can be included in a table:

Code: Select all

\begin{tabular}{|c|}
\hline 
\Sexpr{qs}
\tabularnewline
\hline
\end{tabular}
Which will print the value 1 within the table environment.

Good luck,
Tormod Bøe
Post Reply