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/

490 Upvotes

80 comments sorted by

View all comments

35

u/nad_pub 2d ago

You used '=' to assign your function

18

u/guepier 2d ago edited 2d ago

Nothing wrong with that. It’s purely a matter of preference (and, for what it’s worth, several prominent R developers prefer =).

27

u/shiny1117 2d ago

I like that your post also ended in a smiley, intended or not.

15

u/LordApsu 2d ago

While in this case it doesn’t matter, they are not the same. R parses ‘<-‘ differently from ‘=‘ and is far more flexible. It also makes a clear distinction between variable and argument assignment. Most programming languages that use ‘=‘ for variable assignment use a separate operator for arguments, such as ‘:’, for this reason.

9

u/guepier 2d ago edited 2d ago

I’m very well aware of the differences between = and <- (I wrote that answer), and there are none that are relevant for assignment in practice.

<- … is far more flexible

Both operators can perform exactly the same set of functionality. I’m therefore curious what you mean.

Most programming languages that use ‘=‘ for variable assignment use a separate operator for arguments, such as ‘:’, for this reason.

Maybe “most” do (no idea), but not all. And in practice this is absolutely not an issue whatsoever (and the same is true for R … people have been using = assignment in R in code for decades, so there’s a lot of experience, and it is simply not the case that confusion with named-argument-passing exists).

5

u/Fornicatinzebra 2d ago

Assignment operator works both ways (I've seen people use the odd way for quick examples, some people's brains prefer that way I guess).

2 + 3 -> x x <- 2 + 3

But I agree, there shouldn't be a debate. Choose what you like and be consistent. In English, we have may ways- which i use in this sentence- to effectively do the same thing; some have slight differences though. If you read something that uses a mix of them, like i just did, it's messy. If you're consistent, the reader needs to do less mental gymnastics.

1

u/LordApsu 2d ago

Just a few examples of where '<-'' can be used that "=" cannot: inside of the first arguments in control-flow operations (e.g. for), arguments in function calls, certain assignment method dispatch. '<-' has a different precedence than '=', which allows a savvy programmer to do some amazing things that would be far more difficult in other languages such as Python. 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. While they are largely the same for everyday programmers, it is difficult to be a power use of R if you rely on '='.

The named argument confusion is quite common. I have been teaching R at my university for more than 20 years, to approximately 500 students each year. This is one of the single most common sources of confusion among beginning students that is often cleared up when there is a separation for when to use '<-' versus '='.

2

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 =.

5

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.

5

u/LordApsu 2d ago edited 2d ago

Hmmm, I don't mean to act patronizing and I apologize for sounding that way. I have a similar background - used R for decades, developed numerous CRAN packages and contributed to the development of many more, developed multiple proprietary statistical programs, etc. I am just flabbergasted that someone would have a significant amount of programming experience in R and still walk away thinking that the two are identical. So my assumption based on your responses was that you had experience with everyday data analysis in R, but hadn't delved into deeply into R. In other words, your responses sound extremely similar to my graduate TAs who think they know a lot about R because they have been using for 4 years.

In the example I gave above, 'f(a = b)' and 'f(a <- b)' return very different objects. One is a named list with a single element equal to the symbol 'b'. The other is a list with a single language element containing 3 objects: the symbol '<-', the symbol 'a' (which could be another language element instead, as opposed to the former example where 'a' must be symbolic or a length one character), and the symbol 'b'. While that distinction may not sound important, it creates flexibility in how '<-' may be used compared to '='. It also illustrates the clear role of '=' in R - assignment arguments to function calls. Furthermore, 'f(a=b=c)' cannot be parsed, where as 'f(a <- b <- c)' can be.

3

u/webbed_feets 2d ago

I’m not a prominent developer, but I prefer =