generated from AEADataEditor/replication-template-development
-
Notifications
You must be signed in to change notification settings - Fork 5
/
template-config.R
156 lines (111 loc) · 4.77 KB
/
template-config.R
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
#Template config.R
# INSTRUCTIONS:
#
# Step 1: Add or modify code to install libraries
#
# If the author provides a setup or config file that installs packages, use it.
# Then proceed to Step 2.
# If not, then copy this code to "config.R", modify the lines after this comment block,
# and add "source("config.R", echo = TRUE)" to the main file.
#
# If the author does not have a main file, you can instead copy this code to "main.R",
# and add "source("authorcode.R", echo = TRUE)" to the end of this file, for each R file
# provided by the author.
#
# Step 2: Run the code generating a log file
#
# The following command works on Linux, MacOS, and on Windows
# from the "Terminal" within Rstudio:
# R CMD BATCH program.R
# For alternative ways to do that, see
# https://github.com/labordynamicsinstitute/replicability-training/wiki/R-Tips
#*================================================
#* lets capture the current wd, so we can return to it later
temphome <- getwd()
#*================================================
#* This lists the libraries that are to be installed.
#* Adjust this by adding on additional ones identified by the authors as necessary
global.libraries <- c("devtools","rprojroot")
# For example, you can add on two additional ones:
# global.libraries <- c("foreign","devtools","rprojroot","ggplot2","nonsenseR")
#*==============================================================================================*/
#* This is specific to AEA replication environment. May not be needed if no confidential data */
#* are used in the reproducibility check. */
#* Replicator should check the JIRA field "Working location of restricted data" for right path */
sdrive <- ""
#*================================================
#* This lists any paths, relative to the root directory, that are to be created.
create.paths <- c("logs","libraries")
# for instance, the following paths might be necessary
#create.paths <- c("data/raw","data/interwrk","data/generated","results")
################################################
# Setup for automatic basepath detection #
################################################
# Preferred:
# in bash, go to the root directory and type
# "touch .here". Then the following code will work cleanly.
# Alternative:
# There is already a "name_of_project.Rproj" file in the root directory.
# No further action needed
# If for some reason that does not work (and it always should)
# manually override:
# rootdir <- "path/to/root/directory"
rootdir <- ""
####################################
# global libraries used everywhere #
####################################
posit.date <- Sys.Date() - 31
# posit.date <- "2020-01-01" # uncomment and set manually if the above does not work
# PPM only snapshots on weekdays (not sure why...)
if ( weekdays(posit.date) %in% c("Saturday","Sunday") ) posit.date <- posit.date - 2
options(repos = c(CRAN = paste0("https://packagemanager.posit.co/cran/",posit.date)))
################################################
# No additional changes needed below this line #
################################################
# print option repos
message(paste0("Setting Posit Package Manager snapshot to ",posit.date))
message("If this does not work, set the date manually in line 22")
getOption("repos")
####################################
# Set path to root directory #
# #
####################################
install.packages("here")
if ( rootdir == "" ) rootdir <- here::here()
setwd(rootdir)
# Main directories
for ( dir in create.paths){
if (file.exists(file.path(rootdir,dir))){
} else {
dir.create(file.path(rootdir,dir))
}
}
# Setting project-specific library
.libPaths(file.path(rootdir,"libraries"))
# Get information on the system we are running on
Sys.info()
R.version
# Function to install libraries
# inject here back into these libraries
global.libraries <- c(global.libraries,"here")
pkgTest <- function(x)
{
if (!require(x,character.only = TRUE))
{
install.packages(x,dep=TRUE)
if(!require(x,character.only = TRUE)) stop("Package not found")
}
return("OK")
}
## Add any libraries to this line, and uncomment it.
results <- sapply(as.list(global.libraries), pkgTest)
# lets get back to where we started
setwd(temphome)
# keep these lines in the config file
message("======================================================================================================")
message(paste0(" Current working directory: ",getwd()))
print(sessionInfo())
message("Current libPaths:")
message(.libPaths())
message(list.files(.libPaths()[1]))
message("Done with configuration.")