[R] Assessing the name of an object within an argument

Thomas Mailund mailund at birc.au.dk
Tue Jan 10 10:33:32 CET 2017


Yes, I was too fast there, sorry. I sent a correction right after but must have picked Reply instead of Replay All.

Cheers
Thomas



On 10 January 2017 at 10:31:30, Bert Gunter (bgunter.4567 at gmail.com<mailto:bgunter.4567 at gmail.com>) wrote:

This is false. formals() gives the FORMAL argument list of the function, not the ACTUAL arguments supplied. That is obtained by the construction

deparse(substitute(dataset))

The OP should consult a good R tutorial for this and other uses of substitute(), part of the "computing on the language" functionality of R.

Bert



On Jan 10, 2017 4:04 AM, "Thomas Mailund" <mailund at birc.au.dk<mailto:mailund at birc.au.dk>> wrote:

You can get that using `formals()`.

my_func <- function(dataset = iris)
{
  #print(dataset) # here I do not want to print the dataset but the name
  # of the object - iris in this case - instead

  print(formals()$dataset) # this is what you want
}

This gives you what the arguments were as an alist. It won’t always be a name, of course, but when it is, as in this case, that will be a symbol you can print.

Cheers
        Thomas



On 10 January 2017 at 09:51:55, g.maubach at weinwolf.de<mailto:g.maubach at weinwolf.de> (g.maubach at weinwolf.de<mailto:g.maubach at weinwolf.de>(mailto:g.maubach at weinwolf.de<mailto:g.maubach at weinwolf.de>)) wrote:

> Hi All,
>
> I have a function like
>
> my_func <- function(dataset)
> {
> some operation
> }
>
> Now I would like not only to operate on the dataset (how this is done is
> obvious) but I would like to get the name of the dataset handed over as an
> argument.
>
> Example:
>
> my_func <- function(dataset = iris)
> {
> print(dataset) # here I do not want to print the dataset but the name
> of the object - iris in this case - instead
> # quote() does not do the trick cause it prints "dataset" instead of
> "iris"
> # as.name<http://as.name>() gives an error saying that the object can not coerced to a
> symbol
> }
>
> Is there a way to do this?
>
> Kind regards
>
> Georg
>
> ______________________________________________
> R-help at r-project.org<mailto:R-help at r-project.org> mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
______________________________________________
R-help at r-project.org<mailto:R-help at r-project.org> mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


	[[alternative HTML version deleted]]



More information about the R-help mailing list