Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .Rprofile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#### -- Packrat Autoloader (version 0.4.8-1) -- ####
source("packrat/init.R")
#### -- End Packrat Autoloader -- ####
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,5 @@ target/
#Ipython Notebook
.ipynb_checkpoints

packrat/lib*/
packrat/src/
217 changes: 217 additions & 0 deletions packrat/init.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
local({

## Helper function to get the path to the library directory for a
## given packrat project.
getPackratLibDir <- function(projDir = NULL) {
path <- file.path("packrat", "lib", R.version$platform, getRversion())

if (!is.null(projDir)) {

## Strip trailing slashes if necessary
projDir <- sub("/+$", "", projDir)

## Only prepend path if different from current working dir
if (!identical(normalizePath(projDir), normalizePath(getwd())))
path <- file.path(projDir, path)
}

path
}

## Ensure that we set the packrat library directory relative to the
## project directory. Normally, this should be the working directory,
## but we also use '.rs.getProjectDirectory()' if necessary (e.g. we're
## rebuilding a project while within a separate directory)
libDir <- if (exists(".rs.getProjectDirectory"))
getPackratLibDir(.rs.getProjectDirectory())
else
getPackratLibDir()

## Unload packrat in case it's loaded -- this ensures packrat _must_ be
## loaded from the private library. Note that `requireNamespace` will
## succeed if the package is already loaded, regardless of lib.loc!
if ("packrat" %in% loadedNamespaces())
try(unloadNamespace("packrat"), silent = TRUE)

if (suppressWarnings(requireNamespace("packrat", quietly = TRUE, lib.loc = libDir))) {

# Check 'print.banner.on.startup' -- when NA and RStudio, don't print
print.banner <- packrat::get_opts("print.banner.on.startup")
if (print.banner == "auto" && is.na(Sys.getenv("RSTUDIO", unset = NA))) {
print.banner <- TRUE
} else {
print.banner <- FALSE
}
return(packrat::on(print.banner = print.banner))
}

## Escape hatch to allow RStudio to handle bootstrapping. This
## enables RStudio to provide print output when automagically
## restoring a project from a bundle on load.
if (!is.na(Sys.getenv("RSTUDIO", unset = NA)) &&
is.na(Sys.getenv("RSTUDIO_PACKRAT_BOOTSTRAP", unset = NA))) {
Sys.setenv("RSTUDIO_PACKRAT_BOOTSTRAP" = "1")
setHook("rstudio.sessionInit", function(...) {
# Ensure that, on sourcing 'packrat/init.R', we are
# within the project root directory
if (exists(".rs.getProjectDirectory")) {
owd <- getwd()
setwd(.rs.getProjectDirectory())
on.exit(setwd(owd), add = TRUE)
}
source("packrat/init.R")
})
return(invisible(NULL))
}

## Bootstrapping -- only performed in interactive contexts,
## or when explicitly asked for on the command line
if (interactive() || "--bootstrap-packrat" %in% commandArgs(TRUE)) {

message("Packrat is not installed in the local library -- ",
"attempting to bootstrap an installation...")

## We need utils for the following to succeed -- there are calls to functions
## in 'restore' that are contained within utils. utils gets loaded at the
## end of start-up anyhow, so this should be fine
library("utils", character.only = TRUE)

## Install packrat into local project library
packratSrcPath <- list.files(full.names = TRUE,
file.path("packrat", "src", "packrat")
)

## No packrat tarballs available locally -- try some other means of installation
if (!length(packratSrcPath)) {

message("> No source tarball of packrat available locally")

## There are no packrat sources available -- try using a version of
## packrat installed in the user library to bootstrap
if (requireNamespace("packrat", quietly = TRUE) && packageVersion("packrat") >= "0.2.0.99") {
message("> Using user-library packrat (",
packageVersion("packrat"),
") to bootstrap this project")
}

## Couldn't find a user-local packrat -- try finding and using devtools
## to install
else if (requireNamespace("devtools", quietly = TRUE)) {
message("> Attempting to use devtools::install_github to install ",
"a temporary version of packrat")
library(stats) ## for setNames
devtools::install_github("rstudio/packrat")
}

## Try downloading packrat from CRAN if available
else if ("packrat" %in% rownames(available.packages())) {
message("> Installing packrat from CRAN")
install.packages("packrat")
}

## Fail -- couldn't find an appropriate means of installing packrat
else {
stop("Could not automatically bootstrap packrat -- try running ",
"\"'install.packages('devtools'); devtools::install_github('rstudio/packrat')\"",
"and restarting R to bootstrap packrat.")
}

# Restore the project, unload the temporary packrat, and load the private packrat
packrat::restore(prompt = FALSE, restart = TRUE)

## This code path only reached if we didn't restart earlier
unloadNamespace("packrat")
requireNamespace("packrat", lib.loc = libDir, quietly = TRUE)
return(packrat::on())

}

## Multiple packrat tarballs available locally -- try to choose one
## TODO: read lock file and infer most appropriate from there; low priority because
## after bootstrapping packrat a restore should do the right thing
if (length(packratSrcPath) > 1) {
warning("Multiple versions of packrat available in the source directory;",
"using packrat source:\n- ", shQuote(packratSrcPath))
packratSrcPath <- packratSrcPath[[1]]
}


lib <- file.path("packrat", "lib", R.version$platform, getRversion())
if (!file.exists(lib)) {
dir.create(lib, recursive = TRUE)
}
lib <- normalizePath(lib, winslash = "/")

message("> Installing packrat into project private library:")
message("- ", shQuote(lib))

surround <- function(x, with) {
if (!length(x)) return(character())
paste0(with, x, with)
}

## The following is performed because a regular install.packages call can fail
peq <- function(x, y) paste(x, y, sep = " = ")
installArgs <- c(
peq("pkgs", surround(packratSrcPath, with = "'")),
peq("lib", surround(lib, with = "'")),
peq("repos", "NULL"),
peq("type", surround("source", with = "'"))
)
installCmd <- paste(sep = "",
"utils::install.packages(",
paste(installArgs, collapse = ", "),
")")

fullCmd <- paste(
surround(file.path(R.home("bin"), "R"), with = "\""),
"--vanilla",
"--slave",
"-e",
surround(installCmd, with = "\"")
)
system(fullCmd)

## Tag the installed packrat so we know it's managed by packrat
## TODO: should this be taking information from the lockfile? this is a bit awkward
## because we're taking an un-annotated packrat source tarball and simply assuming it's now
## an 'installed from source' version

## -- InstallAgent -- ##
installAgent <- 'InstallAgent: packrat 0.4.8-1'

## -- InstallSource -- ##
installSource <- 'InstallSource: source'

packratDescPath <- file.path(lib, "packrat", "DESCRIPTION")
DESCRIPTION <- readLines(packratDescPath)
DESCRIPTION <- c(DESCRIPTION, installAgent, installSource)
cat(DESCRIPTION, file = packratDescPath, sep = "\n")

# Otherwise, continue on as normal
message("> Attaching packrat")
library("packrat", character.only = TRUE, lib.loc = lib)

message("> Restoring library")
restore(restart = FALSE)

# If the environment allows us to restart, do so with a call to restore
restart <- getOption("restart")
if (!is.null(restart)) {
message("> Packrat bootstrap successfully completed. ",
"Restarting R and entering packrat mode...")
return(restart())
}

# Callers (source-erers) can define this hidden variable to make sure we don't enter packrat mode
# Primarily useful for testing
if (!exists(".__DONT_ENTER_PACKRAT_MODE__.") && interactive()) {
message("> Packrat bootstrap successfully completed. Entering packrat mode...")
packrat::on()
}

Sys.unsetenv("RSTUDIO_PACKRAT_BOOTSTRAP")

}

})
51 changes: 51 additions & 0 deletions packrat/packrat.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
PackratFormat: 1.4
PackratVersion: 0.4.8.1
RVersion: 3.3.2
Repos: CRAN=http://cran.rstudio.com/

