2012-03-16 23 views
27

私はコマンドラインでのみlinuxでRを使用しています。Rの定義済み変数のリスト

しばらくしてプロジェクトに戻ってきて、私が使用した変数名を忘れてしまいました.Rのコマンド履歴にはそれらが含まれていません。

私は、すべてのユーザー定義変数をリストするコマンドがあることを覚えているようですが、それが何であるかを覚えておらず、Web上で見つけることができません。

ユーザー定義変数をすべてRにリストするにはどうすればよいですか?ヘルプページから

答えて

40

ls()

‘ls’ and ‘objects’ return a vector of character strings giving the 
names of the objects in the specified environment. When invoked 
with no argument at the top level prompt, ‘ls’ shows what data 
sets and functions a user has defined. When invoked with no 
argument inside a function, ‘ls’ returns the names of the 
functions local variables. This is useful in conjunction with 
‘browser’. 

編集:私はあなたがそうでなければ、ドットで始まる変数を

ls(all.names = TRUE) 

を使用する必要がありますすべての変数をリストすることに注意すべきですリストには表示されません。

+0

数字です。私はそれを推測すべきです。ありがとう! –

+0

@Dasonこれと 'objects()'の違いはありますか? – frank

+1

@ frank No. https://github.com/wch/r-source/blob/51a4342f1b93d85bf6750cded97d8fa013984f46/src/library/base/R/attach.R#L200 – Dason

2

あなたは、このリンクをチェックアウトすることもできます。

Tricks to manage the available memory in an R session

それが自分のメモリ使用量と一緒にオブジェクトを表示するための素晴らしい機能を持っています。それはRのための私のスタートアップスクリプトの一部です。

# Written by Dirk Eddelbuettel found here: https://stackoverflow.com/questions/1358003/tricks-to-manage-the-available-memory-in-an-r-session 

# improved list of objects 

.ls.objects <- function (pos = 1, pattern, order.by, 
         decreasing=FALSE, head=FALSE, n=5) { 
    napply <- function(names, fn) sapply(names, function(x) 
             fn(get(x, pos = pos))) 
    names <- ls(pos = pos, pattern = pattern) 
    obj.class <- napply(names, function(x) as.character(class(x))[1]) 
    obj.mode <- napply(names, mode) 
    obj.type <- ifelse(is.na(obj.class), obj.mode, obj.class) 
    obj.size <- napply(names, object.size) 
    obj.dim <- t(napply(names, function(x) 
         as.numeric(dim(x))[1:2])) 
    vec <- is.na(obj.dim)[, 1] & (obj.type != "function") 
    obj.dim[vec, 1] <- napply(names, length)[vec] 
    out <- data.frame(obj.type, obj.size, obj.dim) 
    names(out) <- c("Type", "Size", "Rows", "Columns") 
    if (!missing(order.by)) 
     out <- out[order(out[[order.by]], decreasing=decreasing), ] 
    if (head) 
     out <- head(out, n) 
    out 
} 
# shorthand 
lsos <- function(..., n=10) { 
    .ls.objects(..., order.by="Size", decreasing=TRUE, head=TRUE, n=n) 
} 
関連する問題