Hi, I need to produce a standard report for several variables in Sweave and thus would need the possibility to define a TeX-command which includes R-code like \newcommand{\meansd}[1]{The mean is \Sexpr{mean(#1)} and the standard deviation is \Sexpr{sd(#1)} . } and then just write \meansd{age} in the latex code to get the whole sentence. The above does not work, since Sweave ignores the \newcommand and does not expand the \statistics Is there an alternative way to achieve this? Thanks, Martin Posch Department of Medical Statistics Medical University of Vienna Schwarzspanierstr. 17, A-1090 Vienna Tel.: +43-1-4277-63205
Here are a couple of ideas (not tested). Neither is as succinct as what you had in mind. \newcommand{\meansd}{The mean is \Sexpr{mean(tmpvar)} and the standard deviation is \Sexpr{sd(tmpvar)} . <<echo=FALSE,results=hide>>tmpvar <- age @ \meansd <<echo=FALSE,results=hide>>tmpvar <- weight @ \meansd <<echo=FALSE,results=hide>>tmpvar <- height @ \meansd ###### or perhaps ######## <<echo=FALSE,results=hide>>meansd <- function(x) cat('The mean is ',mean(x),' and the standard deviation is ',sd(x),'.',sep='') @ <<results=tex>>meansd(age) @ <<results=tex>>meansd(weight) @ <<results=tex>>meansd(height) @ -Don At 11:54 AM +0200 6/25/04, Martin Posch wrote:>Hi, > >I need to produce a standard report for several variables in Sweave >and thus would need the possibility to define a TeX-command which >includes R-code like > >\newcommand{\meansd}[1]{The mean is \Sexpr{mean(#1)} and the >standard deviation is \Sexpr{sd(#1)} . > } >and then just write > >\meansd{age} > >in the latex code to get the whole sentence. > > >The above does not work, since Sweave ignores the \newcommand and >does not expand the \statistics > >Is there an alternative way to achieve this? > >Thanks, >Martin Posch > > > >Department of Medical Statistics >Medical University of Vienna >Schwarzspanierstr. 17, A-1090 Vienna >Tel.: +43-1-4277-63205 > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html-- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA
I do something like <<echo=f>>needed <- function(var){ a <- mean(var) b <- sd(var) c <- paste("The mean is ",a," and the var is ",b,collapse="") return(c) } @ \Sexpr{needed(something)} Wich is to do the macro in R instead of LaTeX2e Steen Ladelund -----Oprindelig meddelelse----- Fra: Martin Posch [mailto:Martin.Posch at univie.ac.at] Sendt: 25. juni 2004 11:55 Til: r-help at stat.math.ethz.ch Emne: [R] Sweave: R code in self defined TeX-commands Hi, I need to produce a standard report for several variables in Sweave and thus would need the possibility to define a TeX-command which includes R-code like \newcommand{\meansd}[1]{The mean is \Sexpr{mean(#1)} and the standard deviation is \Sexpr{sd(#1)} . } and then just write \meansd{age} in the latex code to get the whole sentence. The above does not work, since Sweave ignores the \newcommand and does not expand the \statistics Is there an alternative way to achieve this? Thanks, Martin Posch Department of Medical Statistics Medical University of Vienna Schwarzspanierstr. 17, A-1090 Vienna Tel.: +43-1-4277-63205 ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html