Dear Rexperts, Given, aar <-function(command) { switch(command, {scrn = cat("scrn :Screening","\n")} {dx = cat("dx :Diagnosis","\n")} {df = cat("df :Don't Forget","\n")} ) } I want to be able to do: aar("dx") # function does cat("dx :Diagnosis","\n") aar(c("dx","df")) # function does cat("dx :Diagnosis","\n") # function does df = cat("df :Don't Forget","\n") BUT IT IS NOT WORKING FOR ME. Please help:-) -- Oscar Oscar A. Linares, MD Translational Medicine Unit LaPlaisance Bay, Bolles Harbor Monroe, Michigan [[alternative HTML version deleted]]
I think you just missed some commas out... aar <- function(command = c("scrn", "dx", "df")) { command <- match.arg(command) switch(command, scrn = cat("scrn :Screening","\n"), dx = cat("dx :Diagnosis","\n"), df = cat("df :Don't Forget","\n") ) } Colin. Ps you don't need the curly brackets here if theres only one expresion, and sometimes its good to restrict the inputs to only those you want So that aar("something wrong") # Error in match.arg(command) : 'arg' should be one of "scrn", "dx", "df" -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of oscar linares Sent: 18 November 2009 10:40 To: r-help at r-project.org Subject: [R] Switch Help Dear Rexperts, Given, aar <-function(command) { switch(command, {scrn = cat("scrn :Screening","\n")} {dx = cat("dx :Diagnosis","\n")} {df = cat("df :Don't Forget","\n")} ) } I want to be able to do: aar("dx") # function does cat("dx :Diagnosis","\n") aar(c("dx","df")) # function does cat("dx :Diagnosis","\n") # function does df = cat("df :Don't Forget","\n") BUT IT IS NOT WORKING FOR ME. Please help:-) -- Oscar Oscar A. Linares, MD Translational Medicine Unit LaPlaisance Bay, Bolles Harbor Monroe, Michigan [[alternative HTML version deleted]] ______________________________________________ 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. ______________________________________________________________________ This email has been scanned by the MessageLabs Email Security System. For more information please visit http://www.messagelabs.com/email
And if you want to do both do invisible( lapply(c("scrn","dx"), aar) ) but I think you will have to use multiple ifs rather than switch if you intend to add more functionality... . . . I think you just missed some commas out... aar <- function(command = c("scrn", "dx", "df")) { command <- match.arg(command) switch(command, scrn = cat("scrn :Screening","\n"), dx = cat("dx :Diagnosis","\n"), df = cat("df :Don't Forget","\n") ) } Colin. Ps you don't need the curly brackets here if theres only one expresion, and sometimes its good to restrict the inputs to only those you want So that aar("something wrong") # Error in match.arg(command) : 'arg' should be one of "scrn", "dx", "df" -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of oscar linares Sent: 18 November 2009 10:40 To: r-help at r-project.org Subject: [R] Switch Help Dear Rexperts, Given, aar <-function(command) { switch(command, {scrn = cat("scrn :Screening","\n")} {dx = cat("dx :Diagnosis","\n")} {df = cat("df :Don't Forget","\n")} ) } I want to be able to do: aar("dx") # function does cat("dx :Diagnosis","\n") aar(c("dx","df")) # function does cat("dx :Diagnosis","\n") # function does df = cat("df :Don't Forget","\n") BUT IT IS NOT WORKING FOR ME. Please help:-) -- Oscar Oscar A. Linares, MD Translational Medicine Unit LaPlaisance Bay, Bolles Harbor Monroe, Michigan [[alternative HTML version deleted]] ______________________________________________ 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. ______________________________________________________________________ This email has been scanned by the MessageLabs Email Security System. For more information please visit http://www.messagelabs.com/email
If you want type twice commands in function aar, you could try this: aar <-function(command) { switch(command, scrn = cat("scrn :Screening","\n"), dx = cat("dx :Diagnosis","\n"), df = cat("df :Don't Forget","\n")) } invisible(Vectorize(aar)(c('dx', 'df'))) On Wed, Nov 18, 2009 at 8:39 AM, oscar linares <winsaam at gmail.com> wrote:> Dear Rexperts, > > Given, > > aar <-function(command) { > > switch(command, > ?{scrn = cat("scrn ?:Screening","\n")} > ?{dx ? = cat("dx ? ?:Diagnosis","\n")} > ?{df ? = cat("df ? ?:Don't Forget","\n")} > ) > } > > I want to be able to do: > > aar("dx") # function does cat("dx ? ?:Diagnosis","\n") > > aar(c("dx","df")) ?# function does cat("dx ? ?:Diagnosis","\n") > ? ? ? ? ? ? ? ? ? ? ? ?# function does df ? = cat("df ? ?:Don't > Forget","\n") > > BUT IT IS NOT WORKING FOR ME. > > Please help:-) > > -- > Oscar > Oscar A. Linares, MD > Translational Medicine Unit > LaPlaisance Bay, Bolles Harbor > Monroe, Michigan > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
I believe that is what you want: aar <-function(command) { for(i in command){ cat(i,":",switch(EXPR=i, scrn = "Screening", dx = "Diagnosis", df = "Don't Forget"), "\n") } } > aar(c("dx","df")) dx : Diagnosis df : Don't Forget Alain oscar linares wrote:> Dear Rexperts, > > Given, > > aar <-function(command) { > > switch(command, > {scrn = cat("scrn :Screening","\n")} > {dx = cat("dx :Diagnosis","\n")} > {df = cat("df :Don't Forget","\n")} > ) > } > > I want to be able to do: > > aar("dx") # function does cat("dx :Diagnosis","\n") > > aar(c("dx","df")) # function does cat("dx :Diagnosis","\n") > # function does df = cat("df :Don't > Forget","\n") > > BUT IT IS NOT WORKING FOR ME. > > Please help:-) > >-- Alain Guillet Statistician and Computer Scientist SMCS - Institut de statistique - Universit? catholique de Louvain Bureau c.316 Voie du Roman Pays, 20 B-1348 Louvain-la-Neuve Belgium tel: +32 10 47 30 50