Henrik Andersson
2004-Sep-01 10:13 UTC
[R] Accesing the name of an assigned object in a function
I want to use the name that I assign to an object in the function that produces the output, somewhat like below: stupid.function <- function(input){ [body] cat("Summarized output is ", output$summary, "Full output is given by typing", assigned.name, "\n") } assigned.name <- stupid.function(whatever) or another example is a function that sinks the results to a text file and names it assigned.name.txt . I checked the help for function, <-, assign but could not find it, is it possible ? --------------------------------------------- Henrik Andersson Netherlands Institute of Ecology - Centre for Estuarine and Marine Ecology P.O. Box 140 4400 AC Yerseke Phone: +31 113 577473 h.andersson at nioo.knaw.nl http://www.nioo.knaw.nl/ppages/handersson
Arne Henningsen
2004-Sep-01 11:14 UTC
[R] Accesing the name of an assigned object in a function
Hi Hendrik, if I understand you right, match.call() can help you. All the best, Arne On Wednesday 01 September 2004 12:13, Henrik Andersson wrote:> I want to use the name that I assign to an object in the function that > produces the output, somewhat like below: > > stupid.function <- function(input){ > [body] > cat("Summarized output is ", output$summary, "Full output is given > by typing", assigned.name, "\n") > } > > assigned.name <- stupid.function(whatever) > > > or another example is a function that sinks the results to a text file > and names it assigned.name.txt . > > I checked the help for function, <-, assign but could not find it, is it > possible ? > > > --------------------------------------------- > Henrik Andersson > Netherlands Institute of Ecology - > Centre for Estuarine and Marine Ecology > P.O. Box 140 > 4400 AC Yerseke > Phone: +31 113 577473 > h.andersson at nioo.knaw.nl > http://www.nioo.knaw.nl/ppages/handersson > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html-- Arne Henningsen Department of Agricultural Economics University of Kiel Olshausenstr. 40 D-24098 Kiel (Germany) Tel: +49-431-880 4445 Fax: +49-431-880 1397 ahenningsen at agric-econ.uni-kiel.de http://www.uni-kiel.de/agrarpol/ahenningsen/
Eryk Wolski
2004-Sep-01 11:34 UTC
[R] Accesing the name of an assigned object in a function
?assign /E On Wed, 1 Sep 2004, Henrik Andersson wrote:> I want to use the name that I assign to an object in the function that > produces the output, somewhat like below: > > stupid.function <- function(input){ > [body] > cat("Summarized output is ", output$summary, "Full output is given > by typing", assigned.name, "\n") > } > > assigned.name <- stupid.function(whatever) > > > or another example is a function that sinks the results to a text file > and names it assigned.name.txt . > > I checked the help for function, <-, assign but could not find it, is it > possible ? > > > --------------------------------------------- > Henrik Andersson > Netherlands Institute of Ecology - > Centre for Estuarine and Marine Ecology > P.O. Box 140 > 4400 AC Yerseke > Phone: +31 113 577473 > h.andersson at nioo.knaw.nl > http://www.nioo.knaw.nl/ppages/handersson > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
Gabor Grothendieck
2004-Sep-01 15:57 UTC
[R] Accesing the name of an assigned object in a function
Arne Henningsen <ahenningsen <at> email.uni-kiel.de> writes:> > Hi Hendrik, > > if I understand you right, match.call() can help you. > > All the best, > Arne > > On Wednesday 01 September 2004 12:13, Henrik Andersson wrote: > > I want to use the name that I assign to an object in the function that > > produces the output, somewhat like below: > > > > stupid.function <- function(input){ > > [body] > > cat("Summarized output is ", output$summary, "Full output is given > > by typing", assigned.name, "\n") > > } > > > > assigned.name <- stupid.function(whatever) > > > > > > or another example is a function that sinks the results to a text file > > and names it assigned.name.txt . > > > > I checked the help for function, <-, assign but could not find it, is it > > possible ?As far as I know you are going to have to pass the assigned.name to the function although there are a number of tricks that can make this a bit nicer. For example, run this: example(lm) # which has the effect of defining lm.D9 which we will use for our # examples below. Using that: #### 1 set1 <- function(x, value) { name <- as.character(substitute(x)) print(summary(value)$fstatistic) cat("That was F and df. For more info type", name, "\n") assign(name, value, parent.frame()) invisible(value) } set1(x, lm.D9) x # We can make this a bit prettier this way: #### 2 set2 <- structure(NA,class="set2") "[<-.set2" <- function(tmp,x,value) { name <- as.character(substitute(x)) print(summary(value)$fstatistic) cat("That was F and df. For more info type", name, "\n") assign(name, value, parent.frame()) tmp } # now we can write: set2[xx] <- lm.D9 xx # Another strategy might be to use a global variable to store the # last output from your function: #### 3 set3 <- function(value) { name <- as.character(substitute(x)) print(summary(value)$fstatistic) cat("That was F and df. For more info type .set3\n") assign(".set3", value, .GlobalEnv) invisible(value) } xxx <- set3(lm.D9) # or just set3(lm.D9) .set3