在下面的代码中,字母和字母是全局的,或者在全局搜索路径中,可以通过另一个包访问(同样的事情!
>信件 [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">字母 [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()字符(0)> <- "1 2 3"> ls()[1] "A"> 马币(a)> ls()字符(0)>
出于同样的原因,它没有列出所有附加包中的所有导出函数(来自?ls
):
默认情况下,使用调用"ls"或"对象"的环境。
指定包含LETTERS
的环境,ls
将打印其名称。
# LETTERS is in there somewhere...
sapply(search(),ls)
如果你读?LETTERS
你会注意到这些是常量而不是数据对象。该页还指示这些常量位于 base
命名空间中。要列出它们,我们必须告诉ls()
查看该命名空间:
> ls(name = "package:base", pattern = "LETTERS")
[1] "LETTERS"
> ls(name = "package:base", pattern = "letters")
[1] "letters"
apropos("letters",where=TRUE)
9 9
"letters" "LETTERS"
表示它们在搜索列表中的位置为 9,即,
search()
[1] ".GlobalEnv" "package:stats" "package:graphics"
[4] "package:grDevices" "package:utils" "package:datasets"
[7] "package:methods" "Autoloads" "package:base"
base
包。
查看特定模式的对象并有一个正则表达式搜索模式,你可以去探索apropos
。我有点担心我会找到多少,所以我首先检查了这项工作的长度,只有 30。以下是目前在我的工作区中找到的所有字符矢量。请注意,"字母"和"字母"都显示出来。
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"
如果你用一个新的会话来做到这一点,你就不会得到那么多的"。视情况而定"。许多其他内置确实出现在这里,但缺少"pi",因为它不是字符模式。如果你在我的机器中寻找"pi",它的位置是 25,这在你使用 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"
所以所有这些包都有一些以"pi"开头的对象(主要是函数)。数字位置将随着加载的包数量而变化,因为最近加载的包数量较少会将"base"的搜索位置推高。
因为它们是内置的数据对象。 对正态变量进行正常赋值,您将看到它:
R> myLETTERS <- LETTERS
R> ls()
[1] "myLETTERS"
R>
运行
R> data()
显示所有可用的数据集。 请参阅help(data)
了解更多信息,例如如何在特定环境中而不是全球环境中查找数据。