[Rd] sys.call() inside replacement functions incorrectly returns *tmp*

Emil Bode emil@bode @ending from d@n@@kn@w@nl
Tue Oct 16 11:53:34 CEST 2018


I think what you want to do is hard if you insist on using a replacement function, i.e. massign(vars) <- values.
But as you wrote, you can use a modified assignment operator, or you could write something comparable to assign, so massign(listofvars, listofvalues, environment) shouldn’t be too hard.
I believe in this context, the term “replacement function” is a bit confusing. In my experience, replacement functions are for editing a certain aspect of a variable or object, mostly attributes, or they can replace certain elements. But in order to edit something, it has to exist in the first place.
It may be that I’m reading it wrong, but to me “massign (x, y, z) = somelist” reads “I want to edit some aspects of x, y and z, by inputting somelist to them”. In that guise, I can see some function like mlevels(factor1, factor2, factor3) <- as.character(1:10), to set the level attributes to all three factors to the same set of levels.
That’s not something easily supported with replacement functions, but it is possible.
But for creating new objects, or reassigning them, a replacement function seems not the way to go to me.
And as Barry noted, why do you want to pull these values apart in the first place? I guess your example of polar coordinates was just an example, but if you want to store multiple values that’s a lot easier in a list, and it keeps your workspace tidier.
For your other point, editing attributes is possible with `attr<-`.
If you want to have it as an infix operator without having to use quotes:
`%$%<-` <- function(obj, attr, value) {
  `attr<-`(obj, as.character(substitute(attr)), value)
Now “myobj %$% myattr <- values “ will do as you desire

Best regards,
Emil Bode

From: Abs Spurdle <spurdle.a using gmail.com>
Date: Tuesday, 16 October 2018 at 01:02
To: Emil Bode <emil.bode using dans.knaw.nl>
Cc: r-devel <r-devel using r-project.org>
Subject: Re: [Rd] sys.call() inside replacement functions incorrectly returns *tmp*

Kia Ora

> Although I'm not sure what problem it would solve...

Given that you asked, I was interested in writing a multiple assignment function as a replacement function, so something like:
massign (x, y, z) = construct.some list ()

Obviously, that's not possible.

Probably the best example I can think of is converting cartesian coordinates to polar coordinates.
Then we might have something like (note, untested, written in my email):
cart2polar = function (x, y)
    list (theta=atan (y / x), r=sqrt (x * x + y * y) )

massign (r, theta) = cart2polar (x, y)

Now, I'm considering a multiple assignment operator, so something like:
c (theta, r) $<-$ cart2polar (x, y)

And while we're on the topic, I'm also considering an attribute operator (to access object attributes), something like:

This would be similar to . in C++/Java or @ in S4.
And seems like something obvious that's missing from R.

Implementing an attribute read this way is easy, however, implementing an attribute assignment this way (without language level support) is difficult.

kind regards

	[[alternative HTML version deleted]]

More information about the R-devel mailing list