Reader small image

You're reading from  Learning R Programming

Product typeBook
Published inOct 2016
Reading LevelBeginner
PublisherPackt
ISBN-139781785889776
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Kun Ren
Kun Ren
author image
Kun Ren

Kun Ren has used R for nearly 4 years in quantitative trading, along with C++ and C#, and he has worked very intensively (more than 8-10 hours every day) on useful R packages that the community does not offer yet. He contributes to packages developed by other authors and reports issues to make things work better. He is also a frequent speaker at R conferences in China and has given multiple talks. Kun also has a great social media presence. Additionally, he has substantially contributed to various projects, which is evident from his GitHub account: https://github.com/renkun-ken https://cn.linkedin.com/in/kun-ren-76027530 http://renkun.me/ http://renkun.me/formattable/ http://renkun.me/pipeR/ http://renkun.me/rlist/
Read more about Kun Ren

Right arrow

Chapter 9.  Metaprogramming

In the previous chapter, you learned about the structure and features of an environment and also learned how to create and access an environment. Environment plays an important role in lazy evaluation, copy-on-modify, and lexical scoping, which are enabled by the environments associated with a function when it is created and called.

Now that we have a solid understanding of how functions work, we will go further in this chapter by learning to work with functions in more advanced forms. You will learn the metaprogramming facilities that make R flexible in interactive analysis. More specifically, we will cover the following topics in this chapter:

  • Functional programming: closures and higher-order functions

  • Computing on language with language objects

  • Understanding non-standard evaluation

Understanding functional programming


In the previous chapter, you learned the behavior of a function in detail, including when an argument is evaluated (lazy evaluation), what happens when we try to modify an argument (copy-on-modify), and where to look for variables not defined within the function (lexical scoping). These technical terms that describe the behaviors may look more difficult than they actually are. In the following sections, you will learn about two types of functions: functions that are defined in functions and functions that work with other functions.

Creating and using closures

A function defined in a function is called a closure_. It is special because in the function body of the closure, not only the local arguments but also the variables created in the parent function are also available.

For example, suppose we have the following function:

add <- function(x, y) {
  x + y
} 

This function has two arguments. Each time we call add(), we should supply two arguments. If we...

Computing on language


In the previous section, we introduced the functional programming facilities in R. You learned that functions are just another type of object we can pass around. When we create a new function, say fun, the environment we create will be associated with the function. This environment is called the enclosing environment of the function, which can be accessed via environment(fun). Each time we call the function, a new executing environment that contains the unevaluated arguments (promises) will be created to host the execution of the function, which enables lazy evaluation. The parent of the executing environment is the enclosing environment of the function, which enables lexical scoping.

Functional programming allows us to write code in higher level of abstraction. Metaprogramming goes even further. It allows us to tweak the language itself and make certain language constructs easier to use in a certain scenario. Some popular R packages use metaprogramming in their functions...

Summary


In this chapter, you learned about the idea and usage of functional programming, including closures and higher order functions. We went further by digging into the metaprogramming facilities, including language objects, evaluation functions, formula, and the implementation of dynamic scoping to ensure user-input expressions are correctly handled when we customize the evaluation behavior. Since a number of popular packages use metaprogramming and non-standard evaluation to make interactive analysis easier, it is important to understand how it works so that we can be more confident to predict and debug the code.

In the next chapter, we will walk into another infrastructure of R: the object-oriented programming systems. You will learn the basic idea of object-oriented programming, how this idea is implemented in R, and how it can be useful. More specifically, we will begin with the looser S3 system, cover the stricter system S4 which offers a richer set of features, and introduce the...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learning R Programming
Published in: Oct 2016Publisher: PacktISBN-13: 9781785889776
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Kun Ren

Kun Ren has used R for nearly 4 years in quantitative trading, along with C++ and C#, and he has worked very intensively (more than 8-10 hours every day) on useful R packages that the community does not offer yet. He contributes to packages developed by other authors and reports issues to make things work better. He is also a frequent speaker at R conferences in China and has given multiple talks. Kun also has a great social media presence. Additionally, he has substantially contributed to various projects, which is evident from his GitHub account: https://github.com/renkun-ken https://cn.linkedin.com/in/kun-ren-76027530 http://renkun.me/ http://renkun.me/formattable/ http://renkun.me/pipeR/ http://renkun.me/rlist/
Read more about Kun Ren