data.table - Selecting Which Argument to Pass Dynamically in R -
i'm trying pass specific argument dynamically function, function has default values or arguments.
here's toy example:
library(data.table) mydat <- data.table(evildeeds=rep(c("all","lots","some","none"),4), capitalsins=rep(c("all", "kinda","not_really", "virginal"), each = 4), hellprobability=seq(1, 0, length.out = 16)) hellraiser <- function(arg1 = "all", arg2= "all "){ mydat[(evildeeds %in% arg1) & (capitalsins %in% arg2), hellprobability]} hellraiser() hellraiser(arg1 = "some") whicharg = "arg1" whichval = "some" #could not work: hellraiser(eval(paste0(whicharg, '=', whichval)))
i love way specify dynamically argument i'm calling: in other words, same result hellraiser(arg1="some")
while picking whether send arg1 or arg2 dynamically. goal able call function 1 parameter specified, , specify dynamically.
i managed result with
hellraiser(eval(parse(text=paste(whicharg, ' = \"', whichval, '\"', sep=''))))
Comments
Post a Comment