2

I want to take user input, call a function using that input, then print the actual arguments to the screen.

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

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

     summary.lm(fits)$call

}

The output of this code is:

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

What I want is:

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

1 Answer 1

1

With some regex tinkering on the 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)
}
Sign up to request clarification or add additional context in comments.

1 Comment

Great function, does exactly what I wanted on its own. However, I have it nested within another function and when I call to fitfun from within that function it will still print "dataset" as "dataset." With a little tweaking to my parent function, using your function, I was able to get outcome and predictor to print with their actual values. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.