Is it necessary/useful to learn C for R package development?

I notice that many statistical package developers write the core algorithm in C, and then wrap the C files in an R function. It is a common strategy to speed up the intensive statistical computation.

As a student in statistics major, it is very likely for me to write statistical packages in the coming years. Thus, I am curious that will it be necessary/useful to learn C for R package development?

Probably useful but not necessary.

Packages themselves are not difficult to make:

Have a look at:

3 Likes

I agree with @williaml.

There are three reasons why you'd want to develop packages with a C/CPP (or even Go):

  1. Speed
  2. To access functions from the vast armory of available libraries
  3. To have a richer suite of procedural/imperative coding tools to work from than are provided in base R

While we all chant faster is better, the truth is that for most day-to-day work, a ten-fold difference may amount to less than a second.

With packages like RCpp, it's possible to access functions directly from R without having to compile a C/Cpp program first.

R mostly presents itself to the user as a functional language, not a procedural one. Identify a class of target object, compare it to a source object and then compose a function, often composite of many functions, to implement the f(x) = y. With thousands of packages, there is probably already one or more existing functions for any standard statistical problem. It's only when you cannot find a way to string together functions because a new algorithm is needed, for example, that it might be necessary to turn to writing it in C/Cpp and, even then, there's Haskell, a functional language to consider.

2 Likes

Is it necessary to learn C/C++? No, of course not. That said, not only lots of packages but R itself is written in C. If you want or need to dig deep enough you'll likely ending up wanting to at least understand some C/C++ source code. So your 2nd implicit question:
Is is useful? Very context dependent, but the maximum likelihood relationship between general usefulness of C/C++ skills and time spent playing with R is strongly positive.

2 Likes

Hello!

For what it's worth, I have now built many packages for work and personal use and I have no idea how to code in C or C++. Yes, I'm sure it's useful, but for me the juice is not worth the squeeze.

I love packaging my functions. It makes it easy to find and document so that when you come back to them after a while you know where they are and what they do.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.