R for C++ Developers
Move from C++ to base R: vectors, missing values, data frames, functions, S3 dispatch, S4 validity and closure state. Build a connected multi-file basket and port a stock ledger.
C++ and R side by side
The same program in C++ and in R, from the lessons of this course.
Text and Output
C++ stream insertion writes values through std::cout, with a newline supplied explicitly. R cat also needs an explicit final newline, while paste and paste0 construct strings. Rscript runs an entry file and source evaluates supporting declarations. R print…
C++1#include <iostream>2#include <string>3 4int main() {5 std::string name="Ada"; std::cout << "Hello, " + name << "\n";6}R1ready <- 32cat(paste0("Ready: ", ready), "\n", sep="")Comparisons and Recycling
C++ standard vector equality compares sequences and yields one bool, while pointer equality compares addresses. R == is elementwise and can recycle shorter operands. Use all to reduce a vector when one decision is needed, or identical to include storage types…
C++1#include <iostream>2#include <vector>3 4int main(){5std::vector<int> a{2,3},b{2,3};std::cout<<std::boolalpha<<(a==b)<<"\n";6}R1cat(identical(1L, 1), all(c(2, 3) == c(2, 3)), "\n")Vector Subsetting
C++ vectors use zero-based indexes; back accesses the last element only when the vector is nonempty. A negative subscript is not end-relative access. R positive indexes are one-based, zero selects nothing and negative indexes exclude positions. Do not mix…
C++1#include <iostream>2#include <vector>3 4int main(){5std::vector<int> values{4,8,12};std::cout<<values.back()<<"\n";6}R1values <- c(4, 8, 12)2cat(paste(values[-1], collapse=" "), "\n", sep="")Typed Iteration
C++ algorithms and loops produce results in containers with declared element types. R lapply returns a list, sapply attempts simplification and vapply checks a supplied result prototype. Choosing the expected type and length per result makes shape and…
C++1#include <iostream>2#include <vector>3#include <string>4 5int main(){6std::vector<std::string> words{"a","tea"};std::vector<std::size_t> lengths;for(const auto& word:words)lengths.push_back(word.size());std::cout<<lengths[0]<<":"<<lengths[1]<<"\n";7}R1words <- c("a", "tea")2sizes <- vapply(words, nchar, integer(1), USE.NAMES=FALSE)3cat(paste(sizes, collapse=" "), "\n", sep="")Functions and Returns
C++ functions declare result types and generally use explicit return statements for produced values. R returns its last evaluated expression by default and has return for early exit. R functions use lexical scoping and lazy argument promises instead of…
C++1#include <iostream>2int twice(int value){return value*2;}3 4int main() {5 std::cout<<twice(3)<<"\n";6}R1is_long <- function(text) nchar(text)>42cat(is_long("coffee"), "\n", sep="")String Parsing
C++ from_chars reports where parsing stopped, so full-token validation checks the end pointer as well as the error code. R trimws and strsplit work on character vectors, and strsplit returns a list of token vectors. Fixed separators differ from regular…
C++1#include <iostream>2#include <string>3#include <charconv>4#include <system_error>5 6int main() {7 std::string text="12cats";int value=0;auto result=std::from_chars(text.data(),text.data()+text.size(),value);bool valid=result.ec==std::errc{} && result.ptr==text.data()+text.size();std::cout<<std::boolalpha<<valid<<"\n";8}R1parts <- strsplit("tea,cake", ",", fixed=TRUE)[[1]]2cat(length(parts), "\n", sep="")
Syllabus
10 chapters48 lessons1 project48 challenges99 quiz questionsFiles and Atomic Values
6 lessonsLogic and Iteration
6 lessonsCollections and Ownership
5 lessonsData and Transformations
4 lessonsFunctions and Closures
5 lessonsParsing and S3 Dispatch
5 lessonsObjects and Invariants
6 lessonsCollaboration and Contracts
5 lessonsProject: Basket
4 lessonsFinal Challenges
2 lessonsFrequently asked questions
Who is R for C++ Developers for?
For people who already write C++ and want to learn R. Each lesson starts from C++ you know and shows how R does the same thing, so you skip the beginner basics.
Do I need to know R already?
No. The course starts with your first R program. You need C++ basics: variables, conditions, loops, functions and collections.
How many lessons does R for C++ Developers have?
R for C++ Developers contains 48 interactive lessons you can complete at your own pace.
What does R for C++ Developers cover?
The course is organized into chapters covering: Files and Atomic Values, Logic and Iteration, Collections and Ownership, Data and Transformations, and more.
Is R for C++ Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.