Setting Function Defaults R on a Project Specific Basis

Setting Function Defaults R on a Project Specific Basis

The Defaults package used to do that; retired in 2014.

Change default arguments of an R function at runtime

UPDATE: 2020-12-13

This method is no longer available

Yes, the Defaults package allows you to do this.

Changing defaults in a function inside a locked package

I think the colloquial way to achieve what you want is via option and packages in fact do so, e.g., lattice (although they use special options) or ascii.

Furthermore, this is also done so in base R, e.g., the famous and notorious default for stringsAsFactors.

If you look at ?read.table or ?data.frame you get: stringsAsFactors = default.stringsAsFactors(). Inspecting this reveals:

> default.stringsAsFactors
function ()
{
val <- getOption("stringsAsFactors")
if (is.null(val))
val <- TRUE
if (!is.logical(val) || is.na(val) || length(val) != 1L)
stop("options(\"stringsAsFactors\") not set to TRUE or FALSE")
val
}
<bytecode: 0x000000000b068478>
<environment: namespace:base>

The relevant part here is getOption("stringsAsFactors") which produces:

> getOption("stringsAsFactors")
[1] TRUE

Changing it is achieved like this:

> options(stringsAsFactors = FALSE)
> getOption("stringsAsFactors")
[1] FALSE

To do what you want your package would need to set an option, and the function take it's values form the options. Another function could then change the options:

options(foo=c("A", "B", "C"))

default <- function(x=getOption("foo")){
x
}
default()

change.default <- function(x){
options(foo=x)
}

change.default(1:3)
default()

If you want your package to set the options when loaded, you need to create a .onAttach or .onLoad function in zzz.R. My afex package e.g., does this and changes the default contrasts. In your case it could look like the following:

.onAttach <- function(libname, pkgname) {
options(foo=c("A", "B", "C"))
}

ascii does it via .onLoad (I don't remember what is the exact difference, but Writing R Extensions will help).

Change a function's default argument for all calls within a scope

You can change the defaults of a function:

mydnorm <- dnorm
formals(mydnorm)$mean <- 2
> mydnorm
function (x, mean = 2, sd = 1, log = FALSE)
.External(C_dnorm, x, mean, sd, log)
<environment: namespace:stats>

So using your list:

T_par<-list(mean=7,sd=10)
mydnorm <- dnorm
formals(mydnorm)[names(T_par)] <- T_par
mydnorm
> mydnorm
function (x, mean = 7, sd = 10, log = FALSE)
.External(C_dnorm, x, mean, sd, log)
<environment: namespace:stats>

How to rewrite a function's default argument in R?

Like this:

paste <- function(..., sep="", collapse=NULL) base::paste(...,sep=sep, collapse=collapse)

But for this there is already a function paste0.

Not also that if paste is called using the base namespace, it will use the default version.

Correct way to specifiy optional arguments in R functions

You could also use missing() to test whether or not the argument y was supplied:

fooBar <- function(x,y){
if(missing(y)) {
x
} else {
x + y
}
}

fooBar(3,1.5)
# [1] 4.5
fooBar(3)
# [1] 3

R: 2 functions with the same name in 2 different packages

You have probably already noticed that the order of loading the packages makes a difference, i.e. the package that gets loaded last will mask the functions in packages loaded earlier.

To specify the package that you want to use, the syntax is:

chron::is.weekend()
tseries::is.weekend()

In other words, use packagename::functionname()

In addition, if you know that you will always want to use the function in chron, you can define your own function as follows:

is.weekend <- chron::is.weekend    #EDIT

Where in R do I permanently store my custom functions?

Yes, create a package. There are numerous tutorials as well as the Writing R Extensions manual that came with your copy of R.

It may seem like too much work at first, but you will probably be glad that you did this in the longer run.

PS And you can then load that package from ~/.Rprofile. For really short code, you can also define it there.



Related Topics



Leave a reply



Submit