]> code.communitydata.science - rises_declines_wikia_code.git/blob - RCommunityData/R/load_if_missing.R
Initial commit
[rises_declines_wikia_code.git] / RCommunityData / R / load_if_missing.R
1 # Community Data Science Collective R Utilities
2 #
3 # Copyright (c) 2010-2016 Benjamin Mako Hill and Aaron Shaw 
4 # mako@atdot.cc, aaronshaw@northwestern.edu
5
6 # load a file if a variable is missing
7 load.if.missing <- function (var.name, file.name) {
8   if (!exists(var.name)) {
9     load(file.name, parent.frame())
10
11     # check to see if we're dealing with a data.table because, if we
12     # are, we need to do some nasty back and forth
13     if (class(eval(as.name(var.name)))[1] == "data.table") {
14
15       # gnarly function that loads resorts things within the parent
16       # frame to get around the bug in data.table
17       assign(var.name,
18              data.table(as.data.frame(eval(as.name(var.name))),
19                         key=attr(eval(as.name(var.name)), "sorted")),
20              parent.frame())
21     }
22   }
23 }
24

Community Data Science Collective || Want to submit a patch?