-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rprofile
166 lines (148 loc) · 6.78 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
##
## https://mran.revolutionanalytics.com/documents/rro/reproducibility/doc-research/
##
#### utils:::assignInNamespace("q",function(save="no",status=0,runLast=TRUE) {
#### .Internal(quit(save,status,runLast))
#### }, "base")
#### utils:::assignInNamespace("quit",function(save="no",status=0,runLast=TRUE) {
#### .Internal(quit(save,status,runLast))
#### }, "base")
.vl_env <- new.env()
local({
.Last <- function() {
if(interactive()) {
try(utils::savehistory("~/.Rhistory"))
}
}
.First <- function() {
if(interactive()) {
Sys.setenv(R_HISTSIZE=5000)
## Sys.setenv("_R_CHECK_LENGTH_1_CONDITION_"=TRUE) # See cond: argument in ?`if` ... no longer works 2024.12.10
try(utils::loadhistory("~/.Rhistory"))
}
}
qn <- function() quit('no')
cd <- function(dir="~") setwd(dir)
## print.data.frame <- data.table:::print.data.table ## data.table *is* data.frame so we use its print method which, IMO, is much more useful for printing data.frame too!!
#### ## saner printing of sf objects!
#### print.sf <- function(x, ..., n = ifelse(options("max.print")[[1]] == 99999, 20, options("max.print")[[1]])) {
#### geoms = which(vapply(x,function(col) inherits(col,"sfc"), TRUE))
#### nf = length(x) - length(geoms)
#### app = paste("and", nf, ifelse(nf == 1, "field", "fields"))
#### if (any(!is.na(st_agr(x))))
#### app = paste0(app,"\n","Attribute-geometry relationship: ", sf:::summarize_agr(x))
#### if (length(geoms) > 1)
#### app = paste0(app,"\n","Active geometry column: ", attr(x, "sf_column"))
#### print(st_geometry(x),n=0,what="Simple feature collection with",append=app)
#### if(is.data.table(x)) {
#### ## data.table:::print.data.table(x, ...)
#### NextMethod()
#### } else {
#### print.data.frame(x, ...)
#### }
#### ## print.data.frame(x, ...)
#### invisible(x)
#### }
# some handy aliases
cwd <- getwd
pwd <- getwd
h <- utils::help
## len <- length
lf <- list.files
home <- cd
hd <- utils::head
tl <- utils::tail
doff <- grDevices::dev.off
default_options <- options()
options(datatable.print.class=TRUE ## causes problems with test.data.table()
, datatable.print.nrows=30
, datatable.print.topn=5
, datatable.print.trunc.cols = TRUE
## , datatable.verbose=TRUE
, defaultPackages=c(options()$defaultPackages, "tools")
## , digits=18 ## to catch issues with floating point rounding. Try log(1000)/log(10) with different values of digits to see how it gets printed.
, digits.secs=2
, editor="vim"
## , error=utils::recover
, expressions=2000
, help_type = "html"
, help.try.all.packages=TRUE
, locatorBell=FALSE
## , lubridate.fasttime=TRUE
, max.print=500
, mc.cores = parallel::detectCores()
, Ncpus = parallel::detectCores() - 2
# , prompt="R=> " # Causes problems with ESS
## , repos=structure(c(CRAN="https://cloud.r-project.org/",
## INLA="https://inla.r-inla-download.org/R/stable"))
, timeout = max(300, getOption("timeout")) ## INLA download timesout with default value! See ?download.file of why we need higher value here...
, useFancyQuotes=FALSE # causes problems with LaTeX
## , warnPartialMatchArgs=TRUE ## Causes lots of warnings!
, warnPartialMatchAttr=TRUE ## Comment if weird "repeatwithsimplewarning" error messages start popping up.
, warnPartialMatchDollar=TRUE ## Comment if weird "repeatwithsimplewarning" error messages start popping up.
, xtable.booktabs=TRUE
# , xtable.type="html"
)
## http://andrewgelman.com/2010/10/29/could_someone_p/
## ?setHook and ?plot.new
### setHook('plot.new',function(...) graphics::par(mar=c(3,3,2,1),mgp=c(2,0.7,0),tck=-0.01))
### Above causes problems with raster::plot!
setHook(packageEvent("sp","onLoad"),
function(...) {
fn <- function(object) str(object,max.level=2)
spclasses <- c("SpatialPoints", "SpatialPointsDataFrame",
"SpatialMultiPoints", "SpatialMultiPointsDataFrame",
"SpatialGrid", "SpatialGridDataFrame",
"SpatialLines", "SpatialLinesDataFrame",
"SpatialPolygons", "SpatialPolygonsDataFrame")
for(spclass in spclasses) setMethod("show",spclass,fn)
})
setHook(packageEvent("tuneR", "onLoad"), function(...) options(WavPlayer="/Users/vijay/code/R/playRWave"))
functions_to_learn_this_session <- local({
last_seen <- NA ; fns_to_learn <- c()
function() {
if(is.na(last_seen) | Sys.Date() > last_seen) {
fns_to_learn <<- sample(c(lsf.str("package:base"), lsf.str("package:utils"),
lsf.str("package:stats"), lsf.str("package:MASS")), 5)
}
last_seen <<- Sys.Date()
cat("R-Trivia: Do you know the following?\n")
invisible(cat(paste(seq_along(fns_to_learn), fns_to_learn, sep=". "), sep="\n"))
}
})
}, env=.vl_env)
if( .Platform$OS.type == "unix" & !Sys.getenv("USER") == "root") {
if (file.exists("~/code/R/utils.R")) {
local(source("~/code/R/utils.R", local=.vl_env), envir=.vl_env)
}
}
attach(.vl_env)
Sys.setenv(MAKEFLAGS = "-j4")
if (interactive()) {
## if (ispkginstalled("fortunes")) print(fortunes::fortune()) ## Slows down R!
suppressPackageStartupMessages(require(compiler))
suppressPackageStartupMessages(require(fastmatch))
match <- fmatch ## faster matching!
## suppressPackageStartupMessages(require(fasttime))
suppressPackageStartupMessages(compilePKGS(TRUE)) ## ?compiler::compile
setCompilerOptions(suppressUndefined=TRUE) ## ?compiler::compile
suppressPackageStartupMessages(require(MASS))
suppressPackageStartupMessages(require(Matrix))
suppressPackageStartupMessages(require(parallel))
suppressPackageStartupMessages(require(bit64))
suppressPackageStartupMessages(require(RSQLite))
suppressPackageStartupMessages(require(data.table))
suppressPackageStartupMessages(require(ggplot2)); ## theme_set(theme_vl())
suppressPackageStartupMessages(require(utils))
suppressPackageStartupMessages(require(sf))
## suppressPackageStartupMessages(require(knitr))
## suppressPackageStartupMessage(require(ff))
## invisible(enableJIT(3)) ## ?compiler::compile ## causes problems! 2015.11.14
functions_to_learn_this_session()
if(isTRUE(any(grepl("^(linux|darwin)",R.version$os,ignore.case=TRUE)))) options(width=system("tput cols",intern=TRUE))
}
## if(ispkginstalled("rstan")) rstan::rstan_options(auto_write=TRUE)
# http://reasoniamhere.com/2013/09/23/7-r-tips-rescued-from-the-vault/
makeActiveBinding(".", function() .Last.value, .GlobalEnv) # ?makeActiveBinding
## Sys.setenv(TZ="America/New_York") ## Weird warnings on mac!! 2017.11.14
## All packages needed in scripts have to be explicitly stated!!