Skip to content

assigning multiple outputs

3 messages · Sachinthaka Abeywardana, PIKAL Petr, Joshua Wiley

#
Hi
I get an error
Error: unexpected '{' in "f<-function{"

and I wonder what do you want. 

f<- function(a=1, b=1) { list(a,b)}

f()
[[1]]
[1] 1

[[2]]
[1] 1

f(2,2)
[[1]]
[1] 2

[[2]]
[1] 2

If you do not want this so please send code example with desired output 
resul.

Regards
Petr
this
http://www.R-project.org/posting-guide.html
#
Hi Sachin,

What you have probably is the best way.  In the vast majority of
cases, functions have no business doing assignment outside of
themselves.  If you find you have a function producing such disparate
output that it makes no sense for all the output to be stored
together, it might be better to create two separate functions.

Remember, functions usually return values, not make changes to the
environment.  Consider:

a <- "an object containing valuable results, that potentially took a
very long time to compute"

f <- function() {
  x <- 1
  assign("a", x, envir = .GlobalEnv)
}

results <- f()

results
a ## what happened to my data?!?!?!

Sincerely,

Josh

P.S. but I showed you the way, if you still think its a good idea in
your case, assign() will do it.

On Fri, Nov 18, 2011 at 12:01 AM, Sachinthaka Abeywardana
<sachin.abeywardana at gmail.com> wrote: