Tsallaka zuwa abun ciki
Justice Tech School

R Scope and Closures

Variables inside a function are local. Functions can read outer variables, and <<- changes an outer one. A function created inside another keeps access to its variables.

Example
make_counter <- function() { n <- 0; function() { n <<- n + 1; n } }
c1 <- make_counter()
c1(); print(c1())

Exercise

Assign to an outer variable.

n  n + 1

Quiz Test

What does <<- do?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.