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 8. Inside R

In the previous chapters, you learned the basics of R programming language, and understood the usage of vectors, matrices, lists, and data frames to represent data in different shapes. You also saw how we can use the built-in functions to solve simple problems. However, simply knowing these features does not help you solve every problem. Real-world data analysis usually involves careful and detailed transformation and aggregation of data, which can be done with a good variety of functions, whether they are built-in or provided by extension packages.

To best use these functions rather than let them confuse you with unexpected results, you need a basic but concrete understanding of how R functions work. In this chapter, we will cover the following topics:

  • Lazy evaluation

  • Copy-on-modify mechanism

  • Lexical scoping

  • Environments

If you understand these concepts and their roles in the code, most R code should appear highly predictable to you, which means higher productivity in both...

Understanding lazy evaluation


A big part of understanding how R works can be done by figuring out how R functions work. After going through the previous chapters, you should know the most commonly used basic functions. However, you may still be confused about their exact behavior. Suppose we create the following function:

test0 <- function(x, y) {
  if (x > 0) x else y
} 

The function is somewhat special because y seems to be needed only when x is greater than zero. What if we only supply a positive number to x and ignore y? Will the function fail because we don't supply every argument in its definition? Let's find out by calling the following function:

test0(1)
## [1] 1 

The function works without y being supplied. It looks like we are not required to supply the values to all arguments when we call a function but only to those that are needed. If we call test0 with a negative number, y is needed:

test0(-1)
## Error in test0(-1): argument "y" is missing,...

Understanding the copy-on-modify mechanism


In the previous section, we showed how lazy evaluation works and how it may help save computing time and working memory by avoiding unnecessary evaluation of function arguments. In this section, I will show you an important feature of R that makes it safer to work with data. Suppose we create a simple numeric vector x1:

x1 <- c(1, 2, 3) 

Then, we assign the value of x1 to x2:

x2 <- x1 

Now, x1 and x2 have exactly the same value. What if we modify an element in one of the two vectors? Will both vectors change?

x1[1] <- 0
x1
## [1] 0 2 3
x2
## [1] 1 2 3 

The output shows that when x1 is changed, x2 will remain unchanged. You may guess that the assignment automatically copies the value and makes the new variable point to the copy of the data instead of the original data. Let's use tracemem() to track the footprint of the data in memory.

Let's reset the vectors and conduct an experiment by tracing the memory addresses...

Understanding lexical scoping


In the previous section, we introduced the copy-on-modify mechanism. The examples demonstrated two cases in which this mechanism happens. When an object has multiple names or is passed as an argument to a function, modifying it will cause the object to be copied, and it is the copied version that is actually modified.

To modify an object outside a function, we introduced the use of <<-, which finds the variable outside the function first and modifies that object instead of copying one locally. This leads to an important idea that a function has inside and outside. Inside a function, we can somehow refer to variables and functions outside.

For example, the following function uses two outside variables:

start_num <- 1
end_num <- 10
fun1 <- function(x) {
  c(start_num, x, end_num)
} 

We first create two variables and define a function called fun1. The function simply puts together start_num, argument x, and end_num into a new...

Understanding how an environment works


In the previous sections, you learned about lazy evaluation, copy-on-modify, and lexical scoping. These mechanisms are highly related to a type of object called environment. In fact, lexical scoping is enabled exactly by the environment. Although environments look quite similar to lists, they are indeed fundamentally different in several aspects. In the following sections, we will get to know the behavior of environment objects by creating and manipulating them, and see the way its structure determines how R functions work.

Knowing the environment object

An environment is an object consisting of a set of names and has a parent environment. Each name (also known as a symbol or variable) points to an object. When we look up a symbol in an environment, it will search the set of symbols and return the object the symbol points to if it exists in the environment. Otherwise, it will continue to look up its parent environment. The following diagram illustrates...

Summary


In this chapter, we went inside R and learned how R functions basically work. More specifically, you learned lazy evaluation, copy-on-modify, lexical scoping, and how environments work to allow these mechanisms. Having a concrete understanding of how R code is run not only helps you write the correct code but also makes it easier to find bugs from unexpected results.

In the next chapter, we will build on top of the foundation laid in this chapter. You will learn the basics of metaprogramming, which enables powerful features of interactive analysis.

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 $15.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