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
55
56
57
58
59
60
61
|
#######################################################################
# 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)
}
#######################################################################
# heading - take a string and print it out with underline
# Useful for formatting output text files
#
headingFunc <- function(string, ul='=', caps=T){
if(length(string) != 1) string <- 'Error in headingFunc'
if(class(string) != 'character') string <- as.character(string)
if(caps) string <- toupper(string)
cat('\n', string, '\n', sep='')
reps <- floor(nchar(string) / nchar(ul))
filler <- substr(ul, 1, (nchar(string) %% nchar(ul)))
cat(rep(ul, reps), filler, '\n\n', sep='')
}
############ handy for debugging #####################################
#######################################################################
# unWarn - set options()
unWarn <- function() options( warn = -1)
#######################################################################
# Warn - set options()
warn <- function() options( warn = 0)
#######################################################################
# sort of created by mistake when Joe typed htail
# prints the head and tail of a datframe or array.
# -- separates head from tail with a row of '...' (can be buggy)
# you can pass in lengths for head and tail if you want to.
htail <- function(x, min_obs=15, ...){
if(dim(x)[1] < min_obs) return(x)
hd <- head(x, ...)
tl <- tail(x, ...)
dots <- rep('...', ncol(x))
res <- rbind(hd, '...'=dots, tl)
return(res)
}
#######################################################################
# 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.")}
}
|