11.8 Resources
This section provides links to various resources on writing functions in R and computer programming in general.
11.8.1 More about functions
For more details on R functions:
read Chapter 19: Functions (Wickham & Grolemund, 2017) and complete its exercises;
see R-exercises.com for additional exercises;
study the Posit cheatsheets on base R and Advanced R on Functions:
11.8.2 Programming
There are many good books on computer programming, but even more bad books on the same topic, unfortunately. Here are some recommendations that I found helpful.
Classics
To discover programming as an inspiring and deeply gratifying intellectual endeavor, it helps to stay away from the “Mastering WhizzBang v2.0 in 10 easy steps” and “X for dummies” variety of flashy cookbooks and stick with the serious stuff:
Although Donald E. Knuth published the first volume of The Art of Computer Programming (TAOCP) in 1968, it is still incomplete today (Knuth, 1968).71 The volumes provide the defining treatise on the design and analysis of algorithms and evoke either reverence or fear in the initiated (i.e., a bit like The Bible):
- TAOCP on Donald Knuth’s homepage
- Wikipedia: TAOCP
- For a career in computer science, read the books and then send your resume to Bill Gates
- TAOCP on Donald Knuth’s homepage
A classic text on object-oriented programming with a mindset of combining simple functions into powerful systems is The Structure and Interpretation of Computer Programs (SICP) (Abelson, Sussman, & Sussman, 1996):72
How to become a hacker (by Eric S. Raymond) captures the mindset of many aspiring programmers (just as The cathedral and the bazaar captures the essence of open software developement).
For developing a pragmatic approach towards programming and software design, The Pragmatic Programmer: From Journeyman to Master (Hunt & Thomas, 1999) is helpful, despite its cringeworthy title.
Online help
Teach a man to fish, and he’ll use StackOverflow for a day.
Give him a fish, and he’ll use StackOverflow for a lifetime of free fish.Joshua Ulrich (2016)
R developers devote an enormous amounts of time and effort into documenting functions and packages. If your questions are not answered by typing ‘?’ and the name of a function, here are some pointers to online resources:
R help shows that R provides many ways of helping yourself, but also pointers to various online resources
R seek is a search engine specializing on R
rdrr.io allows searching R packages
Stack Overflow is a wonderful place to learn more about R — but make sure you properly searched previous answers prior to asking new questions
The Learning Machines blog (by Holger K. von Jouanne-Diedrich) provides many excellent posts on basic programming concepts. Examples relevant to this chapter include:
11.8.3 Advanced R
To really understand how functions are evaluated and used in R, Advanced R (Wickham, 2014a) devotes chapters on
See also the 2nd edition of Advanced R (Wickham, 2019).
For specific issues when writing functions within the tidyverse, see:
- Tidy evaluation (by Lionel Henry and Hadley Wickham)
- The dplyr vignette on programming (
vignette("programming")
)
11.8.4 Recursion
More xkcds on recursion
Another witty example of recursion is provided in the xkcd clip of Figure 11.6.
There are many excellent xkcd comics on this theme. For instance, check out
- Pixels (xkcd.com/1416)
- Conversation (xkcd.com/1423)
- Ozymandias (xkcd.com/1557)
- Tools (xkcd.com/1629)
- Fixing Problems (xkcd.com/1739)
for related illustrations. Note that the following examples are particularly relevant here:
- Functional (xkcd.com/1270) (see explainxkcd.com for an explanation), and
- Error Bars (xkcd.com/2110) is especially suited for psychologists doing statistics
11.8.5 Sorting
Sorting algorithms
Check out Wikipedia: Sorting algorithm for many additional algorithms.
xkcd comics also has a well-known clip on Ineffective sorts (xkcd.com/1185)
To understand Recursion you have to understand Recursion… illustrates the concept of recursion in the context of sorting (with many nice examples and video clips).
Video clips of sorting algorithms
In Section 11.4.2, we illustrated the Quick sort algorithm by an Hungarian folk dance (Küküllőmenti legényes). Readers with a deep love of folk dances should check out related enactments of sorting algorithms (created by Sapientia University, Tirgu Mures (Marosvásárhely), Romania, under the direction of Kátai Zoltán and Tóth László):
More technically minded readers should watch the following clip (and try to re-implement its sorting algorithms in R):
- 15 Sorting Algorithms in 6 Minutes (by Timo Bingmann): Visualization and “audibilization” of 15 sorting algorithms in 6 minutes. Sorts random shuffles of integers, with both speed and the number of items adapted to each algorithm’s complexity.
This concludes our links to resources on programming functions in R.
[11_functions.Rmd updated on 2024-12-20 by hn.]
References
Fun fact: One of the collateral projects of TAOCP that pre-occupied the author for years was the development of a typesetting system that was able to present mathematical expressions and computer code in aesthetically pleasing form. This side-project developed into the TeX system (see Wikipedia: TeX/LaTeX for details), which is still the de facto standard in many sciences today.↩︎
This book is originally from 1984 and uses the Lisp dialect Scheme. If this seems obscure, note that SICP is widely known as “the wizard book” or “the purple book”, and was used to teach many generations of MIT students into the realms of computer science.↩︎