1

I have read that much of the functionality of R is done by C code that is called by R under the hood when an R program is run. Is this the case for every R program? Does this mean that every R statement is converted to C which is then compiled by a C compiler?

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Minaj
  • 165
  • 5
  • 1
    "Done by C code" is a loose and vague way of saying it that obscures some important points. There is certainly a runtime environment that executes natively on your machine, but how that runtime was created is not relevant at this point. It was probably translated from some high-level language like C, but that's not generally an observable part of the runtime. In other words, just because "someone wrote a C program to solve problem X" doesn't mean that problem X is intricately connected with C. – Kerrek SB Aug 21 '17 at 16:56
  • Granted R was written in C/C++. But that aside, even at run time, certain functions call C libraries. Does that mean these C libraries are already pre-compiled into 1s and 0s? – Minaj Aug 21 '17 at 17:35
  • Yes. They're not "C libraries". They're just "libraries". You access them through some form of ABI or FFI, which may or may not borrow a style or vocabulary from C, but there's nothing fundamentally "C" about either the libraries or the runtime. – Kerrek SB Aug 21 '17 at 17:57

1 Answers1

4

It means that R is written in C or C++ (I don't know which) in the same way that your web browser is written in C or C++. That doesn't mean web pages are converted to C and compiled with a C compiler, and neither are R programs.

  • 1
    Exactly. Basically C, and some Fortran, and a bunch of R in its libraries. C++ is supported for extensions but not used by R itself. – Dirk Eddelbuettel Aug 21 '17 at 16:52
  • I am having a hard time trying to figure out if a simple R "for" loop is written in C, hard to search through R source code for that. I had an instructor tell me in case of a timing test not to use sapply because "it's written in C" but I would assume the for loop is implemented in C as well? – JimLohse Feb 11 '18 at 06:01
  • Actually, without providing proof, the [first line of this answer](https://stackoverflow.com/a/42440872/3255525) makes a good case about speed of for vs. lapply in R – JimLohse Feb 11 '18 at 06:04