diff options
| author | St33v <st33v@woodenspoon.net> | 2013-05-16 18:15:59 +1000 |
|---|---|---|
| committer | St33v <st33v@woodenspoon.net> | 2013-05-16 18:15:59 +1000 |
| commit | 9e1606fd57a0a755088506956594cb12aa73721a (patch) | |
| tree | f6ec9f7d569d9acddd8e0505dc065af2d3bfdc6a | |
| parent | ec46fe192652f459955f90645d59cae7606c7a85 (diff) | |
Create file settings and cleanup
copied various functions from (my local) sp_fn.R
| -rw-r--r-- | file settings and cleanup | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/file settings and cleanup b/file settings and cleanup new file mode 100644 index 0000000..a26bc0b --- /dev/null +++ b/file settings and cleanup @@ -0,0 +1,54 @@ + +####################################################################### +# clean a path and unlist it (for setDir()) +# - expects Windows backslashes '\' +cleanPath <- function(filename = choose.files()){ + x <- gsub('\\\\','/', filename) # nice easy way to find and clean the path + y <- (gregexpr('/', x)) # vector of positions of / + return(list(path=x, pos=y)) +} + +####################################################################### +# takes CleanPath() output as arg, returns just the filename +justFile <- function(x) { + pos <- unlist(x$pos) + pos <- pos[length(pos)] + fname <- substring(x$path, pos+1, nchar(x$path)) + return(fname) +} + +####################################################################### +# this one allows you to use shortcuts (nicer file picker) +setDir <- function(){ + x <- cleanPath() # list of path + filename + y <- x$pos[length(x$pos)] # just the last one + y <- substring(x$path, 1, y-1) # the directory! + setwd(y) + print(sprintf('working Dir set to %s', getwd())) + return(y) +} + +####################################################################### +# yymmdd date format +yymmdd <- function(thisDate = Sys.Date()){ + return(format(thisDate, '%y%m%d')) +} +####################################################################### +# creates a new subdirectory, defaulting to the current date +# - subdirectory of a directory called 'plot' +# - So if you are creating lots of plots they are collected by date +plotDir <- function(pdir = 'plot', ...){ + plot_dir <- paste(pdir, '/', yymmdd(...), sep='') + dir.create(plot_dir, rec=T) + return(plot_dir) +} + + + +####################################################################### +# nukem - erase all objects (after asking nicely) +nukem <- function(){ + x <- readline ("\n\tErase all objects? (Y)") + if (tolower(substr(x, 1, 1)) != "n") {rm(list=ls(all=TRUE))}# NUKE all objects + else {print("Nothing was changed.")} +} |
