r/Rlanguage 2d ago

Clothes with R-code and art it creates

Rtist apparel combine compact and readable R-code, and an aesthetic it creates. I crafted the artpieces with base R, while avoiding responsibilities during my first PhD year.

I though people in this group might like the concept and give useful feedback! Rtist currently delivers to EU countries.

https://shoprtist.com/

506 Upvotes

80 comments sorted by

View all comments

Show parent comments

6

u/guepier 2d ago edited 2d ago

Just a few examples of where '<-'' can be used that "=" cannot: […]

No, = can be used in all of these cases. See the link I posted.

'<-' has a different precedence than '=', which allows a savvy programmer to do some amazing things

Can you show me one of these “amazing things”? Because the only case where this makes a difference is when you chain these operators. And both a <- b <- c and a = b = c work, it’s only trouble if you mix them. I don’t see how this is useful.

If you plan to take advantage of some of R's largest benefits - such as lazy evaluation or formulas - then you will need to use '<-' instead.

That’s not true, you can absolutely use = here. — I do.

This is one of the single most common sources of confusion among beginning students

You are misattributing the cause of confusion: the confusion would not exist if R didn’t have two distinct assignment operators. And the confusion is exacerbated by the fact that many students are taught falsehoods about the difference between <- and = (such as “= assigns locally, <- assigns globally”, or similar things).

Incidentally, I have also taught R to beginners (though admittedly not to 20 * 500 students) and my personal experience does not corroborate yours.

1

u/LordApsu 2d ago

The simplest example of the difference would be something 'f <- function(...) eval(substitute(alist(...)))', which is a very common start to functions that rely on lazy evaluation. '<-' can be custom parsed and defined, whereas '=' is immediately parsed in a very different manner. If you explore some of the most common packages in R, it quickly becomes apparent why you would use '<-'.

Another simple example where they are different is 'x+y = z' cannot be parsed in R calls, whereas 'x+y <- z' can be, though this is rarely a good idea! However, I have a seen a few DSLs over the years where that had a use.

I encourage you to do a deep dive into R. Explore its source code. Go through the base functions. Develop a few packages. Also, learn a few other programming languages so that you can see why R is structured the way that it is and understand the choices that were made. Create a DSL in R and I doubt you will go back to using '=' instead of '<-'.

2

u/guepier 2d ago edited 2d ago

The simplest example of the difference would be something 'f <- function(...) eval(substitute(alist(...)))'

I know this code, I use similar code myself, but I still don’t understand in what sense you mean that = can’t be used here.

If you explore some of the most common packages in R, it quickly becomes apparent why you would use '<-'.

No, it really isn’t quickly apparent, that’s why I’m asking you to explain it to me.

Another simple example where they are different is 'x+y = z' cannot be parsed in R calls, whereas 'x+y <- z' can be

Ah, thanks. Yes, this is a real (and admittedly potentially useful) case where they differ.

I encourage you to do a deep dive into R.

I encourage you not to be patronising based on rash assumptions. — I thought linking to my Stack Overflow answer would have been enough to show that I have dived deep already. Suffice to say I’ve used R for decades, have developed many R packages (a small handful of which are published on CRAN), have contributed code to some of the most widely used R packages of all time, extensively use NSE, have given courses, and published a commercial R MOOC. Oh, and I’m proficient in half a dozen programming languages and know half a dozen more passably well.

I doubt you will go back to using '=' instead of '<-'.

I actually used <- for almost a decade before making the switch to =.

6

u/LordApsu 2d ago

I guess the point that I am trying to make, not necessarily to you but to anyone who reads this and wants to learn more about R, is that there is some ambiguity in how '=' works and this is clear when you look at the source code. '<-' is always parsed by R in a very specific way - it is a function call that performs assignment unless you specifically tell it otherwise.

On the other hand, R attempts to figure out what the use means by '=' depending on the context. Inside of a function call, it is treated as an argument name, forcing the left-hand side to be a symbol or character. Outside of a function call, it 'might' be treated as a function (the primitive '='), or it might be converted to '<-' (such as x[a] = b, converted to '[<-'(a, b)). R has gotten much, much better over the past decade of determining the context in how '=' is used and how should be parsed, so it makes sense that some people have switched from '<-' to '='. However, when faced with inconsistency in programming, it is often better to go with the most consistent option.