there is a better way to output the local variables of a function into the global environment. R provides a special assignment operator which comes in handy here.
myfunct <- function(x, y)
{
val1 <<- x + y
val2 <<- x - y
result <- val1 * val2
return(result)
}
please notice the extra character in the assignment operator. what this does is that the variables val1 and val2 are assigned in the global environment and at the same time you can use it to calculate the result. you just return the result from the function. you can go ahead and play around with val1 and val2 in the global environment.
Edit after few comments : I agree that in general this will not be a good feature. but you have to understand the requirements of the question. my understanding was that there is a pre-written function which is being used which already has a form. now in such a case a lot of times you would want to debug the function without wanting to change the form of the function.
so without touching what the function is returning, if you want to output an internal variable, then this feature of R comes in very handy. but i agree, before you close R for the day, make sure you have removed all instances of this operator.