Return arguments of nested function in R -
i want take user input, call function using input, print actual arguments screen.
fitfun <- function(dataset = auto, outcome = 1, predictor = 3) { fits <- lm(dataset[,outcome] ~ dataset[,predictor]) summary.lm(fits)$call } the output of code is:
> fitfun() lm(formula = dataset[, outcome] ~ dataset[, predictor]) what want is:
> fitfun() lm(formula = auto[, 1] ~ auto[, 3])
with regex tinkering on summary(fits)$call text, try this:
fitfun <- function(dataset = mtcars, outcome = 1, predictor = 3) { fits <- lm(dataset[,outcome] ~ dataset[,predictor]) call <- paste(summary(fits)$call, collapse = " ") call <- gsub("dataset", deparse(substitute(dataset)), call) call <- gsub("outcome", outcome, call) call <- gsub("predictor", predictor, call) return(call) }
Comments
Post a Comment