返回R中嵌套函数的参数

时间:2017-07-28 19:16:51

标签: r function

我想获取用户输入,使用该输入调用函数,然后将实际参数打印到屏幕。

fitfun <- function(dataset = Auto, outcome = 1, predictor = 3) {

    fits <- lm(dataset[,outcome] ~ dataset[,predictor])

     summary.lm(fits)$call

}

此代码的输出为:

> fitfun()
lm(formula = dataset[, outcome] ~ dataset[, predictor])

我想要的是:

> fitfun()
lm(formula = Auto[, 1] ~ Auto[, 3])

1 个答案:

答案 0 :(得分:1)

summary(fits)$call文本进行一些正则表达式修改,请尝试以下操作:

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)
}