20 Advanced programming

 

This chapter covers

  • Diving deeper into the R language
  • Using R’s OOP features to create generic functions
  • Tweaking code to run more efficiently
  • Finding and correcting programming errors

Previous chapters introduced various topics that are important for application development, including data types (section 2.2), control flow (section 5.4), and function creation (section 5.5). This chapter will review these aspects of R as a programming language from a more advanced and detailed perspective. By the end of this chapter, you’ll have a better idea of how the R language works, which will help you create your own functions, and ultimately, your own packages.

We’ll start with a review of objects, data types, and control flow before moving on to details of function creation, including the role of scope and environments. The chapter introduces R’s approach to object-oriented programming and discusses the creation of generic functions. Finally, we’ll go over tips for writing efficient code-generating and debugging applications. Mastery of these topics will help you understand the code in other people’s applications and aid you in creating new applications. In chapter 22, you’ll have an opportunity to put these skills into practice by creating a useful package from start to finish.

20.1 A review of the language

20.1.1 Data types

20.1.2 Control structures

20.1.3 Creating functions

20.2 Working with environments

20.3 Non-standard evaluation

20.4 Object-oriented programming

20.4.1 Generic functions

20.4.2 Limitations of the S3 model

20.5 Writing efficient code

20.5.1 Efficient data input

20.5.2 Vectorization

20.5.3 Correctly sizing objects

20.5.4 Parallelization

20.6 Debugging