Functional Programming in R 4 by Unknown

Functional Programming in R 4 by Unknown

Author:Unknown
Language: eng
Format: epub, pdf


Chapter 4 SCope and CloSureS

Environment Chains, Scope, and

Function Calls

When you call a function in R, you first create an empty environment for that function. That environment is where its parameters will be stored, and any local variables the function assigns to will also go there. Whenever R needs to look up a value, it will look in this environment. If it finds the variable, you get the value in this table. If it doesn’t, R doesn’t just give up, however. It continues the search.

Environments are not only table but chains of tables. Each environment has a “parent” (you can use the parent.env() function to get it), and if a lookup fails for an environment, R will look in the parent instead. If it fails there, it will try the grandparent, and it will continue as long as there are environments.

Consider this code:

f < - function(x) {

sqrt(sum(x + y))

}

x < - 1:10

y < - 1:10

f(x**2)

71

Chapter 4 SCope and CloSureS

Figure 4-2. The local and global environments are connected through a parent pointer

## [1] 20.97618

We define three variables, f, x, and y, in the global environment, where f is a function. Then we call the function, creating a local environment where f’s argument x will be placed; see Figure 4-2.

The x in the local and the global environments refers to different values, and if we look for the value of x inside the function, we get the one in the local environment; see Figure 4-3. The sqrt, sum, and y names used in the function body are not defined in the local environment. If we only had environments as tables, we wouldn’t be able to run this code. The parent environment, however, allows us to find nonlocal variables. When R cannot find y in the local environment, it will go to the parent environment, which is the global environment, and find it there; see Figure 4-4.

The sqrt and sum variables are also not defined in the local environment, so R will also look for them in the parent, but they are not defined in the global environment either. Although I will not look beyond the global environment in the rest of this book, the environment 72

Chapter 4 SCope and CloSureS

chain goes deeper than that. After the global environment, you will find environments that hold names from loaded packages, which is why you can use functions from packages. Once you are through them, you get to the so-called “base environment,” where you find the base R functions (you can get hold of it with the baseenv() function) and then an empty environment that terminates the search. It is in this base environment we find sum and sqrt; see Figure 4-5. The mechanism is the same all the way down, so if you understand how it works between functions and the global environment, you will also understand how environments work everywhere1.

Figure 4-3. Finding local variable x

1 Packages also use the environment system to differentiate between public and private names, but not through a separate mechanism.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Popular ebooks
Enterprise LMS with Adobe Learning Manager by Damien Bruyndonckx(3533)
Building Data Science Solutions with Anaconda by Dan Meador(3444)
Simplify Big Data Analytics with Amazon EMR by Sakti Mishra(2689)
Building Modern CLI Applications in Go by Marian Montagnino(1428)
Getting Started with Forex Trading Using Python by Alex Krishtop(1370)
Simplify Big Data Analytics With Amazon EMR: A Beginner's Guide to Learning and Implementing Amazon EMR for Building Data Analytics Solutions by Sakti Mishra(1076)
Natural Language Processing Fundamentals by Sohom Ghosh(703)
Sebastian Raschka Python Machine Learning Unlock deeper insights into Machine Leaning with this vital guide to cutting-edge predictive analytics by Unknown(541)
+ Excel for Finance and Accounting: Learn how to optimize Excel formulas and functions for financial analysis by Suraj Kumar Lohani(514)
Mastering Data Analysis with Python: A Comprehensive Guide to NumPy, Pandas, and Matplotlib by Rajender Kumar(390)
Statistical Data Analysis Using SAS Intermediate Statistical Methods 2nd Edition by Unknown(357)
Fundamentals of Data Engineering by Joe Reis and Matt Housley(340)
Essential Guide to LLMOps by Ryan Doan;(319)
Data Wrangling Using Pandas, SQL, and Java by Oswald Campesato(304)
Hacking SaaS by Eric Mersch(248)
ChatGPT Millions: Ideas to Generate Your First Million Using ChatGPT and AI by Johnson Omar(233)
MICROSOFT OFFICE 365 FOR BEGINNERS & ADVANCED USERS: THE MOST UPDATED USERG GUIDE TO LEARN MICROSOFT OFFICE 365 (WORD, EXCEL, POWERPOINT, & PUBLISHER) by GIBSON CRYSTAL(225)
Microsoft PowerPoint - BATSA presentation D Crow - web.ppt by denslows(223)
Confident Data Science: Discover The Essential Skills of Data Science by Adam Ross Nelson(206)
Excel 2019 PivotTables: Easy Excel Essentials 2019 - Book 1 by M.L. Humphrey(201)