I am writing a simple nested function in R which returns a numeric output. I would like to add text to this returned output and limit the results to one decimal place. Something like this:
FirstFun <- function(x,z,h,g) {
result1 <- (x + z)/(1-(0.2*(h/g))-(0.8*(h/g)^2))
return(paste("Your answer = ", result1, "inches"), nsmall = 1)
}
FirstFun(13,387,1728,1980)
When using nsmall = 1
I get this error message:
Error in return(paste("Your answer = ", result1, "inches"), nsmall = 1) :
multi-argument returns are not permitted
When I delete nsmall = 1
it works fine:
> FirstFun(13,387,1728,1980)
[1] "Your answer = 1850.71887427348 inches"
How can I limit my output to one decimal place?
I'm also running into a problem when I try to nest the output of the first function into a second function:
SecondFun <- function(result1,h,g) {
result2 <- result1*(1-(0.2*(h/g))-(0.8*(h/g)^2))
return(paste("New answer = "), result2, "inches")
}
SecondFun(FirstFun(13,387,1728,1980),1728,1980)
But the error message I get here is:
Error in result1 * (1 - (0.2 * (g/h)) - (0.8 * (g/h)^2)) :
non-numeric argument to binary operator
My code worked fine yesterday when the only output was numeric, but now I want to include text along with my outputs and I'm running into problems. Any help is greatly appreciated!