R: pass a vector of symbols into a function instead of a long argument list -
i'm writing r package calls c code .c() function. need pass on 50 arguments .c(), bloats code , prone error. rather type
output <- .c("my_dynlib", arg2, arg3, arg4, arg5, arg6, ..., arg53)
i rather have character vector mynames
of additional arguments send .c() , like
f <- vectorize(as.symbol, "x") mysymbols <- f(mynames) output <- .c("my_dynlib", mysymbols)
but isn't quite want because mysymbols list. there way compactly pass collection of arguments function don't want rewrite?
footnote: i'm guessing of suggest passing more complicated arguments .call, .external, or rcpp, that's more overhaul want deal right now.
edit: if want mynames
point slots in s3 or s4 object?
suppose want use symbols refer class slots in data frame.
> y = data.frame(a = 1:4, b = 5:8) > mynames = paste("y@", slotnames(y), sep = "") > mysymbols = lapply(mynames, as.symbol) > str(mysymbols) list of 4 $ : symbol y@.data $ : symbol y@names $ : symbol y@row.names $ : symbol y@.s3class
i use do.call @josilber said, do.call doesn't recognize symbols
> do.call(print, mysymbols) error in (function (x, ...) : object 'y@.data' not found
even though can use symbols manually.
> y@.data [[1]] [1] 1 2 3 4 [[2]] [1] 5 6 7 8
if have list of objects want pass function, can do.call
function. in case, sounds following should work:
do.call(".c", c("my_dynlib", mysymbols))
Comments
Post a Comment