Package: R6
Source: CRAN
Version: 2.2.0
Hash: 712773a3439bd32db5c40938e1a9aac2

Package: crayon
Source: CRAN
Version: 1.3.2
Hash: 576a9d297a567d6a5ebd164ca5221590

Package: digest
Source: CRAN
Version: 0.6.10
Hash: 87165608ca8aeb0958835c3c5b52b6b7

Package: magrittr
Source: CRAN
Version: 1.5
Hash: bdc4d48c3135e8f3b399536ddf160df4

Package: packrat
Source: CRAN
Version: 0.4.8-1
Hash: 6ad605ba7b4b476d84be6632393f5765

Package: praise
Source: CRAN
Version: 1.0.0
Hash: 77da8f1df873a4b91e5c4a68fe2fb1b6

Package: stringi
Source: CRAN
Version: 1.1.2
Hash: 1ad6b1cc11d982ac233bf2fa2f7c9684

Package: stringr
Source: CRAN
Version: 1.1.0
Hash: 4d21d0063b37a17ea18eaec6acd29bf3
Requires: magrittr, stringi

Package: testthat
Source: CRAN
Version: 1.0.2
Hash: 88d5291104227f9dc2e7c7c1d0eb6c74
Requires: R6, crayon, digest, magrittr, praise
15 changes: 15 additions & 0 deletions packrat/packrat.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
auto.snapshot: TRUE
use.cache: FALSE
print.banner.on.startup: auto
vcs.ignore.lib: TRUE
vcs.ignore.src: FALSE
external.packages:
local.repos:
load.external.packages.on.startup: TRUE
ignored.packages:
quiet.package.installation: TRUE
snapshot.recommended.packages: FALSE
snapshot.fields:
Imports
Depends
LinkingTo
6 changes: 6 additions & 0 deletions tddc/build_aarrrr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from string import Template

d = { 'input_filename':"in.R", 'output_filename':"out.R" }
f = open( 'r/main.R' )
s = Template( f.read() )
print(s.substitute(d))
37 changes: 37 additions & 0 deletions tddc/r/main.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Driver program
library(stringr)
source("clean_methods.R")

clean_columns <- function(input_file,output_file){

reader <- file(input_file, open = 'rt')
writer <- file(output_file, open = 'wt')
num_col = 0

column_names <- scan(reader,what=character(),nlines=1,sep=',',skip=0,quiet=TRUE)
num_col = length(column_names)
column_names <- vapply(column_names,FUN=function(x)
if(str_detect(x,',')) paste0('\"',x,'\"') else x,FUN.VALUE=character(1),USE.NAMES=FALSE)
write(column_names,file=writer,ncolumns=num_col,sep=',')

column_methods = list()
for (i in 1:num_col) {
column_methods[i] = paste0("clean_col_",i)
}

while (length(row <- scan(reader,what=character(),nlines=1,sep=',',skip=0,quiet=TRUE)) > 0 ){
cleaned_row = character()
for (i in 1:num_col) {
cleaned_row[i] = get(column_methods[[i]])(row[i])
if(str_detect(cleaned_row[i],",")) cleaned_row[i] <- paste0('\"',cleaned_row[i],'\"')
}
write(cleaned_row,file=writer,ncolumns=num_col,sep=',')
}

close(reader)
close(writer)
}

input_file <- $input_filename
output_file <- $output_filename
clean_columns(input_file,output_file)