Live data from Hacker News

Learn R Through Examples (2020)

gexijin.github.io

51–60 of 68 posts

Re: Learn R Through Examples (2020)

#51
post #23

Lot's of negative comments in here about learning R from experienced programmers. I've found this is largely because experienced programmers have this unjustified bias that R is some toy language that should be easy to learn and has nothing to teach them. If you approached a language like Rust in the same way you would likely be just as frustrated with it. Certainly R has its quirks, but most of this comes from being…

I find it highly unlikely that learning R will expand your programming views anywhere near Haskell. Haskell is an advanced functional programming language. Most R stuff seems to be incoherent, hard to verify correctness, hacky. It does not seem built on a solid foundation like Haskell. Truly everything being a vector is not a huge take away. As for "here's just a bunch of examples", well that seems sort of a brute fo…

R certainly expanded my programming views! Haskell did too, but the lessons of Haskell didn't stick the way that R's lessons did. Here are some of the things I learnt from R (though they can be found in other languages of course).

* Multiple dispatch. Before learning R, I knew about polymorphism in Java and C++, and multiple dispatch in R broadened my mind and turns out to be very handy.

* The idea of "frames". In R, when you invoke `lm(height~sex*age, data=mydataframe)`, the first argument (the formula) doesn't get evaluated until the lm command asks it to be evaluated, and lm can set up the "frame" for that evaluation, i.e. the place where variables are looked up, however it likes. In fact, lm sets it up to include variables from both the scope in which you invoked lm, and also from mydataframe. This is what makes R so wonderfully concise for modelling in data science, compared to e.g. Python + pandas. I knew about frames from interactive debuggers, but until R it never occurred to me that the programming language could manipulate them.

* "Held" arguments. In R, when you invoke `plot(x, y1+y2)`, it doesn't just evaluate the arguments and then call the plot function -- it leaves the arguments unevaluated, and invokes plot. Plot then (1) decides when to evaluate them, (2) gets access to the language expression `y1+y2`, which means that it can print "y1+y2" on the plot label, (3) it can even define extra variables to include in the scope when y1+y2 gets evaluated. (I knew about held arguments earlier, from Mathematica, but they only clicked when I read the R documentation.)

I've read that R is a descendent of Scheme, and that that's where it gets all its "manipulate language expressions" from. I don't know any Scheme, nor Lisp, and I should definitely learn them -- but in the meantime, my experience has been that R's ability to manipulate language expressions is what makes it such a wonderful sweet spot as a data modelling language. I mostly use Python + pandas nowadays, but it feels such a slog in comparison.

Re: Learn R Through Examples (2020)

#52

I recently tried to migrate my R code to Julia. Even though I already knew R data.table is faster than DataFrames.jl, I was totally blown away by how slow Julia is. So I quickly gave up. I think I will have to write unavoidable hard loop in cpp, which I really don't want to do...

There are a few tricks to getting Julia to be actually fast, and while it's not hard per se if you know them all (at least for numerical work), it's definitely not trivial.

IMHO, you really have to embrace dispatch-oriented programming, and that includes being scrupulous about avoiding type instability. You also have to be a bit conscious about allocations, since it's easy to write Julia code (especially if you're trying to write in a "vectorized" style as is common in R, Python, Matlab) that generates absurd numbers of allocations, which must then be garbage-collected. But also easy to avoid those allocations if you know.

It took about two years, but after picking up more of this, I was eventually able to switch everything my group does from a two-language solution of matlab for scripts and plotting and C (with MPI) for HPC to all-Julia. This [1] was originally targeted at academics making the same switch, but much of it could be relevant to those with an R background as well.

[1] https://github.com/brenhinkeller/JuliaAdviceForMatlabProgram...

Re: Learn R Through Examples (2020)

#54

Lot's of negative comments in here about learning R from experienced programmers. I've found this is largely because experienced programmers have this unjustified bias that R is some toy language that should be easy to learn and has nothing to teach them. If you approached a language like Rust in the same way you would likely be just as frustrated with it. Certainly R has its quirks, but most of this comes from being…

> S3, predates Java and therefore uses the Generic Function paradigm of OOP similar to Common Lisp's ClOS

S3 appeared a few years before Java but there were other OOP languages like C++ around at the time.

Re: Learn R Through Examples (2020)

#55

Lot's of negative comments in here about learning R from experienced programmers. I've found this is largely because experienced programmers have this unjustified bias that R is some toy language that should be easy to learn and has nothing to teach them. If you approached a language like Rust in the same way you would likely be just as frustrated with it. Certainly R has its quirks, but most of this comes from being…

S4 is the one that is reminiscent of CLOS. Dylan was explicitly cited as an inspiration [0]

[0] There's an old article from Robert Gentleman named something like "S4 objects in 5 pages, more or less" but I can't find it. However, there's a mention of Dylan and CLOS here: https://genomebiology.biomedcentral.com/articles/10.1186/gb-...

EDIT: Here's the document I was looking to cite: https://www.stat.auckland.ac.nz/S-Workshop/Gentleman/S4Objec...

Re: Learn R Through Examples (2020)

#56

Here is my take on R as a guy who does stats as well as some software engineering in more mainstream languages like Python. R is a fantastic DSL for data manipulation and statistical analysis, with both traditional and modern tools, on datasets up to the gigabyte scale. It has great, easy-to-use data structures and unparalleled APIs in the tidyverse. It is not the thing for the latest deep learning implementation on…

As an experienced R programmer that doesn’t do much statistics or have a background in computer science. What language characteristics does R lack that makes it a DSL instead of GPL?

