Change default arguments of an R function at runtime
Asked Answered
E

4

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

Euryale answered 18/4, 2012 at 15:3 Comment(3)
An interesting question, but it sounds a bit dangerous. Why would you want to do that?Dracula
I am trying to implement a VM for R, therefore I need to know whether function signatures can be assumed to be immutable.Euryale
#9896311Hotze
G
7

UPDATE: 2020-12-13

This method is no longer available

Yes, the Defaults package allows you to do this.

Gigantic answered 18/4, 2012 at 15:9 Comment(5)
I found another way to accomplish this task: formals(f) = pairlist(x=1)Euryale
@SvenHager: that's essentially what Defaults does, and you might want to use alist instead of list or pairlist.Gigantic
Cool. I might use this to change paste( , sep=" ") to paste( , sep=""). (Yes, I know about paste0 in R 2.15.0)Philippopolis
@KevinWright: be careful doing that with widely-used support functions like paste. You may break things that will be difficult to diagnose.Gigantic
It looks like Defaults is no longer available from CRAN. You can still do this using the formals function, see: #9896311Magel
M
2

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

formals(f) <- 2

Magel answered 10/2, 2016 at 18:23 Comment(1)
This errors using R 4.3.2. You should set the value explicitly with formals(f)$x <- 2.Sprit
D
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).

Discriminating answered 22/3, 2019 at 14:57 Comment(0)
C
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:

Catchpenny answered 13/12, 2020 at 18:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.