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)

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 his blog: "R" you ready?.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



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.