Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
If I run ls() in R, it shows the variables (including functions) in my workspace. But there are other variables visible too, e.g. functions from packages like ls itself. And if I write a function...
#1: Initial revision
How does scoping work in R?
If I run `ls()` in R, it shows the variables (including functions) in my workspace. But there are other variables visible too, e.g. functions from packages like `ls` itself. And if I write a function myself, all of those are still visible as well as local variables from that function, but not variables in other functions. For example, ``` r a <- 1 f <- function() { b <- 2 print(a) print(b) } g <- function() { print(a) print(b) } ls() #> [1] "a" "f" "g" f() #> [1] 1 #> [1] 2 g() #> [1] 1 #> Error in g(): object 'b' not found ``` <sup>Created on 2023-10-01 with [reprex v2.0.2](https://reprex.tidyverse.org)</sup> What are the actual rules for scoping?