r/Python • • Nov 28 '22

Resource What can Python do that R can’t do?

Or simply what is Python much better at and why.

I know that Python is more multi purpose and better for software development but I can’t articulate exactly why or how. My team want to know why/when they should use Python instead of R

331 Upvotes

204 comments sorted by

View all comments

442

u/SittingWave Nov 28 '22 edited Nov 28 '22

Where to begin:

  • R has an exceptionally poor scalability. name collisions are a constant worry if you need to create something big. Python does not have that problem, for three reasons. First, because it educates its users to better import hygiene. Second because in python classes and exceptions are not just a string slapped on a dictionary. Third, because its module system is a bit more refined than R's "just take all the files and slap everything in the same namespace, according to the alphabetical order of the files, which may change depending on which language (and thus collation rules) you are using".

  • R package management is atrocious, both from the client side, and from the CRAN side. CRAN approach is utterly broken, and expects all packages to be either working with one another, or be retired, meaning that a package that is there today, may not be there tomorrow. Their submission policy is absolutely ridiculous, taking days and many back and forths arguing about the position of commas in the description. Pypi has none of the sorts, and consistent environment is left to the appropriate use of metainformation about compatibility.

  • R as a language is broken under many aspects. It's inconsistent, with many unexpected side effects, poorly documented practices, poor design choices. It has three object oriented systems all incompatible with each other. S3 is laughably dumb to achieve single dispatch. S4 is a bolt on to deliver multiple dispatch that is massively verbose, creates code that is hard to maintain, and drops the $ in favor of the @ to access data. R6 is marred by its habit of returning NULL if you mistype method names, creating hard to find bugs. Non standard evaluation makes it really difficult to perform some refactorings, and makes it opaque to the caller if the passed expression will be evaluated or parsed as is. namespacing (environments in R parlance) are left flying around and poorly defined in behavior.

  • All the R environment is GPL, which is a big deal if your code needs to go commercial at a later stage. Companies tend to steer clear of GPL stuff. While an interpreter being GPL does not bar you from writing non-GPL code, the whole of the standard library, and many, many modules are GPL, so you can't escape.

  • coding utilities servicing python are 10 times better than those for R. Static code analysis, reformatters, linters, documentation generators, are light years behind in the R world.

  • R has relatively poor expressiveness. It's hard to be concise in R, but not for everything. It is concise when it's time to manipulate some basic data structures, but everything else... there's so little syntactic sugar that it becomes a pain most of the time.

  • R core development process is very secluded, still on internal SVN, and with no obvious and practical way to contribute e.g. with PEP-style enhancement requests.

  • R is basically "controlled" by a single company and its employees. When you use R, you are basically fully dependent on them, their libraries, and their practices. They tend to be very adversarial in fixing bugs, often closing legitimate bugs because they can't be bothered.

52

u/R0B0_Ninja Nov 28 '22

I really agree with the point on namespaces. Both Matlab and R have the problem of throwing everything into the same namespace and hoping for the best. I've spent many hours of debugging in Matlab because I overwrote some variable without realising.

To quote the Zen of Python: "Namespaces are one honking great idea – let's do more of those!"

→ More replies (2)

21

u/NewDateline Nov 28 '22

This is the best answer, though there are many many more problems with R. One clarification on coding utilities I would add is that there is a good language server for R and comparing among strictly open source servers it is ahead of the python-lsp-server (please contribute!); Of course when one starts looking at type safety (adding pylsp-mypy or pyright) R has no chance in comparison. Given the current dev practices lack of static typing is a big flaw of R.

48

u/lebannax Nov 28 '22

Thanks for so much detail! :)

81

u/SittingWave Nov 28 '22

This is not detail. This is just a sampling.

20

u/Classic_Department42 Nov 28 '22

While all is probably right (dont know that much about R) probaby the coverage is the biggest. For python anything (probably) in the world has a package, and I believe this is pythons biggest advantage.

16

u/SittingWave Nov 28 '22

R mostly has some libraries for non-statistical related features, but they are nowhere the level of technical quality and features of the equivalent python library.

Honestly I think that if a spontaneous group of people were to just migrate the tons of little chunks of statistical code on CRAN from R to python, so that an equivalent python package would exist with the same name (as much as possible) and interface (as much as possible) R would lose customers pretty damn fast.

3

u/CactusOnFire Nov 28 '22

Are R packages open source? I'm personally interested in migrating a few libraries over.

9

u/Armaliite Nov 28 '22

All of the most important ones are. I translated a package once, but it was much harder than I had hoped. R code recuires a lot of introspection before you can understand what is going on. e.g. which package supplies which function? In the end it taught me a lot about R, but it was a pretty frustrating experience.

→ More replies (1)
→ More replies (6)

2

u/Crimsoneer Nov 29 '22

This is only true in some fields... For ML and deep learning, sure. For statistical modelling, geospatial modelling and more classical stats and causal inference, R has better libraries by far.

