Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Scope (computer science)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== R === [[R (programming language)|R]] is a lexically scoped language, unlike other implementations of [[S (programming language)|S]] where the values of free variables are determined by a set of global variables, while in R they are determined by the context in which the function was created.<ref>{{cite web|url=https://cran.r-project.org/doc/FAQ/R-FAQ.html#Lexical-scoping|title=R FAQ|website=cran.r-project.org|access-date=19 March 2018}}</ref> The scope contexts may be accessed using a variety of features (such as <code>parent.frame()</code>) which can simulate the experience of dynamic scope should the programmer desire. There is no block scope: <syntaxhighlight lang="r"> a <- 1 { a <- 2 } message(a) ## 2 </syntaxhighlight> Functions have access to scope they were created in: <syntaxhighlight lang="r"> a <- 1 f <- function() { message(a) } f() ## 1 </syntaxhighlight> Variables created or modified within a function stay there: <syntaxhighlight lang="r"> a <- 1 f <- function() { message(a) a <- 2 message(a) } f() ## 1 ## 2 message(a) ## 1 </syntaxhighlight> Variables created or modified within a function stay there unless assignment to enclosing scope is explicitly requested: <syntaxhighlight lang="r"> a <- 1 f <- function() { message(a) a <<- 2 message(a) } f() ## 1 ## 2 message(a) ## 2 </syntaxhighlight> Although R has lexical scope by default, function scopes can be changed: <syntaxhighlight lang="r"> a <- 1 f <- function() { message(a) } my_env <- new.env() my_env$a <- 2 f() ## 1 environment(f) <- my_env f() ## 2 </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)