Q:

Select Output in User Defined Function in R

I have written a function that produces as output 2 matrices, say A & B, and I have used list() in order to separate them in my output.  Now I would like to re-write my function so that the displayed output is ONLY matrix B unless I specify it when calling the function (however, my function has still to compute both matrices.)  Basically, I would like to hide matrix A from the output unless I say otherwise. 
Can I do this in R? 

A:

Yes. 
Here's an example:
myfun <- function(a, b, Bonly=TRUE) {
  # calculations
  result <- list(a, b)
  if (Bonly) return(result[2]) else return(result)
}

Basically you set a variable that has a default in the function with the notation x=DEFAULT in the set of arguments passed to the function.  The variable does not need to be specified for the function to run.  If the variable has the default value then return just B, otherwise return both. 
> myfun(1,2)
[[1]]
[1] 2

> myfun(1,2, FALSE)
[[1]]
[1] 1

[[2]]
[1] 2

