-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rprofile
61 lines (56 loc) · 3.04 KB
/
.Rprofile
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
options(tsci.systemwrapper = function(cmd='',...,VERBOSE=getOption('sysverbose',T)
,CHECKFILES=c('files')){ # nodeps
args <- list(...); sysargs <- list();
# separate out the args intended for system
for(ii in intersect(names(args),names(formals(system)))){
sysargs[[ii]] <- args[[ii]]; args[[ii]] <- NULL;};
# check to make sure all arguments listed in checkfiles contain only files
# that exist
for(ii in intersect(CHECKFILES,names(args))){
if(!all(.exist <- file.exists(args[[ii]]))){
stop('The following files cannot be found:\n'
,paste(args[[ii]][!.exist],collapse=', '))}};
for(xx in args) cmd <- paste(cmd,paste(xx,collapse=' '));
if(VERBOSE) message('Executing the following command:\n',cmd);
return(do.call(system,c(command=cmd,sysargs)));
});
options(tsci.gitsub = function(stopfile='.developer'){if(!file.exists(stopfile)){
unlink(getOption('tsci.systemwrapper')("git submodule --quiet foreach 'echo $path'"
,intern=TRUE,VERBOSE=FALSE)
,recursive = TRUE,force = TRUE);
getOption('tsci.systemwrapper')('git submodule update --init --recursive --remote')} else {
message('Developer mode-- ignoring.'); return(0);
}});
clean_slate <- function(command="",removepatt='^\\.RData$|.*\\.[Rr]\\.rdata$|.*\\.html$|.*_cache$'
,all=TRUE,cleanglobal=TRUE
,envir=parent.frame()){
if(!interactive()) warning('This function is intended to run in an '
,'interactive session to restart that\n '
,'session on a clean slate. If you are calling it '
,'non-interactively (from a\n script or '
,'function), don\'t expect any code that you put '
,'after it to work!');
# remove cached files
unlink(list.files(pattern=removepatt,all.files=TRUE,full.names = TRUE),recursive=TRUE,force=TRUE);
# clear out calling environment
rm(list=ls(all.names=all,envir = envir),envir = envir);
# also global environment if specified
if(cleanglobal) rm(list=ls(all.names=all,envir=.GlobalEnv),envir = .GlobalEnv);
# if rstudioapi available, use it to restart the session
if(requireNamespace('rstudioapi') && rstudioapi::isAvailable()){
rstudioapi::restartSession(command)};
}
renameoutput <- function(file=get('.currentscript')
,fromdata=basename(get('inputdata')['dat03'])
,suffix='.html'){
fromdata <- gsub('^[0-9]{10,12}_[0-9a-z]{5,7}',''
,fromdata) %>% gsub('\\.[^.]*$','',.);
file <- file.path(dirname(file),gsub('\\.[^.]*$','',basename(file)));
newfile <- file.path(dirname(file)
,paste0(basename(file),'_'
,substr(tools::md5sum(paste0(file,suffix)),1,5)
,fromdata,suffix));
file.symlink(paste0(file,suffix),newfile);
if(file.exists(newfile)) sprintf('Linked %s to %s',paste0(file,suffix)
,newfile);
}