[R] Writing R-scripts

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Tue Feb 14 18:23:07 CET 2012


A) You should (re)read the posting guidelines, and pay particular attention to supplying reproducible examples. (Not your whole script.. just a cut down version that others can run that exhibits the problem.)

B) Perhaps you should reconsider the task you have set yourself. You seem to be writing an interpreter of sorts... R is already an interpreter. Why not write a line or two in R, calling on libraries and/or sourcing a file of your own convenience functions as needed instead of encoding your wishes in arcane "testtype" values?
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

Cem Girit <girit at comcast.net> wrote:

>Hello Jim,
>
>	Thank you. This will really help me to get the result I need.  But I
>am having an error message when I run the code although "testtype" is
>set to
>2 in the calling function to run the Dunnet test. What am I doing
>wrong?
>
>> pt<-parametric.tests(testtype=2, resp, groups, vehicle="Control",
>alpha=0.05)
>Error in clean.args(resp, group, vehicle, alpha, lmat, testtype) : 
>  unused argument(s) (testtype)
>
>library(plotrix)
>
>parametric.tests<-function(testtype, resp, group, vehicle, alpha,
>lmat){
>
>result<-do.call(deparse(substitute(testtype)), 
> clean.args(resp, group, vehicle, alpha, lmat, testtype))
>return(result)
>}
>
>if (testtype==1){
>
>#######################################################################
>
>## resp:  response variable, must be numeric and vector
>## group: group id for resp, numeric or character
>## alpha: CL 0.05 or 0.01
>
>## Bonferroni
> bonferroni.test <- function(resp, group, alpha) 
>{
>.....
>
> result <- data.frame(label=label, estimate=estimate, alpha=alpha,
>p.value=pval, significance=sig)
> return(result)
>}
>} else if (testtype==2){
>
>#######################################################################
>## resp:  response variable, must be numeric and vector
>## group: group id for resp, numeric or character
>## alpha: CL 0.05 or 0.01
>## vehicle: label for the reference group (for example, vehicle =
>"Control"
>if the lable for the ## reference group is 'Control')
>
>## Dunnett 
>dunnett.test <- function(resp, group, alpha, vehicle)
>{
>....
>
> result <- data.frame(label=label, estimate=estimate, alpha=alpha,
>lower=lower, upper=upper, p.value=pval, significance=sig)
> return(result)
>}
>}
>
>Cem
>
>-----Original Message-----
>From: Jim Lemon [mailto:jim at bitwrit.com.au] 
>Sent: Tuesday, February 14, 2012 3:32 AM
>To: Cem Girit
>Cc: r-help at r-project.org
>Subject: Re: [R] Writing R-scripts
>
>On 02/14/2012 08:59 AM, Cem Girit wrote:
>> Hello,
>>
>> 	This is my first attempt to write a script in R. The program below
>is 
>> intended to do some parametric tests on group data. There are 
>> subroutines for each type of test. The call to the
>"parametric.tests", 
>> routine sets the argument "testtype" for the test to be used. How can
>
>> I transfer the calculated values (in "result" below) in each routine 
>> to the calling parametric.tests routine?
>>
>Hi Cem,
>You may find it easier to use the do.call method and the clean.args
>function:
>
>library(plotrix)
>parametric.tests<-function(testfun,arglist) {
>  result<-do.call(deparse(substitute(testfun)),
>   clean.args(arglist,testfun))
>  return(result)
>}
>parametric.tests(mean,list(x=1:5,na.rm=TRUE,foo="?"))
>
>If the function called ("testfun") is available, and the argument
>"arglist"
>contains sufficient arguments for it to run, it will return the value
>of the
>function. The reason for using "clean.args" is in case you are passing
>a
>fixed list of arguments, all of which may not be appropriate for any of
>the
>functions that you want to pass as "testfun".
>
>Jim
>
>______________________________________________
>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.



More information about the R-help mailing list