One topic that's important to discuss in R is

a question of, you know, when a function sees a

symbol in its body and it's executing inside the R

environment, how does it assign a value to that symbol?

So for example, take a look at this, this function here that I've defined called lm.

So lm here is a function which takes

its argument x, and it multiplies it times itself.

So you can think of it as squaring the,

squaring the input.

Now, there's already a function in R called lm,

so I've created an, a function here also called lm, so when I call lm

somewhere else in R, maybe in another function or something like that

how does R know what value to assign to the symbol lm?

So it sees the symbol lm, and how does it know

whether to call the function that I just defined here or the

lm function that's in the stats package that's

used to model, you know, to fit linear models.

And so the, the idea that R needs to bind a value to a symbol.

So in this case the, in the previous slide, the symbol

was lm, and it needs to bind a value to it.

And the value is going to be a function of some sort.

It's either going to be my function or it's

going to be the function in the stats package.

And so when r tries to bind a value to a symbol what

it does is, it searches through a

series of environments to find the appropriate value.

So environments are kind of, you can think of them

as lists of objects and values or symbols and values.

And so, when you're working on the command line, and

you need to retrieve a value of an R object basically,

what happens is, the first thing that happens is, you search

the global environment for a symbol name matching the one requested.

And so for the global environment, it's just

your workspace, and it consists of all the

things that you've defined or loaded into R.

And so if there's a symbol there that

matches the name of the one that you're requesting

then it will take that symbol and, and

then retrieve the value that's associated with that symbol.

So in this case, I've defined lm in my global environment.

And so, because that exists, if I'm working the

command line, when I call lm, it's going to

find that object first.

So if there, if there's no match in the global environment, then what happens is,

the, R will search the namespaces of each of the packages on the search list.

So if you look at.

So the search list consists of all the R packages that are currently loaded into R.

And so you'll see that there, there's an order to the search list.

So, and it goes starts at the first element, which is the global environment.

That's number one

on the search list.

That's always number one on the search list.

Now, you can see, second on the search list is

the stats package, the graphics package, the GR devices package.

All the way down at the very is the base package, okay?

And so, somewhere in this list of packages R

is going to look for a function called lm.

And, of course, if it's not in the global environment, then it

will eventually find it in the stats package which is the function

that's used to fit linear models.