3

I already searched in different forums but until now I couldn’t find a solution. So I hope that you can help me…

Like mentioned in the title there is no increment of my variable x in the code chunk which is wrapped by a \whiledo loop. Let’s have a look at my code:

    \documentclass[10pt, a4paper]{article}
    \usepackage{ifthen}
    \usepackage{pifont}

    \begin{document}
    \SweaveOpts{concordance=TRUE}

    \newcounter{ctra}
    \setcounter{ctra}{1}
    \whiledo {\value{ctra} < 7}
    {

         <<echo=FALSE, results=hide>>=

         if(!exists("x")){x=numeric()}

         if(length(x)==0)
         {
         x=2
         }else{
         x=x+1
         }
         @

    \Sexpr{x} \\
    \stepcounter{ctra}
    }
    \end{document}

When I now compile the script in RStudio I get a pdf with a bunch of 2 but I expected a numerical series like 2, 3, 4, 5, 6 and 7.

Can anybody give me a hint to solve that problem? And please explain the background if necessary, because I’m not very experienced in Sweave.

Here I edit my question for the first time…

This numerical series I need to get the right data into my table. The code of two table rows will be something like that

    \setlength{\arrayrulewidth}{1pt}
    \begin{tabular}{|p{3.5cm}*{5}{p{1.75cm}}|}
    \hline
    \textbf{Stab} & \textbf{\ctab Spr \Sexpr{vers[1]}} & \textbf{\ctab Spr \Sexpr{vers[2]}} & \textbf{\ctab Spr \Sexpr{vers[3]}} & \textbf{\ctab Spr \Sexpr{vers[4]}} & \textbf{\ctab Spr \Sexpr{vers[5]}} \\
    \hline
    \rowcolor[gray]{0.7} 
    \hspace{0.5cm}Dampf [-] & \ctab \Sexpr{Drsp[[1]][vers[1]]} & \ctab \Sexpr{Drsp[[1]][vers[2]]} & \ctab \Sexpr{Drsp[[1]][vers[3]]} & \ctab \Sexpr{Drsp[[1]][vers[4]]} & \ctab    \Sexpr{Drsp[[1]][vers[5]]} \\
    \hspace{0.5cm}MaUbschw [bar] & \ctab \Sexpr{Drsp[[2]][ vers[1]]} & \ctab \Sexpr{Drsp[[2]][vers[2]]} & \ctab \Sexpr{Drsp[[2]][vers[3]]} & \ctab \Sexpr{Drsp[[2]][vers[4]]} & \ctab \Sexpr{Drsp[[2]][vers[5]]} \\
    % and so on...
    \hline
    \end{tabular}

But my above loaded data – in the script - has a high quantity of rows – up to 200 rows. The data file is comparable with an R data.frame.

One row in my data will be one column in my table.

This is too much to put it just in one table. My solution is to put a loop over the table and create a table with only 5 columns. In the pdf I will have some tables one below the other.

Therefore I make a vector with the quantity of rows.

    vers=c(1:nrow(Drsp)) # R-code

In the first loop the first elements of the vector will be taken to fill the table with 5 columns. Because I have 5 columns I can “shorten” the vector before we will go in the second loop.

    vers=vers[-c(1:5)] # R-code

For the second table I have the right data in the first 5 elements of the vector.

