-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-r-project.sh
executable file
·212 lines (165 loc) · 5.58 KB
/
create-r-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
#!/bin/sh
opt_project_name=simple
opt_project_suffix="r-proj"
opt_author_name=""
opt_author_email=""
opt_license="GPL"
### functions #####################
function fnct_project_dirname()
{
prj_name=$1
prj_suff=$2
if [ -n "$prj_suff" ]; then
echo "${prj_name}-${prj_suff}"
else
echo "${prj_name}"
fi
}
###
function fnct_author_name()
{
author_name=$1
if [ -n "${opt_author_name}" ]; then
echo "${opt_author_name}"
else
echo "__AUTHOR_NAME__"
fi
}
###
function fnct_author_email()
{
author_name=$1
if [ -n "${opt_author_email}" ]; then
echo "${opt_author_email}"
else
echo "__AUTHOR_EMAIL__"
fi
}
###
function fnct_script_header()
{
fileName=$1
echo "###########################################################
# Filename : ${fileName}
#-----------------------------------------------------------
# Project : ${opt_project_name}
# Author(s) : ${AuthorName} <${AuthorEmail}>
# Date : $(date -I) (ISO 8601/YYYY-MM-DD)
#-----------------------------------------------------------
# License : ${opt_license}
#-----------------------------------------------------------
"
}
#######################################
### main ###
#######################################
AuthorName=$(fnct_author_name $opt_author_name)
AuthorEmail=$(fnct_author_email $opt_author_email)
### create project directory ###
project_dirname=$(fnct_project_dirname $opt_project_name $opt_project_suffix)
if [ ! -d "$project_dirname" ]; then
mkdir "$project_dirname/" || \
{ echo "Error: Can't create '$project_dirname/' " >&2; exit 1; }
else
echo "Error: Directory '$project_dirname/' already exists. " >&2; exit 1;
fi
### create sub directorie ###
sub_dirnames="R data doc figs output"
for subdirname in ${sub_dirnames}; do
subdir="${project_dirname}/${subdirname}"
mkdir "${subdir}/"
# add an empty '.gitignore' file to subdir.
# Workaround for git with empty directories.
touch "${subdir}/.gitignore"
done
###
echo "$(fnct_script_header ${project_dirname}/R/functions.R)
# Description: only function definitions - no code that actually runs
############################################################
" > ${project_dirname}/R/functions.R
###
echo "$(fnct_script_header ${project_dirname}/R/utilities.R)
# Description: only utility function definitions - no code that actually runs
############################################################
" > ${project_dirname}/R/utilities.R
###
echo "#!/usr/bin/Rscript
$(fnct_script_header ${project_dirname}/${opt_project_name}-main.R)
# Description : executable (main) R-script of project '${opt_project_name}'
# This script ...
#
############################################################
# library(foreign)
# library(some_package)
# library(some_other_package)
source("R/functions.R")
source("R/utilities.R")
### stata ###
# dta.data.path <- ./data/__DATA_PATH__
# dta.file.name <- __DATA_NAME__.dta
# dta.file.path <- file.path(dta.data.path, dta.file.name)
# my.data.df <- read.dta(dta.file.path, convert.factors=FALSE)
### spss ###
# spss.data.path <- ./data/__DATA_PATH__
# spss.file.name <- __DATA_NAME__.sav
# spss.file.path <- file.path(spss.data.path, spss.file.name)
# my.data.df <- read.spss( spss.file.path,
# to.data.frame = TRUE,
# use.value.labels = FALSE
# )
### sas ###
# sas.data.path <- ./data/..
## ? foreign::read.ssd
## ? foreign::read.xport
###
my.data.df <- iris # default: R std. dataset 'iris'
###################
summary(my.data.df)
" > ${project_dirname}/${opt_project_name}-main.R
###########################################################
echo "
=================================================
== Project : ${opt_project_name}
== Author : ${AuthorName} <${AuthorEmail}>
== Date : $(date -I) (ISO 8601/YYYY-MM-DD)
== What/Abstract:
This project ....
=== Directory Structure: ===
${project_dirname}/
|__ README.txt
|__ ${opt_project_name}-main.R
|__ functions.R
|__ utilities.R
|__ R/
|__ data/
|__ doc/
|__ figs/
|__ output/
* The ${project_dirname} directory contains this R project.
* The README.txt file (this) is the description of the project directory
structure.
* The ${opt_project_name}-main.R is the main/executable R file ...
TODO: add more text for "what is ${opt_project_name}-main.R..."
* The R directory contains various files with function definitions
(but only function definitions - no code that actually runs).
* The data directory contains data used in the analysis. This is treated
as read only; in paricular the R files are never allowed to write to
the files in here. Depending on the project, these might be csv files,
a database, and the directory itself may have subdirectories.
* The doc directory contains the paper. I work in LaTeX which is nice
because it can pick up figures directly made by R. Markdown can do the
same and is starting to get traction among biologists. With Word you’ll
have to paste them in yourself as the figures update.
* The figs directory contains the figures. This directory only contains
generated files; that is, I should always be able to delete the contents
and regenerate them.
* The output directory contains simuation output, processed datasets,
logs, or other processed things.
=== NOTES ===
* The base directory stucture based on the article
http://nicercode.github.io/blog/2013-04-05-projects/ by Vince Buffalo
(accessed: 2013-06-07)
" > ${project_dirname}/README.txt
############################
## show project structure
find "$project_dirname"