Edit: I found an interesting quote from a guy named Martin Fowler about the subject.

“Languages can have a domain focus but still be general-purpose languages. A good example of this is R, a language and platform for statistics; it is very much targeted at statistics work, but has all the expressiveness of a general-purpose programming language. Thus, despite its domain focus, I would not call it a DSL.”

Re: Learn R Through Examples (2020)

#57
post #26
post #24

Earlier quoted context omitted.

Just going back and looking through some of the homework, here's what I'm seeing imported: readr, caret, lattice, ggplot2, RColorBrewer, mlbench, ElemStatLearn, klaR, dplyr, arules, arulesViz, tensorflow

In this list only tensorflow requires Python

And I haven't used it, but there is Torch for R as the alternative which isn't supposed to have any dependency on Python.

https://torch.mlverse.org/

Re: Learn R Through Examples (2020)

#58
post #55

Lot's of negative comments in here about learning R from experienced programmers. I've found this is largely because experienced programmers have this unjustified bias that R is some toy language that should be easy to learn and has nothing to teach them. If you approached a language like Rust in the same way you would likely be just as frustrated with it. Certainly R has its quirks, but most of this comes from being…

S4 is the one that is reminiscent of CLOS. Dylan was explicitly cited as an inspiration [0] [0] There's an old article from Robert Gentleman named something like "S4 objects in 5 pages, more or less" but I can't find it. However, there's a mention of Dylan and CLOS here: https://genomebiology.biomedcentral.com/articles/10.1186/gb-... EDIT: Here's the document I was looking to cite: https://www.stat.auckland.ac.nz/S-W…

Arguably the S3 object system is also "functional" in spirit, even if it's single-dispatch.

https://arxiv.org/pdf/1409.3531.pdf

Object-Oriented Programming, Functional Programming and R (John M. Chambers)

"Chambers and Hastie (1992), in the discussion of classes and methods, noted that S differed from other OOP languages because of its functional programming style. In fact, this version of functional OOP finessed the resulting distinction from encapsulated OOP in two ways. First, the methods were dispatched according to a single argument, the first formal argument of the generic function in principle. As a result, the methods were unambiguously associated with a single class, as they would be in encapsulated OOP. Methods were actually dispatched on either argument to the usual binary operators, but a number of encapsulated OOP languages do the same, under the euphemism of operator overloading.

"Second, the question of whether methods belonged to a class or a function was avoided by not having them belong to either. Methods were assigned as ordinary functions and identified by the pattern of their name: “function.class”. In any case, there were no class objects and generic functions were ordinary functions that invoked UseMethod() to select and call the appropriate method. Neither the function nor the class was able to own the methods."

Re: Learn R Through Examples (2020)

#59
post #23

Earlier quoted context omitted.

I find it highly unlikely that learning R will expand your programming views anywhere near Haskell. Haskell is an advanced functional programming language. Most R stuff seems to be incoherent, hard to verify correctness, hacky. It does not seem built on a solid foundation like Haskell. Truly everything being a vector is not a huge take away. As for "here's just a bunch of examples", well that seems sort of a brute fo…

R certainly expanded my programming views! Haskell did too, but the lessons of Haskell didn't stick the way that R's lessons did. Here are some of the things I learnt from R (though they can be found in other languages of course). * Multiple dispatch. Before learning R, I knew about polymorphism in Java and C++, and multiple dispatch in R broadened my mind and turns out to be very handy. * The idea of "frames". In R,…

Syntactic forms ('frames', 'held arguments') are reasonably useful, but have two flaws:

A. Understanding how to implement functions using syntactic forms is a steep learning curve. I remember running out of dplyr and having to implement a udf. Fairly unpleasant experience (enquo, !!, perhaps other unusual constructs). Felt like programming C macros.

B. "A function can decide where variables are looked up however it likes" is a significant obstacle in understanding how even basic constructs like function calls actually work. There is a non-trivial amount of hard-to-debug dark magic lurking behind every corner.

A middle ground has never been achieved. For example, `plot(expr(x), expr(y1 + y2))`, where the system limits the dark magic to explicit uses of the `expr()` construct, and `expr(x)` always means `{vars: vars(x), expr: (vars(x)) => x}`. Instead of patching interpreter environments, simply call a lambda function.

Re: Learn R Through Examples (2020)

#60

Earlier quoted context omitted.

R certainly expanded my programming views! Haskell did too, but the lessons of Haskell didn't stick the way that R's lessons did. Here are some of the things I learnt from R (though they can be found in other languages of course). * Multiple dispatch. Before learning R, I knew about polymorphism in Java and C++, and multiple dispatch in R broadened my mind and turns out to be very handy. * The idea of "frames". In R,…

Syntactic forms ('frames', 'held arguments') are reasonably useful, but have two flaws: A. Understanding how to implement functions using syntactic forms is a steep learning curve. I remember running out of dplyr and having to implement a udf. Fairly unpleasant experience (enquo, !!, perhaps other unusual constructs). Felt like programming C macros. B. "A function can decide where variables are looked up however it l…

I completely agree about the steep learning curve and the feeling of dark magic -- how many times have I had to relearn what deparse(substitute(x)) means -- but oh the satisfaction of broadening my programming horizons. For me it didn't feel like C macros, it felt like "This must be what it feels like to have the power of Lisp"!

That's the weird thing about R. All this dark magic is hiding under the hood, but the core R team hid it so deftly that to the casual statistician it's a straightforward data modelling language that "just works". I'm not sure that it's possible to get rid of the dark magic and retain that data-modeller friendliness.

Post reply on HN