Hi,
n a package, I have a data object with attributes, and I want to
dynamically create a convenience function to access those attributes.
This way, instead of using attr(x, "number"), I would like to use number(x).
Because I don't know in advance which attributes the data object may
have, I've used the following algorithm:
x <- structure(pi, number=exp(1))
a <- attributes(x)
for (n in names(a)) {
if (!exists(n, mode="function")) {
f <- eval(parse(text=sprintf("function(x) { attr(x, '%s') } ", n)))
assign(n, f, envir=.GlobalEnv)
}
}
number(x)
However, I believe modifying the global environment with this is not
allowed by CRAN for a package. Is there a way to implement such
functionality?
Thanks Sigbert
Dynamically create a (convenience) function in a package
3 messages · Sigbert Klinke, Iris Simmons, Martin Maechler
If you don't know the name of the attributes in advance, how can you know the function name to be able to call it? This seems like a very flawed approach. Also, I would discourage the use of eval(parse(text = )), it's almost always not the right way to do what you want to do. In your case, eval(bquote(function(x) attr(x, .(n)))) would be better. On Mon, Oct 30, 2023, 06:28 Sigbert Klinke <sigbert at wiwi.hu-berlin.de> wrote:
Hi,
n a package, I have a data object with attributes, and I want to
dynamically create a convenience function to access those attributes.
This way, instead of using attr(x, "number"), I would like to use
number(x).
Because I don't know in advance which attributes the data object may
have, I've used the following algorithm:
x <- structure(pi, number=exp(1))
a <- attributes(x)
for (n in names(a)) {
if (!exists(n, mode="function")) {
f <- eval(parse(text=sprintf("function(x) { attr(x, '%s') } ", n)))
assign(n, f, envir=.GlobalEnv)
}
}
number(x)
However, I believe modifying the global environment with this is not
allowed by CRAN for a package. Is there a way to implement such
functionality?
Thanks Sigbert
--
https://hu.berlin/sk
https://www.stat.de/faqs
https://hu.berlin/mmstat
https://hu.berlin/mmstat-ar
______________________________________________ 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.
Iris Simmons
on Mon, 30 Oct 2023 06:37:04 -0400 writes:
> If you don't know the name of the attributes in advance,
> how can you know the function name to be able to call it?
> This seems like a very flawed approach.
> Also, I would discourage the use of eval(parse(text = )),
> it's almost always not the right way to do what you want
> to do. In your case,
> eval(bquote(function(x) attr(x, .(n))))
> would be better.
Indeed! Thank you, Iris!
... as an old timer, I'd like to raise
R> fortunes::fortune("eval(parse")
Personally I have never regretted trying not to underestimate my own future
stupidity.
-- Greg Snow (explaining why eval(parse(...)) is often suboptimal, answering
a question triggered by the infamous fortune(106))
R-help (January 2007)
R> fortunes::fortune(106)
If the answer is parse() you should usually rethink the question.
-- Thomas Lumley
R-help (February 2005)
R>
Best, Martin
> On Mon, Oct 30, 2023, 06:28 Sigbert Klinke
> <sigbert at wiwi.hu-berlin.de> wrote:
>> Hi,
>>
>> n a package, I have a data object with attributes, and I
>> want to dynamically create a convenience function to
>> access those attributes. This way, instead of using
>> attr(x, "number"), I would like to use number(x).
>>
>>
>>
>> Because I don't know in advance which attributes the data
>> object may have, I've used the following algorithm:
>>
>> x <- structure(pi, number=exp(1))
>>
>> a <- attributes(x)
>>
>> for (n in names(a)) {
>>
>> if (!exists(n, mode="function")) {
>>
>> f <- eval(parse(text=sprintf("function(x) { attr(x, '%s')
>> } ", n)))
>>
>>
>> assign(n, f, envir=.GlobalEnv)
>>
>> }
>>
>> }
>>
>> number(x)
>>
>> However, I believe modifying the global environment with
>> this is not allowed by CRAN for a package. Is there a way
>> to implement such functionality?
>>
>> Thanks Sigbert
>>
>> --
>> https://hu.berlin/sk https://www.stat.de/faqs
>> https://hu.berlin/mmstat https://hu.berlin/mmstat-ar
>>
>> ______________________________________________
>> 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]]
> ______________________________________________
> 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.