-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_project.sh
executable file
·212 lines (189 loc) · 6.25 KB
/
new_project.sh
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env bash
# Little script to create a new data analysis project directory
# Requires a single cmdline argument for the new project name
# With inspiration from https://github.com/chendaniely/computational-project-cookie-cutter
# Time-stamp: <2019-03-05 13:43:54 (slane)>
# Don't kill files
set -o noclobber
# Exit if no arguments were provided.
[ $# -eq 0 ] && { echo "Usage: $0 [target directory]"; exit 1; }
# Create the base project directory
mkdir $1
# Create the extra required directories
cd $1
mkdir data data-raw figs manuscripts R Rmd scripts
# Add a README.md
cat > README.md <<EOF
# $1
## Project description
Add some description here.
EOF
# Add a basic .gitignore
cat > .gitignore <<EOF
# Git ignore file
*.pdf
*.png
*.doc
*.docx
*.xls
*.xlsx
*.aux
*.log
*.lot
*.ttt
*.out
*.bbl
*.Rhistory
*.html
*.RData
*.rds
*.rda
*.tex
*.fdb_latexmk
*.fls
*.toc
*.lof
*.fff
*.blg
*.bcf
*.rec
*.run.xml
*.zip
*.txt
*~
manuscripts/cache/*
manuscripts/auto/*
.DS_Store
EOF
# Add a basic Makefile
cat > Makefile <<EOF
# Makefile
# Time-stamp: <>
.PHONY: all install-packages clean-manuscripts clobber
all: install-packages data/data.rds manuscripts/manuscript.pdf
# Set the directory of the Makefile.
ROOT_DIR:=\$(shell dirname \$(realpath \$(lastword \$(MAKEFILE_LIST))))
################################################################################
# Build docker container for reproducible environment
# .PHONY: build-docker
# build-docker: .build.docker
# .build.docker: docker/Dockerfile
# docker build --rm -t container-name docker/ \\
# && touch .build.docker
################################################################################
# Rules to make data
# data/data.rds: R/clean-data.R data-raw/data.csv
# cd \$(<D) \\
# && Rscript --no-save --no-restore \$(<F)
################################################################################
# Rules to make manuscripts
# manuscripts/manuscript.tex: manuscripts/manuscript.Rnw data/data.rds
# cd \$(<D) \\
# && Rscript --no-save --no-restore -e "knitr::knit('\$(<F)')"
# manuscripts/manuscript.pdf: manuscripts/manuscript.tex
# cd \$(<D) \\
# && latexmk -pdf \$(<F)
# manuscripts/manuscript.html: manuscripts/manuscript.Rmd
# cd \$(<D) \\
# && Rscript --no-save --no-restore -e "rmarkdown::render('\$(<F)')"
################################################################################
# Cleaning up
clean-manuscripts:
cd manuscripts/ \\
&& rm -f *.aux *.bbl *.bcf *.blg *.fdb_latexmk *.fls *.lof *.log \\
*.lot *.code *.loe *.toc *.rec *.out *.run.xml *~ *.tex
clobber: clean-manuscripts
cd manuscripts/ \\
&& rm -rf auto/ cache/ figure/
################################################################################
# Rule to install packages (from script extraction).
# This has been provided for the case where library/require calls are made in
# separate scripts. If the packages are not installed, make should return an
# error, saying that the packages are not available. This is politic; don't
# assume the user wants to install stuff without asking.
install-packages: scripts/installs.txt
scripts/installs.txt: scripts/strip-libs.sh R/ipak.R
cd scripts; \\
chmod u+x strip-libs.sh; \\
./strip-libs.sh ../R/ installs.txt; \\
./strip-libs.sh ../Rmd/ installs.txt; \\
./strip-libs.sh ../manuscripts/ installs.txt; \\
Rscript --no-save --no-restore ../R/ipak.R insts=installs.txt
################################################################################
# Show help on important targets.
.PHONY: help
help: Makefile
@echo '\\nHelp on $1 Targets\\n----';
@sed -n "s/^#' //p" \$(<F);
@echo '----';
################################################################################
# Rule to create dependency graph.
deps-png:
docker run -v \$\$(pwd):/tmp/graph stevelane/makedeps-graph \\
&& mv deps.* figs/
EOF
# Add install packages scripts
cat > scripts/strip-libs.sh <<EOF
#!/usr/bin/env bash
# Strips libraries from .R/.r scripts and creates a text file with a list of them.
# Requires first argument as folder where scripts are stored, and second as the
# text file it is stored in.
folder=\$1
inst=\$2
for filename in \$folder/*.{r,R}{md,nw,}; do
if [ -f \$filename ]; then
awk -F '[(]|[)]' '/^library|^require/{print \$2;}' \$filename >> \$inst
grep -oE '\b[a-zA-Z0-9]*::' $filename | awk -F '::' '{print $1;}' >> $inst
fi
done
sort < \$inst > \$inst.bk
uniq < \$inst.bk > \$inst
rm \$inst.bk
EOF
cat > R/ipak.R <<EOF
#!/usr/bin/env Rscript
args <- commandArgs(trailingOnly = TRUE)
################################################################################
################################################################################
## Title: ipak
## Author: Steve Lane
## Synopsis: Script to test for installed packages, and if not installed,
## install them.
################################################################################
################################################################################
if(length(args) == 0){
stop("A single argument must be passed to ipak.R: insts.\ninsts is the location of a newline separated list of required packages:\n\tRscript ipak.R insts=path/to/installs.txt\nA repository may be passed using the repos option:\n\tRscript ipak.R insts=path/to/installs.txt repos=REPOS",
call. = FALSE)
} else {
hasOpt <- grepl("=", args)
argLocal <- strsplit(args[hasOpt], "=")
for(i in seq_along(argLocal)){
value <- NA
tryCatch(value <- as.double(argLocal[[i]][2]), warning = function(e){})
if(!is.na(value)){
## Assume int/double
assign(argLocal[[i]][1], value, inherits = TRUE)
} else {
assign(argLocal[[i]][1], argLocal[[i]][2], inherits = TRUE)
}
}
}
if(exists("repos")){
options(repos = c(CRAN = repos))
} else {
## Set default cran repository.
## Use the one set from .Rprofile if given, else set.
if(getOption("repos") == "@CRAN@" | is.null(getOption("repos"))){
options(repos = c(CRAN = "https://cran.rstudio.com"))
}
}
## Check if packages installed
pkg <- scan(insts, "character")
new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
## Install if not already installed (including dependencies).
if(length(new.pkg) > 0){
install.packages(new.pkg, dependencies = TRUE)
} else {
message("All packages currently installed.")
}
EOF