2012-03-16 49 views
27

Tôi đang sử dụng R trong linux, chỉ dòng lệnh.Danh sách các Biến được Xác định trong R

Quay lại dự án sau một thời gian, tôi đã quên tên biến mà tôi đã sử dụng và lịch sử lệnh R không chứa chúng.

Tôi dường như nhớ có một lệnh liệt kê tất cả các biến do người dùng xác định, nhưng không nhớ nó là gì và không thể tìm thấy nó trên web.

Làm cách nào để liệt kê tất cả các biến do người dùng xác định trong R?

Trả lời

40

ls()

Từ trang trợ giúp:

‘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’. 

Chỉnh sửa: Tôi nên lưu ý rằng để liệt kê tất cả các biến bạn sẽ cần phải sử dụng

ls(all.names = TRUE) 

khác biến bắt đầu với một dấu chấm sẽ không hiển thị trong danh sách.

+0

Hình. Tôi nên đoán rằng một. Cảm ơn! –

+0

@ Dason Có sự khác biệt nào giữa điều này và 'đối tượng()' không? – frank

+1

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

2

Bạn có thể muốn kiểm tra liên kết này:

Tricks to manage the available memory in an R session

Nó có một chức năng tuyệt vời để hiển thị đối tượng cùng với việc sử dụng bộ nhớ của họ. Đó là một phần của tập lệnh khởi động của tôi cho 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) 
} 
Các vấn đề liên quan