R how to restrict the names that are in scope to those I create explicitly? R how to restrict the names that are in scope to those I create explicitly? r r

R how to restrict the names that are in scope to those I create explicitly?


Not sure if "scope" means the same thing in R as it may to other languages. R uses "environments" (see http://adv-r.had.co.nz/Environments.html for detailed explanation). Your scope in R includes all environments that are loaded, and as you have discovered, the user doesn't explicitly control every environment that is loaded.

For example,

ls()

lists the objects in your default environment '.GlobalEnv'

search()

lists the currently loaded environments.

ls(name='package.stats')

In default R installations, 'package:stats' is one of the environments loaded on startup.

By default, everything you create is stored in the global environment.

ls(name='.GlobalEnv')

You can explicitly reference objects you create by referencing their environment with the $ syntax.

x <- c(1,2,3).GlobalEnv$x