13

Is it possible to change the default values of formal parameters in an R function at runtime?

Let's assume, we have the function

f <- function(x=1) { 
    ...
}

can I somehow change the default value of x from 1 to, say, 2?


Thanks in advance,
Sven

Sven Hager
  • 3,144
  • 4
  • 24
  • 32
  • An interesting question, but it sounds a bit dangerous. Why would you want to do that? – csgillespie Apr 18 '12 at 15:05
  • I am trying to implement a VM for R, therefore I need to know whether function signatures can be assumed to be immutable. – Sven Hager Apr 18 '12 at 15:07
  • http://stackoverflow.com/questions/9895811/can-i-tell-the-r-plyr-package-to-work-in-parallel-by-default/9895903#9895903 – GSee Apr 19 '12 at 02:57

4 Answers4

7

UPDATE: 2020-12-13

This method is no longer available

Yes, the Defaults package allows you to do this.

not2qubit
  • 14,531
  • 8
  • 95
  • 135
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • 4
    I found another way to accomplish this task: formals(f) = pairlist(x=1) – Sven Hager Apr 18 '12 at 16:07
  • 3
    @SvenHager: that's essentially what Defaults does, and you might want to use `alist` instead of `list` or `pairlist`. – Joshua Ulrich Apr 18 '12 at 16:25
  • Cool. I might use this to change paste( , sep=" ") to paste( , sep=""). (Yes, I know about paste0 in R 2.15.0) – Kevin Wright Apr 18 '12 at 18:24
  • 4
    @KevinWright: be careful doing that with widely-used support functions like `paste`. You may break things that will be difficult to diagnose. – Joshua Ulrich Apr 18 '12 at 18:34
  • 1
    It looks like `Defaults` is no longer available from CRAN. You can still do this using the `formals` function, see: http://stackoverflow.com/questions/9895811/can-i-tell-the-r-plyr-package-to-work-in-parallel-by-default/9895903#9895903 – Jake Fisher Feb 10 '16 at 18:16
2

An alternative (shown in a different SO post) is to use the formals function, e.g.:

formals(f) <- 2

Community
  • 1
  • 1
Jake Fisher
  • 3,220
  • 3
  • 26
  • 39
1

As the Defaults package is no longer available from CRAN, you can use default.

As an example:

x <- list(a = 1, b = 2, c = 3)
default::default(unlist) <- list(use.names = FALSE)
unlist(x)
#> [1] 1 2 3

unlist <- default::reset_default(unlist)
unlist(x)
#> a b c 
#> 1 2 3

Created on 2019-03-22 by the reprex package (v0.2.0.9000).

double-beep
  • 5,031
  • 17
  • 33
  • 41
1

I tried to do the same argument wrapping for the packagefinder library which has an alias of fp() pointing to findPackage(). I tried all sorts of methods, including using formals(), but in the end, the only thing that worked for me were the following 3 variations:

#--------------------------------------
# packagefinder
#--------------------------------------
# fp = findPackage
# Set default to use: 
#   fp(... , display = "console", return.df = TRUE)
#--------------------------------------
fp <- function(...) {
  packagefinder::fp(..., display="console", return.df=TRUE)
}

fp <- function(...) invisible(findPackage(..., display="console", return.df=TRUE))
fp <- function(..., display="console", return.df=TRUE) packagefinder::fp(...,display=display, return.df=return.df)

The formals() method I could not get to work.

# Fail-1
formals(fp) <- alist(... = , display="console", return.df=TRUE)

# Fail-2
MY_ARGS <- list(display="console", return.df=TRUE)
formals(fp)[names(MY_ARGS)] <- MY_ARGS

Other related posts on this:

not2qubit
  • 14,531
  • 8
  • 95
  • 135