There are a few ways to do this in R, but typically you will need to convert your named vector to a named list using as.list
at some point.
For example using with
:
with(as.list(vec), {
fun(a, b)
})
#> a=3
#> b=5
Or do.call
do.call(fun, as.list(vec))
#> a=3
#> b=5
If you are using the function frequently and want to avoid the boilerplate, another option is to use a wrapper function to make it easy to call by just passing vec
f <- function(v) eval(as.call(c(quote(fun), as.list(v))))
f(vec)
#> a=3
#> b=5
Note that $
can't be used to subset named vectors, so your can't even do fun(a = vec$a, b = vec$b)
; you would need to do fun(a = vec['a'], b = vec['b'])
. Using $
requires a list
, a data.frame
or an environment