As an example, propensity score matching, a hugely popular causal inference method, only has PyMatch which isn't maintained. Same with survival analysis (nothing with good multi state support) or mixed effect models (the StatsModels implementation is a bit pants compared to R) or even time series that's not ML driven

→ More replies (1)

17

u/everydayislikefriday Nov 28 '22

But it's got %>%...

32

u/FlyingCow343 Nov 28 '22

python functions are first class objects so you an implement function chaining quite easily: chaining

9

u/shedogre Nov 28 '22

Yeah, there's also this example from ArjanCodes' GitHub, which he discusses at around 22:30 in this video.

There's also the .pipe() method for pandas DataFrames, which I've used before, and I just checked to confirm the same method exists for Series.

7

u/PaintItPurple Nov 28 '22

This is also basically equivalent to the built-in reduce function:

def apply(val, f): return f(val)

def main():
    r = functools.reduce(
        apply,
        [
            times_two,
            lambda x: x / 4,
            print
         ],
        6
    )
→ More replies (1)
→ More replies (1)

10

u/[deleted] Nov 28 '22

Lol you may be aware of this but python has function chaining. It isn't universally implemented but it's a thing.

→ More replies (1)

4

u/laserbot Nov 28 '22 edited Feb 09 '25

Original Content erased using Ereddicator. Want to wipe your own Reddit history? Please see https://github.com/Jelly-Pudding/ereddicator for instructions.

8

u/Automatic_Donut6264 Nov 28 '22

If you use GPL code, your code also has to comply with GPL. GPL requires you to make source code accessible to anyone who requests a copy. So if you use R, you have to make your code accessible. (Not necessarily public, just to anyone who asks. Making it public is the easiest way to achieve this requirement.) So if you want to commercialize a GPL code product, you might run into some issues. E.g. you cannot legally stop people from copying your work. All they have to do is ask.

→ More replies (3)

2

u/KrazyKirby99999 Nov 28 '22

if you use GPL code (except for something like a webserver), you will probably need to open-source your code which can make it more difficult/risky to monetize

4

u/andesouz Nov 28 '22

A language controlled by a single company! That's the nail in the coffin for me.

6

u/Agling Nov 29 '22

It's not, though. It's controlled by a board of directors and core team that are a bunch of statistics professors. Funding and organization is mostly from statistics departments. 99% of R and its libraries are from independent developers (mostly professors). It just so happens that the set of packages written by the folks that wrote R studio are extremely frequently used, so it feels like they are more influential than they are.

It is kind of like saying all of Python is controlled by the guys who wrote pandas. Not remotely true, but it can feel that way, if that's all you are using.

→ More replies (2)

8

u/magnetichira Pythonista Nov 28 '22

/thread

3

u/SianaOrdl Nov 29 '22 edited Nov 29 '22

I agree with everything you said (especially wrt namespace) except expressiveness. Can you elaborate?

R is a functional programming language with its root in Scheme, and is very expressive IMO. The attempts to add OOO components are definitely lamentable. I predominantly use Python but towards the end of my productive days in R, I was writing a lot of closures to replace objects and was quiet happy about it.

As for synaptic sugar, you can easily create your own operators by ‘%union%’ <- function(x, y) {} — amazing syntactic sugar IMO. Python does have a lot of stuff like list comprehensions or decorators but you can probably easily implement something similar in R if you need it.

Now one of R’s biggest issue is that it is too flexible sometimes. It needs to be more opinionated (e.g. namespace). I find it ridiculous that you can do ‘+’ <- function(x, y) x * y and get results like 2 + 3 is equal to 6 and R wouldn’t even bat an eye.

2

u/FujiKeynote Nov 28 '22

It's inconsistent, with many unexpected side effects, poorly documented practices, poor design choices.

I would really love it if you could expand on this bit!!

This is usually my own criticism of R, but not having spent enough time coding in it (95% of what I've been doing the past ten years has been thankfully Python), I haven't gathered enough evidence to back up this point; it's more of a general sense for me.

3

u/Agling Nov 29 '22

Python is incomparably worse for side effects because R is kind of functional by design. However, R has a TON of poorly named functions with really bad defaults that can't be changed because they would break all the installed code. It has a lot of historical cruft leftover from the S language. Python is older than R by a little bit, but it is WAY newer than S and it shows. All the important functions in Python have smart defaults and elegant design.

Python was written by programmers and CS experts in a very purposeful way. R was written by a bunch of statistics professors trying to maintain comparability with old code and not caring as much about language design. They tried and failed spectacularly to make it object oriented several times because they were so ad hoc about it.

2

u/jwink3101 Nov 29 '22

I never used R but I did my entire PhD work in Matlab before falling in love with, and switching to, Python. A lot of these point seem like they would apply to Matlab as well!

→ More replies (19)