Skip to content

Instantly share code, notes, and snippets.

@seeRead
Created September 19, 2012 16:00
Show Gist options
  • Save seeRead/3750487 to your computer and use it in GitHub Desktop.
Save seeRead/3750487 to your computer and use it in GitHub Desktop.
# http://stackoverflow.com/questions/1189759/expert-r-users-whats-in-your-rprofile
#set cran repo
r <- getOption("repos") # hard code the US repo for CRAN
r["CRAN"] <- "http://cran.us.r-project.org"
options(repos = r)
rm(r)
#persistent RHistory
.Last <- function() {
if (!any(commandArgs()=='--no-readline') && interactive()){
require(utils)
try(savehistory(Sys.getenv("R_HISTFILE")))
}
}
#terminal width
#tryCatch(
#{options(
#width = as.integer(Sys.getenv("COLUMNS")))},
#error = function(err) {
#write("Can't get your terminal width. Put ``export COLUMNS'' in your \
#.bashrc. Or something. Setting width to 120 chars",
#stderr());
#options(width=120)}
#)
#memory management
# 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.prettysize <- napply(names, function(x) {
capture.output(print(object.size(x), units = "auto")) })
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.prettysize, obj.dim)
names(out) <- c("Type", "Size", "PrettySize", "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)
}
# aliases
s <- base::summary;
h <- utils::head;
n <- base::names;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment