7

In the code below, LETTERS and letters are global, or in the global search path and reachable through another package (same thing!)

> LETTERS
 [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
[20] "T" "U" "V" "W" "X" "Y" "Z"
> letters
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t" "u" "v" "w" "x" "y" "z"
> ls()
character(0)
> a <- "1 2 3"
> ls()
[1] "a"
> rm(a)
> ls()
character(0)
> 

mda
  • 1,158
  • 14
  • 18
  • Wow, a lot of R programmers in the wild today. :-) – mda Sep 19 '12 at 16:00
  • 5
    Do note that these are **not** global, just **exported** from the `base` namespace and hence accessible from the global workspace. – Gavin Simpson Sep 19 '12 at 16:01
  • Hrmmm... Do all languages consider exports not visible...I think not....consider bash. So "global" in the general compsci theoretical definition. Of course R has specific R lingo. I want to learn it. :-) – mda Sep 19 '12 at 16:04
  • The apropos() and find() functions look promising. I would still like to understand where R is storing its various globals and how it classifies them. – mda Sep 19 '12 at 16:13
  • 1
    @mda: as others have said, they're not "global" variables. They're simply objects found by looking through the objects/packages attached to the search path. They're stored/found by the same means as R stores/finds (non-method) functions. – Joshua Ulrich Sep 19 '12 at 16:19
  • I understand the search path concept. What I'm asking is, what's the easiest way to display everything polluting my namespaces? :-) – mda Sep 19 '12 at 16:28
  • 3
    @mda: I don't think you understand. You would benefit from at least skimming the manuals. You don't have a namespace when you're using R interactively. Only packages have namespaces, and they only contain objects created therein, or objects you import from other namespaces. – Joshua Ulrich Sep 19 '12 at 16:35
  • This is theoretical hair-splitting. I have a scope -- if you don't want to call it a namespace, it's just hair-splitting. But thanks everyone for the great feedback!!! Aside: The Art of R Programming (even the preprint) is a better starting point than the manuals IMHO. – mda Sep 19 '12 at 16:41
  • 5
    @mda: I'm not splitting hairs. You said you wanted to understand the R terminology, so I'm explaining it to you. Scope from the command line is different from scope when writing a package. – Joshua Ulrich Sep 19 '12 at 16:50

5 Answers5

11

For the same reason it doesn't list all the exported functions in all the attached packages (from ?ls):

By default, the environment of the call to 'ls' or 'objects' is used.

Specify the environment that contains LETTERS and ls will print it's name.

# LETTERS is in there somewhere...
sapply(search(),ls)
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
7

If you read ?LETTERS you will note that these are constants and not data objects. That page also indicates that these constants are located in the base namespace. To list them we have to tell ls() to look in that namespace:

> ls(name = "package:base", pattern = "LETTERS")
[1] "LETTERS"
> ls(name = "package:base", pattern = "letters")
[1] "letters"
Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
  • 1
    Good lead! :-) It's funny in R where one can find pages like ?Extract and ?Constants .... is there a listing of all top-level meta-manpages (from the base package)? – mda Sep 19 '12 at 16:21
  • 2
    (The `data` function just loads datasets from packages.) I take credit for badgering R-Core into making `?Constants` work at the console line. It was not too long ago that despite the help page being entitled "Constants", that search would not succeed. `?Extract` has always worked. Another meta-search that is of good use is `?Devices`. Someone might be able to construct a list of help page titles that were not also function names, but it's above my pay grade. – IRTFM Sep 19 '12 at 16:33
6
apropos("letters",where=TRUE)
        9         9 
"letters" "LETTERS" 

indicates that they are at position 9 in the search list, which is,

search()
[1] ".GlobalEnv"        "package:stats"     "package:graphics" 
[4] "package:grDevices" "package:utils"     "package:datasets" 
[7] "package:methods"   "Autoloads"         "package:base"

the base package.

James
  • 65,548
  • 14
  • 155
  • 193
6

If you want to see the objects of a particular mode and have a regex search pattern in mind, you can go exploring with apropos. I was a bit nervous about how much I would find so I first checked this effort's length which only was 30. Here are all the character vectors found in my workspace at the moment. Notice that both "letters" and LETTERS" do show up.

apropos(what="^", mode="character")
 [1] ".Depends"             ".Depends"             ".Depends"             ".Depends"            
 [5] ".Depends"             ".Depends"             ".Depends"             ".Depends"            
 [9] ".Depends"             ".Depends"             ".Depends"             ".Depends"            
[13] ".Device"              ".Firstlib_as_onLoad"  ".knownS3Generics"     ".Library"            
[17] ".Library.site"        ".S3PrimitiveGenerics" "blues9"               "letters"             
[21] "LETTERS"              "month.abb"            "month.name"           "p.adjust.methods"    
[25] "R.version.string"     "sas.get.macro"        "state.abb"            "state.name"          
[29] "tm"   

If you did this with a fresh session you would not get as many ".Depends". Many of the other built-ins do appear here, but "pi" is missing because it's not of character-mode. If you go looking for "pi" in my machine its position is 25 which isn't very meaningful until you use search():

> search()[as.numeric(names(apropos(what="^pi", mode="numeric",where=1)))]
[1] "package:base"
# Removing the numeric restriction
> search()[as.numeric(names(apropos(what="^pi",where=1)))]
 [1] "package:base"      "package:utils"     "package:lubridate" "package:grDevices" "package:graphics" 
 [6] "package:graphics"  "package:MASS"      "package:MASS"      "package:MASS"      "package:base"     

So all of those packages have some-object (functions mostly) that begin with "pi". The numeric position will vary with the number of packages loaded, since recently loaded packages having lower number will push the search position of "base" higher.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
3

Because they are builtin data objects. Do a normal assignment to a normal variable and you will see it:

R> myLETTERS <- LETTERS
R> ls()
[1] "myLETTERS"
R> 

Running

R> data()

shows you all available data sets. See help(data) for more, eg how to look for data in specific environemnts rather than the global one.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725