Skip to content
Prev 200628 / 398503 Next

Switch Help

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:-)