I recently came through this concept of Lexical Scoping in R via the below two functions:

1.**makeVector:**

makeVector <- function(x = numeric()) {

m <- NULL

set <- function(y) {

x <<- y

m <<- NULL

}

get <- function() x

setmean <- function(mean) m <<- mean

getmean <- function() m

list(set = set, get = get,

setmean = setmean,

getmean = getmean)

}

This function is a special function used to 1) Set & get the value of the vector 2)Set & get the value of the mean of the vector.

2.**cachemean:**

cachemean <- function(x, …) {

m <- x$getmean()

if(!is.null(m)) {

message(“getting cached data”)

return(m)

}

data <- x$get()

m <- mean(data, …)

x$setmean(m)

m

}

This function calculates the mean of the vector.However it first finds out if the mean has already been calculated,in which case it returns the cached value of the mean.

I am not being able to understand the flow completely.

Any help is appreciated.!!

Note:It is a part of the R programming course in Coursera.