Browse code

Rprofile - v1.0 - See Versions section for details

Fred authored on01/03/2020 22:04:59
Showing1 changed files
... ...
@@ -1,8 +1,8 @@
1 1
 # Title: .Rprofile
2 2
 # Author: Frédéric CHEVALIER <f15.chevalier@gmail.com>
3
-# Version: 0.4
3
+# Version: 1.0
4 4
 # Created in: 2014-07-06
5
-# Modified in: 2016-10-02
5
+# Modified in: 2020-03-01
6 6
 
7 7
 
8 8
 
... ...
@@ -10,7 +10,7 @@
10 10
 # Comments #
11 11
 #==========#
12 12
 
13
-# ~/.Rprodile: executed by R for interactive shells.
13
+# ~/.Rprofile: executed by R for interactive shells.
14 14
 # see http://stat.ethz.ch/R-manual/R-devel/library/base/html/Startup.html
15 15
 
16 16
 
... ...
@@ -19,6 +19,7 @@
19 19
 # Versions #
20 20
 #==========#
21 21
 
22
+# v1.0 - 2020-03-01: library path set / specific library path depending on the environment / section executed in non interactive mode / repo link updated
22 23
 # v0.4 - 2016-10-02: hour display on prompt simplified / auto-update of terminal column value for display added
23 24
 # v0.3 - 2014-11-03: less function added
24 25
 # v0.2 - 2014-10-21: bug correction about width of terminal
... ...
@@ -31,15 +32,25 @@
31 32
 # User specific options #
32 33
 #=======================#
33 34
 
34
-# Test if it is an interactive session
35
-if (interactive()) {
36
-
37
-# Create a new invisible environment for all the functions to go in so it doesn't clutter your workspace
35
+# Create a new invisible environment for all the objects and functions to go in so it doesn't clutter the workspace
38 36
 .startup <- new.env()
39 37
 
40
-# Don't ask me for my CRAN mirror every time
41
-options("repos" = c(CRAN = "http://cran.revolutionanalytics.com"))
38
+# Determine environment
39
+.startup$is.root  <- system("id -u", intern=TRUE) == 0
40
+.startup$is.conda <- nchar(Sys.getenv("CONDA_PREFIX")) > 0
41
+
42
+# Specific package folder in user global environment only
43
+if (! .startup$is.root && ! .startup$is.conda) {
44
+    .startup$mylib <- paste0("~/.R-libs/", version$major, ".", version$minor)
45
+    if (! dir.exists(.startup$mylib)) {dir.create(.startup$mylib, recursive=TRUE)}
46
+    .libPaths(.startup$mylib)
47
+}
48
+
49
+# Test if it is an interactive session and run the downstream code
50
+if (interactive()) {
42 51
 
52
+# Don't ask me for my CRAN mirror every time
53
+options("repos" = c(CRAN = "https://cran.revolutionanalytics.com"))
43 54
 
44 55
 
45 56
 #===========#
... ...
@@ -81,7 +92,7 @@ require(utils, quietly = TRUE)
81 92
 
82 93
 # All functions executed when exiting R
83 94
 .Last <- function() {
84
-    if (! any(commandArgs() == '--no-readline')) {
95
+    if (! any(commandArgs() == '--no-readline') && ! .startup$is.root) {
85 96
         require(utils, quietly=TRUE)
86 97
         #timestamp(quiet=TRUE)
87 98
         try(savehistory(Sys.getenv("R_HISTFILE")))
... ...
@@ -116,7 +127,7 @@ addTaskCallback(.startup$updatePrompt)
116 127
     file <- tempfile()
117 128
     sink(file); on.exit(sink())
118 129
     print(x)
119
-    file.show(file, delete.file = T)
130
+    file.show(file, delete.file = TRUE)
120 131
 }
121 132
 
122 133
 
... ...
@@ -126,7 +137,7 @@ addTaskCallback(.startup$updatePrompt)
126 137
 #=========#
127 138
 
128 139
 # Moving (bash-like command)
129
-.startup$cd <- setwd
140
+.startup$cd  <- setwd
130 141
 .startup$pwd <- getwd
131 142
 
132 143