Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6390983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:36:58+00:00 2026-05-25T03:36:58+00:00

I’m writing an R function, that is becoming quite big. It admit multiple choice,

  • 0

I’m writing an R function, that is becoming quite big. It admit multiple choice, and I’m organizing it like so:

myfun <- function(y, type=c("aa", "bb", "cc", "dd" ... "zz")){

   if (type == "aa") {
      do something
      - a lot of code here -
      ....
   }

   if (type == "bb") {
      do something
      - a lot of code here -
      ....
   }

   ....
}

I have two questions:

  1. Is there a better way, in order to not use the ‘if’ statement, for every choice of the parameter type?
  2. Could it be more functional to write a sub-function for every “type” choice?

If I write subfunction, it would look like this:

myfun <- function(y, type=c("aa", "bb", "cc", "dd" ... "zz")){

   if (type == "aa") result <- sub_fun_aa(y)
   if (type == "bb") result <- sub_fun_bb(y)
   if (type == "cc") result <- sub_fun_cc(y)
   if (type == "dd") result <- sub_fun_dd(y)
   ....
}

Subfunction are of course defined elsewhere (in the top of myfun, or in another way).

I hope I was clear with my question. Thanks in Advance.

– Additional info –

I’m writing a function that applies some different filters to an image (different filter = different “type” parameter). Some filters share some code (for example, “aa” and “bb” are two gaussian filters, which differs only for one line code), while others are completely different.

So I’m forced to use a lot of if statement, i.e.

 if(type == "aa" | type == "bb"){
  - do something common to aa and bb -

    if(type == "aa"){
      - do something aa-related -
    }
    if(type == "bb"){
      - do something bb-related -
    }
 }

 if(type == "cc" | type == "dd"){
  - do something common to cc and dd -

    if(type == "cc"){
      - do something cc-related -
    }
    if(type == "dd"){
      - do something dd-related -
    }
 }

if(type == "zz"){
     - do something zz-related -
}

And so on.
Furthermore, there are some if statement in the code “do something”.
I’m looking for the best way to organize my code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T03:36:59+00:00Added an answer on May 25, 2026 at 3:36 am

    Option 1

    One option is to use switch instead of multiple if statements:

    myfun <- function(y, type=c("aa", "bb", "cc", "dd" ... "zz")){
      switch(type, 
        "aa" = sub_fun_aa(y),
        "bb" = sub_fun_bb(y),
        "bb" = sub_fun_cc(y),
        "dd" = sub_fun_dd(y)
      )
    }  
    

    Option 2

    In your edited question you gave far more specific information. Here is a general design pattern that you might want to consider. The key element in this pattern is that there is not a single if in sight. I replace it with match.function, where the key idea is that the type in your function is itself a function (yes, since R supports functional programming, this is allowed).:

    sharpening <- function(x){
      paste(x, "General sharpening", sep=" - ")
    }
    
    unsharpMask <- function(x){
      y <- sharpening(x)
      #... Some specific stuff here...
      paste(y, "Unsharp mask", sep=" - ")
    }
    
    hiPass <- function(x) {
      y <- sharpening(x)
      #... Some specific stuff here...
      paste(y, "Hipass filter", sep=" - ")
    }
    
    generalMethod <- function(x, type=c(hiPass, unsharpMask, ...)){
      match.fun(type)(x)
    }
    

    And call it like this:

    > generalMethod("stuff", "unsharpMask")
    [1] "stuff - General sharpening - Unsharp mask"
    > hiPass("mystuff")
    [1] "mystuff - General sharpening - Hipass filter"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a French site that I want to parse, but am running into
I want to construct a data frame in an Rcpp function, but when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.