I think now it’s time to have a look at my compound code:

    \documentclass[10pt, a4paper]{article}
    \usepackage{ifthen}
    \usepackage{pifont}

    % Centering in the table
    \newcommand{\ctab}{\centering\arraybackslash}

    \begin{document}
    \SweaveOpts{concordance=TRUE}

    <<echo=FALSE, results=hide>>=

    # Load the required data
    Drsp=read.csv2(file="F:\\Testfile.csv”,header=TRUE)  

    # Create the vector
    vers=c(1:nrow(Drsp))
    @


    \newcounter{ctra}
    \setcounter{ctra}{1}
    \whiledo {\value{ctra} < \Sexpr{nrow(Drsp)}}
    {

    <<echo=FALSE, results=hide>>=

    if(!exists("x")){x=numeric()}

    if(x>=1){vers=vers[-c(1:5)]} # shorten my vector in every loop

    if(length(x)==0)
    {
      x=1
    }else{
      x=x+1
    }
    @


    % table with the right data, because every loop the vector will be shorten
    \setlength{\arrayrulewidth}{1pt}
    \begin{tabular}{|p{3.5cm}*{5}{p{1.75cm}}|}
    \hline
    \textbf{Stab} & \textbf{\ctab Spr \Sexpr{vers[1]}} & \textbf{\ctab Spr \Sexpr{vers[2]}} & \textbf{\ctab Spr \Sexpr{vers[3]}} & \textbf{\ctab Spr \Sexpr{vers[4]}} & \textbf{\ctab Spr \Sexpr{vers[5]}} \\
    \hline
    \rowcolor[gray]{0.7} 
    \hspace{0.5cm}Dampf [-] & \ctab \Sexpr{Drsp[[1]][vers[1]]} & \ctab \Sexpr{Drsp[[1]][ vers[2]]} & \ctab \Sexpr{Drsp[[1]][ vers[3]]} & \ctab \Sexpr{Drsp[[1]][ vers[4]]} & \ctab    \Sexpr{Drsp[[1]][ vers[5]]} \\
    \hspace{0.5cm}MaUbschw [bar] & \ctab \Sexpr{Drsp[[2]][ vers[1]]} & \ctab \Sexpr{Drsp[[2]][ vers[2]]} & \ctab \Sexpr{Drsp[[2]][ vers[3]]} & \ctab \Sexpr{Drsp[[2]][ vers[4]]} & \ctab \Sexpr{Drsp[[2]][ vers[5]]} \\
    % and so on...         
    \hline
    \end{tabular}

    \addtocounter{ctra}{5}  % because of the 5 table columns


    }  % End whiledo loop
    \end{document}

Okay, I hope I have done everything right…

If there is a easier way to reach my goal please let me know;-)

Many thanks in advance!

2

Sweave processes the source file by ignoring all LaTeX code and executing the R code. This means that code in a LaTeX loop will be executed only once by R. In the generated .tex code any \Sexpr variables are substituted by the value after the single run, in this case \Sexpr{x} is replaced by the string 2. When running LaTeX afterwards, the loop is executed and the string 2 is printed several times.

As an alternative you can put the loop in R and write the desired LaTeX code from there. MWE (save as .Snw, run from within R Sweave("yourfile.Snw"), then compile the generated yourfile.tex):

Edit: new Snw file, same idea, a bit more complex code

Adjust to taste. Convert \ into \\ and put a \n where you want a newline.

\documentclass{article}
\begin{document}
<<results=tex,echo=FALSE>>=

lines_per_table <- 5
cols_per_table <- 4

Drsp <- matrix(rexp(100, rate=.1), ncol=20)
vers <- c(1:ncol(Drsp))

for(x in seq(0,ncol(Drsp),lines_per_table)){
   cat("\\begin{tabular}{l|ccccc|}\n")
   cat("\\hline\n")
   cat("\\textbf{Stab} ")
   for(y in seq(cols_per_table)){
      cat("& Spr",vers[y])
   }
   cat("\\\\\n")
   cat("\\hline\n")
   for(y in seq(lines_per_table)){
      cat("line",y)
      for(z in seq(cols_per_table)){
      cat("&",Drsp[y,vers[z]])
   }
   cat("\\\\\n")
   }
   cat("\\hline\n")
   cat("\\end{tabular}\n\n")
   vers <- vers[-c(1:cols_per_table)]  
}
@
\end{document}

Result:

enter image description here

etc.

End edit

yourfile.Snw

\documentclass{article}
\begin{document}
\noindent
<<results=tex,echo=FALSE>>=
for(x in seq(7)){
   cat("x is \\textbf{",x,"}\\\\",sep="")
}
@
\end{document}

Mind the results=tex option.

Auto-generated yourfile.tex

\documentclass{article}
\usepackage{Sweave}
\begin{document}
\noindent
x is \textbf{1}\\x is \textbf{2}\\x is \textbf{3}\\x is \textbf{4}\\x is \textbf{5}\\x is \textbf{6}\\x is \textbf{7}\\\end{document}

Result:

x is 1

x is 2

x is 3

x is 4

x is 5

x is 6

x is 7

  • Thanks a lot for your answer! I think I have to explain may purpose a little more.. – tueftla Jun 14 '16 at 12:35
  • That looks and works perfect! Thanks a lot! I had to play a little to get my desired format and get my data in the tables… – tueftla Jun 15 '16 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.