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!