Hi, Is there a way to find out if a function was called with an output argument? Or to prevent the printing of large amount of data if the function was called without output argument? Thanks YG -------------- next part -------------- An HTML attachment was scrubbed... URL: https://stat.ethz.ch/pipermail/r-help/attachments/20000608/8d7a01ec/attachment.html
On Thu, 8 Jun 2000, Yves Gauvreau wrote (after adding some line breaks):> Is there a way to find out if a function was called with an > output argument? Or to prevent the printing of large amount of data if > the function was called without output argument?and later:> I forgot to ask if it was possible when there is no output argument to > somehow "put" the data in the global environnement?I think you misunderstand. S is a functional language, so *all* functions return something (possible NULL). And if an object is evaluated at the top-level and is not part of an assignment, its result is *printed* unless the auto-print flag is false. If you don't want to see the result, either enclose the call in invisible() or assign the result to an R object, as in> 2+3[1] 5> invisible(2+3) > z <- 2+3 >(BTW, "+" really is a function in S, and I could have written "+"(2,3) to show this.) I hope I have guessed correctly what you had in mind. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> From: "Yves Gauvreau" <cyg at sympatico.ca> > Date: Fri, 9 Jun 2000 07:05:42 -0400 > > Thanks for the prompt response. > > I know nothing about S and about the fact that it is a functional language. > I don't know much about English, functional languages, stats and maths > either I'm just a retired high school teacher fiddling around to pass the > time. But I know that when someone doesn't know something it only means that > this someone doesn't know this subject and it certainly doesn't mean that > this someone is necessarily stupid.I do wonder if you might want to consider reading some of the documentation about S to help you learn. I would not expect to understand C programming by trial-and-error.> I find out by trial and error later that you can do the following. > > foo<-function(x,y,z){ > ... > abc<-list(a=x,b=y,c=z) > } > > and call > > foo(1,2,3) > > there is nothing outputted to the console which is close to what I ask in > the first part of my query. I'll continue investigating to learn if it's > possible to export or whatever is the right term, the variables I want so > that there available for future use at the top level.If you want to return an object, make it the last statement of your function, that is use foo<-function(x,y,z){ list(a=x,b=y,c=z) } foo(1,2,3) Your example *did* return something, invisibly:> foo<-function(x,y,z){+ abc<-list(a=x,b=y,c=z) + }> foo(1,2,3) > .Last.value$a [1] 1 $b [1] 2 $c [1] 3 -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
At 08:48 AM 6/9/00 -0400, Ben Bolker wrote:> >You may want to look at the help for "assign", or, as a shortcut, try >the "<<-" version of assignment: > >> foo <- function() { a <- 1 } >> foo() >> a >Error: Object "a" not found >> foo <- function() { a <<- 1 } >> foo() >> a >[1] 1 > >Note that this kind of question will eventually lead you into the thick >of issues like environments, scoping, closures, etc. ... but for simple >global assignment I think "<<-" is what you need... but be sure to wear steel-capped boots. This is a quick recipe for shooting yourself in the foot or, worse, shooting someone *else* in the foot who happens to use your software. This warning has been said before but I think it should be voiced *every* time someone brings up superassignment. If you think you need "<<-" almost certainly you don't, and you are doing something dangerous or at least unwise. Leaving aside the dangerous aspect of this kind of assignment, there is an even more fundamental and specific objection. Using superassignment habitually, as some users seem to do, encourages you to think of R functions as macros and not as true functions at all, so you are using the R environment as you would something rather primitive, like Glim. You can lose a lot of power and elegance that way and it is a particularly insidious pit trap from which some people never manage to escape. Superassignment? Just say "No". Bill Venables. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._