added r data module
This commit is contained in:
24
R/load_if_missing.R
Normal file
24
R/load_if_missing.R
Normal file
@@ -0,0 +1,24 @@
|
||||
# Community Data Science Collective R Utilities
|
||||
#
|
||||
# Copyright (c) 2010-2016 Benjamin Mako Hill and Aaron Shaw
|
||||
# mako@atdot.cc, aaronshaw@northwestern.edu
|
||||
|
||||
# load a file if a variable is missing
|
||||
load.if.missing <- function (var.name, file.name) {
|
||||
if (!exists(var.name)) {
|
||||
load(file.name, parent.frame())
|
||||
|
||||
# check to see if we're dealing with a data.table because, if we
|
||||
# are, we need to do some nasty back and forth
|
||||
if (class(eval(as.name(var.name)))[1] == "data.table") {
|
||||
|
||||
# gnarly function that loads resorts things within the parent
|
||||
# frame to get around the bug in data.table
|
||||
assign(var.name,
|
||||
data.table(as.data.frame(eval(as.name(var.name))),
|
||||
key=attr(eval(as.name(var.name)), "sorted")),
|
||||
parent.frame())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user