[R] Function inside Sweave

Duncan Mackay dulcalma at bigpond.com
Fri Jan 10 03:18:01 CET 2014


Hi Silvano

I am not sure exactly what you want as I am not sure of the structure and
format going into xtable
but the structure after you have formed the table is apparently the same
structure.

This is what I suggested before (modified)

for(j in 1:25){ 
yourtable <- ...
xx <- xtable(yourtable)
 caption(xx) <- paste("Table", j)
 print(xx)
 if(j < 25) cat("\n\n")

}

Pre xtable
If you have different colums to tabulate I suggest you use a formula or
bquote via a list to vary the columns in a loop
and these are the variables to be tabulated. You can then use this for your
column headers in xtable /latex.

durin/Post xtable

If single line header for table and formatting not needed to be changed
everything OK

otherwise inside chunk

<<head, echo=FALSE, results=tex>>=

for(j in 1:25){
  yourtable <- ...

  hdx = c("\\begin{table}[h]\n",
         "\\caption{\\prevt{} and their equivalent \\dcode{}}%\n",
         "\\label{tab:", j "}%\n",
         "\\begin{tabular}{l|rrrrr}\n")   # adjust to suit
  cat(hdx)

  # header from col names or otherwise
  out <- names(yourtable)
  out <-
  c("",
    paste("\\multicolumn{1}{c}{",
          out,
          rep(c("} & %\n", "} \\\\"), c(length(out)-1, 1)), sep = ""), "\n"
  )
  cat(out, sep = "")
  # xtable
  xx <- xtable(yourtable)
  
  xnd = c(\\end{tabular}\n",
          "\\end{table}\n")
  cat(xnd)

  if(j < 25) cat("\n\n")
  
}
@ %% end

You can add booktabs functions within tabular by cat if needed 
If you have things that need to be changed they can be accessed by a
vector/list

If you have variable numbers of decimal points see an colour is black
http://tex.stackexchange.com/questions/96982/coloring-text-in-a-dcolumn-alig
ned-cell-of-a-table

Henrik Bengtsson has a package to do this sort of thing but he discontinued
it for ver 3 or 2.15 ca

Regards

Duncan 

-----Original Message-----
From: silvano at uel.br [mailto:silvano at uel.br] 
Sent: Friday, 10 January 2014 04:48
To: Duncan Mackay
Subject: Re: [R] Function inside Sweave

Dear Duncan,

I want relatioship two variables:  DISCIPLINA and Questions.

I have about 100 different disciplines (in same column) and 25 questions (25
differents columns).

Head is:

ALUNO DISCIPLINA FIELD1   DATA SERIE TURMA Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 
PROFESSOR Q11 Q12 Q13 Q14 Q15 Q16 Q17 Q18 Q19 Q20 Q21 Q22 Q23 Q24 Q25

Something like this:

DISCIPLINA:    "6PIV019" "6VET017" "6BIQ014" "6ZOT058" "6ZOT060"  ... 
"6PIV018"

Questions: Q1, Q2, Q3,  ... Q25


I get what I want (form a one Question) with this commands:

