R: Building functions – using default settings that can be modified via the dot-dot-dot / three point argument

April 7, 2009
By

[This article was first published on "R" you ready?, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Before you read this post, please have a look at Enrique’s comment below. He pointed out that the built-in R function modifyList() already does what I wanted to describe in this post. Well, I live to learn 🙂

I was wondering how I could write a function that uses default settings but accepts a list to overwrite the default settings via the dot-dot-dot / three-point argument. Here comes my solution.

# building a function with a list of default settings
# that can be modified by an optional list passed
# via the dot-dot-dot / three point argument

###############################################################

myFunction <- function(...)
{
  print(hasArg(settings))
}

myFunction()
myFunction(settings=list(par1=8))

###############################################################

# now I define a default setting list

myFunction <- function(...)
{
  print(hasArg(settings))
  # define default settings
  settings = list(par1=10, par2=12)
  print(settings)
}

myFunction()
myFunction(settings=list(par1=8))

###############################################################

# as a next step I replace all the elements passed on to
# settings via the dot-dot-dot argument

myFunction <- function(...)
{
  # default settings
  settings = list(par1=10, par2=12)       
  # if settings argument is used
  if(hasArg(settings)){
      suppliedSettings <- list(...)$settings
      matching <- intersect(names(settings),
                            names(suppliedSettings))
      settings[matching] <- suppliedSettings[matching]
  }

  # function operations
  print(settings)
}

myFunction()
myFunction(settings=list(par1=8))

# Now the settings have changed.

###############################################################

# If now some settings are supplied that do not match they
# are simply not considered

myFunction(settings=list(par1=8, parX=6))
# For convenience I add a line to warn the user in case of not
# matching parameters

myFunction <- function(...)
{
  # default settings
  settings = list(par1=10, par2=12)
  # if settings is supplied
  if(hasArg(settings)){
      suppliedSettings <- list(...)$settings
      matching <- intersect(names(settings),
                            names(suppliedSettings))
      settings[matching] <- suppliedSettings[matching]
      notMatching <- setdiff(names(suppliedSettings),
                             names(settings))
      if(length(notMatching)!=0) warning(paste("The
            following arguments are ignored: ", notMatching))
  }

  # function operations
  print(settings)
}

###############################################################

myFunction(settings=list(par1=8, parX=6))
# now the user is warned when some arguments do not match

by Mark Heckmann, April, 2009

To leave a comment for the author, please follow the link and comment on their blog: "R" you ready?.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , ,

Comments are closed.

Search R-bloggers

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)