summaryrefslogtreecommitdiff
path: root/file settings and cleanup
blob: a26bc0b0ed0b5ffa6750b83ba00ca64062e864df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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.")}
}