tab1 = table(DISCIPLINA, Q1)
tab1.prop = round(100*prop.table(tab1, 1),  2) tab1.txt = xtable(tab1.prop,
align="l|rrrrr", label='Q1', caption=c("Apresentação da proposta de programa
a ser desenvolvida na disciplina", "Q1")) print(tab1.txt,
format.args=list(big.mark = ".", decimal.mark = ","),
caption.placement='top', table.placement='H')

But, I need 25 commands like this, because I have 25 questions.


I would like program a function to make this one time only. So, I try this
general function:

DISCIPLINA <- rep(c("A", "B", "C", "D", "E"), 5) for (i in 1:25){
  aux <- paste("Q", i, sep="")
  assign(aux, sample(rep(seq(1:5),5)))
}

cria.tabela <- function(Questao){
  Questao1 <- get(Questao)
  tab1 <- table(DISCIPLINA, Questao1)
  tab1.prop = round(100*prop.table(tab1, 1),  2)
  tab1.txt = xtable(tab1.prop, align="l|rrrrr", label=Questao,
  caption=paste(Questao))
  print(tab1.txt, format.args=list(big.mark = ".", decimal.mark = ","),
  caption.placement='top', table.placement='H') }

geral <- function(){
  for (i in 1:25){
    x<-paste("Q", i, sep="")
    cria.tabela(x)
    }
}

geral()



but I need change the created variable DISCIPLINA by DISCIPLINA in my
dataset and I don't get this.

If you want, I send you my dataset.


Thanks a lot.


Silvano.












-----Mensagem Original-----
From: Duncan Mackay
Sent: Thursday, January 09, 2014 1:12 AM
To: 'Silvano Cesar da Costa'
Cc: R
Subject: RE: [R] Function inside Sweave

Hi Silvano

I cannot use the data and function as the data is not available to me

for the xtable part using data from xtable try as an example to see if it
fits your needs

data(tli)
head(tli)

for(j in 1:5){ xx <- xtable(tli[1:6,]);  caption(xx) <- paste("Table", j);
print(xx); if(j < 5) cat("\n\n")}

You can amend the xtable part to suit -- the cat command gives some spacing

Regards

Duncan


Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Silvano Cesar da Costa
Sent: Thursday, 9 January 2014 04:24
To: r-help at r-project.org
Subject: [R] Function inside Sweave

Hi,

I have a data set involving 25 Questions (Q1, Q2, ... , Q25), 100 Disciplina
and 5 series. The variables are:

ALUNO DISCIPLINA SERIE TURMA Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 ... Q25


I want to create tables associating each of the 25 questions to the
Disciplina.
Something like:

tab1 = table(DISCIPLINA, Q1)
tab1.prop = round(100*prop.table(tab1, 1),  2) tab1.txt = xtable(tab1.prop,
align="l|rrrrr", label='Q1', caption=c("Apresentação da proposta de programa
a ser desenvolvida na disciplina", "Q1")) print(tab1.txt,
format.args=list(big.mark = ".", decimal.mark = ","),
caption.placement='top', table.placement='H')



With the help of a friend, was created the following function:

require(xtable)

DISCIPLINA <- rep(c("A", "B", "C", "D", "E"), 5) for (i in 1:25){
  aux <- paste("Q", i, sep="")
  assign(aux, sample(rep(seq(1:5),5)))
}

cria.tabela <- function(Questao){
  Questao1 <- get(Questao)
  tab1 <- table(DISCIPLINA, Questao1)
  tab1.prop = round(100*prop.table(tab1, 1),  2)
  tab1.txt = xtable(tab1.prop, align="l|rrrrr", label=Questao,
  caption=paste(Questao))
  print(tab1.txt, format.args=list(big.mark = ".", decimal.mark = ","),
  caption.placement='top', table.placement='H') }

geral <- function(){
  for (i in 1:25){
    x<-paste("Q", i, sep="")
    cria.tabela(x)
    }
}

geral()


I need to change the variable DISCIPLINA created in the function by the
variable DISCIPLINA in my dataset, but not working.


Reading data was taken with:

require(foreign)
require(xtable)
dados = read.epiinfo('C:/Colegiado de Veterinária/Dados/A2013/A2013.rec')
head(dados)
tail(dados)
str(dados)
attach(dados)

serie_1 = subset(dados, SERIE=='1')
head(serie_1)
tail(serie_1)
attach(serie_1)


How can I do this?

Thanks a lot,


---------------------------------------------
Silvano Cesar da Costa

Universidade Estadual de Londrina
Centro de Ciências Exatas
Departamento de Estatística

Fone: (43) 3371-4346

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


---
Este email está limpo de vírus e malwares porque a proteção do avast!
Antivírus está ativa.
http://www.avast.com




More information about the R-help mailing list