-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmolten-utils.rnw
195 lines (154 loc) · 4.87 KB
/
molten-utils.rnw
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
\documentclass[11pt]{article}
\usepackage{default}
\title{Utility routines for building molten files}
\author{John Leonard}
\begin{document}
% ===============================================================================================
% Front matter
% ===============================================================================================
\maketitle
\begin{abstract}
This report documents the various utility R subroutines and functions used
when building molten-data files. These routine are separated from
other routines to minimize the need to rebuild the molten data files as work progresses
on the rest of the reporting system.
\end{abstract}
\tableofcontents
\section{About}
These common utilities are combined with several environment settings to create an RParams file. These
RParams files are loaded each time a RNW file is processed.
This file includes both common packages that get loaded and used throughout report generation,
and common utility functions that are reused throughout report generation.
\section{Common packages to be loaded}
Below are the common R packages that are loded.
<<echo=T>>=
###
#
# The following code comes from file "molten-utils.rnw"
# Make any changes to this original file!
#
###
# This is a list of commonly used packages. Keep it maintained and upgrading
# to newer versions of R will be much easier.
desired.package.list = c("reshape","plyr","ggplot2","xtable","tools","gridExtra","Hmisc","maps","mapproj",
"knitr","WriteXLS","R2wd","stringr","xts","lubridate","RColorBrewer")
# fragile packages are necessary but might have not yet been upgraded to current version of R
# use the fragile.package.list to keep track of what you might need.
fragile.package.list = "RDCOMCLIENT"
missing.package.list = setdiff(desired.package.list,intersect(rownames(installed.packages()),desired.package.list))
for (p in missing.package.list){
install.packages(p,repos="http://cran.rstudio.com/")
}
missing.package.list = setdiff(desired.package.list,intersect(rownames(installed.packages()),desired.package.list))
if( length(missing.package.list)> 1) {
msg = paste("
** Couldn't install desired packages:",paste(missing.package.list,collapse=","),"
** You must have recently upgraded to newer version of R.
** If you really need the routines, install them from source
** otherwise remove from desired.package.list.
")
stop(paste("Refer to file molten-utils.rnw",msg),call.=FALSE)
}
OK = 1==1
for (p in desired.package.list){
OK = OK & eval(parse(text=paste("suppressPackageStartupMessages(require(",p,",quietly=TRUE))")))
}
if(!OK){
msg = paste("
** Desired packages couldn't be installed.
")
stop(paste("Refer to file molten-utils.rnw",msg),call.=FALSE)
}
@
\section{Common utility functions}
\subsection{subroutine table_ListToRow}
<<echo=T>>=
table_ListToRow = function( x ){
s = encoded_text_to_latex(paste(x,collapse=' & '))
s = gsub("_","\\\\_",s)
s
}
@
\subsection{function trim}
<<echo=T>>=
trim = function(s){
x = sub("^[[:space:]]*(.*?)[[:space:]]*$", "\\1", s, perl=TRUE)
return(x)
}
@
\subsection{function table_ListToRows}
<<echo=T>>=
table_ListToRows = function( x ) {
result = ""
for(i in 1:length(x)){
s = encoded_text_to_latex(paste(names(x)[i],x[[i]],sep=" & "))
s = gsub("_","\\\\_",s)
if (i==1) {
result = s
} else {
result = paste(result,s,sep="\\\\\n")
}
}
if(nchar(result)>0) result = paste(result,"\\\\\n",sep="")
result
}
@
\subsection{function showParamsAsTable}
<<echo=T>>=
showParamsAsTable = function(){
header = "
\\begin{table}[h]%
\\footnotesize\\centering
\\begin{tabularx}{0.8\\textwidth}{lX}
\\toprule%
param name & value\\\\%
\\midrule
"
middle = table_ListToRows(params)
footer = "
\\bottomrule
\\end{tabularx}
\\caption[List of parameters passed by makefile]{List of parameters passed by makefile}
\\end{table}
"
result = paste(header,middle,footer,sep="")
}
@
\subsection{function parseKeyValues}
<<echo=T>>=
addKeyValues = function(name.value.pairs="",existing.list=list()){
temp = gsub("==","=",name.value.pairs) # a helper, often one will write Gender==F rather than Gender=F
x = unlist(strsplit(unlist(strsplit(temp,";")),"="))
if(length(x)>=2){
existing.list[x[seq(1,length(x),2)]] = x[seq(2,length(x),2)]
}
existing.list
}
@
<<echo=T>>=
cleanName = function( file.name ){
gsub("_","\\\\_",file.name)
}
@
<<echo=T>>=
insertSQLfile = function(
file,
desc=NULL,
section=NULL
){
cat("\n%% InsertSQLfile: ",file,"\n\n",sep="")
# cat("\\clearpage\n",sep="")
if (!is.null(section)) cat(section,sep="")
if (!is.null(desc)) cat("\\input{",desc,"}\n",sep="")
cat("\\fvset{fontsize=\\footnotesize,frame=lines,fontshape=sl,numbers=left,label=SQL file ",cleanName(file),"}\n\n",sep="")
cat("\\VerbatimInput{",file,"}\n\n",sep="")
}
@
\subsection{function FYtoFall}
<<>>=
FYtoFall = function ( x ){
x[grep("^FY",x)] = paste("Fall",as.numeric(substr(x[grep("^FY",x)],3,6))-1)
x
}
@
\end{document}