我想获取用户输入,使用该输入调用函数,然后将实际参数打印到屏幕。
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])
答案 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)
}