-
Notifications
You must be signed in to change notification settings - Fork 6
/
pan_flow.tex
362 lines (313 loc) · 12.3 KB
/
pan_flow.tex
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
\documentclass[xcolor=svgnames]{beamer}\usepackage[]{graphicx}\usepackage[]{color}
%% maxwidth is the original width if it is less than linewidth
%% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
\ifdim\Gin@nat@width>\linewidth
\linewidth
\else
\Gin@nat@width
\fi
}
\makeatother
\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}
\newcommand{\hlnum}[1]{\textcolor[rgb]{0.686,0.059,0.569}{#1}}%
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.192,0.494,0.8}{#1}}%
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.678,0.584,0.686}{\textit{#1}}}%
\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlstd}[1]{\textcolor[rgb]{0.345,0.345,0.345}{#1}}%
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0.161,0.373,0.58}{\textbf{#1}}}%
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0.69,0.353,0.396}{#1}}%
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0.333,0.667,0.333}{#1}}%
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.737,0.353,0.396}{\textbf{#1}}}%
\usepackage{framed}
\makeatletter
\newenvironment{kframe}{%
\def\at@end@of@kframe{}%
\ifinner\ifhmode%
\def\at@end@of@kframe{\end{minipage}}%
\begin{minipage}{\columnwidth}%
\fi\fi%
\def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
\colorbox{shadecolor}{##1}\hskip-\fboxsep
% There is no \\@totalrightmargin, so:
\hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
\MakeFramed {\advance\hsize-\width
\@totalleftmargin\z@ \linewidth\hsize
\@setminipage}}%
{\par\unskip\endMakeFramed%
\at@end@of@kframe}
\makeatother
\definecolor{shadecolor}{rgb}{.97, .97, .97}
\definecolor{messagecolor}{rgb}{0, 0, 0}
\definecolor{warningcolor}{rgb}{1, 0, 1}
\definecolor{errorcolor}{rgb}{1, 0, 0}
\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX
\usepackage{alltt}
\usetheme{Boadilla}
\usecolortheme[named=SeaGreen]{structure}
\usepackage{graphicx}
\usepackage{breqn}
\usepackage{xcolor}
\usepackage{booktabs}
\usepackage{verbatim}
\usepackage{tikz}
\usetikzlibrary{shadows,arrows,positioning}
\definecolor{links}{HTML}{2A1B81}
\hypersetup{colorlinks,linkcolor=links,urlcolor=links}
\usepackage{pgfpages}
\tikzstyle{block} = [rectangle, draw, text width=9em, text centered, rounded corners, minimum height=3em, minimum width=7em, top color = white, bottom color=brown!30, drop shadow]
\newcommand{\ShowSexpr}[1]{\texttt{{\char`\\}Sexpr\{#1\}}}
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\begin{document}
\title[R with Microsoft]{A simple workflow for using R with Microsoft products}
\author[M. Beck]{Marcus W. Beck}
\institute[USEPA NHEERL]{USEPA NHEERL Gulf Ecology Division, Gulf Breeze, FL\\
Email: \href{mailto:[email protected]}{[email protected]}, Phone: 850 934 2480}
\date{May 21, 2014}
%%%%%%
\begin{frame}
\vspace{-0.3in}
\titlepage
\end{frame}
%%%%%%
\begin{frame}{The problem...}
\begin{itemize}
\item R is great and has an increasing user base\\~\\
\item RStudio is integrated with multiple document preparation systems \\~\\
\item Output documents are not in a format that facilitates collaboration with
non R users, e.g., pdf, html \\~\\
\item Data coming to you may be in a proprietary format, e.g., xls spreadsheet
\end{itemize}
\end{frame}
%%%%%%
\begin{frame}{The solution?}
\begin{itemize}
\item Solution one - Make liberal use of `projects' within RStudio \\~\\
\item Solution two - Use \texttt{gdata} package to import excel data \\~\\
\item Solution three - Get pandoc to convert document formats - \href{http://johnmacfarlane.net/pandoc/}{http://johnmacfarlane.net/pandoc/} \\~\\
\end{itemize}
\onslide<2->
\large
\centerline{\textit{Not recommended for simple tasks unless you really, really love R}}
\end{frame}
%%%%%
\begin{frame}{An example workflow}
\begin{itemize}
\item I will present a workflow for integrating Microsoft products within RStudio as an approach to working with non R users \\~\\
\item Idea is to never leave the RStudio environment - dynamic documents! \\~\\
\item General workflow... \\~\\
\end{itemize}
\small
\begin{center}
\begin{tikzpicture}[node distance=2.5cm, auto, >=stealth]
\onslide<2->{
\node[block] (a) {1. Install necessary software and packages};}
\onslide<3->{
\node[block] (b) [right of=a, node distance=4.2cm] {2. Create project in RStudio};
\draw[->] (a) -- (b);}
\onslide<4->{
\node[block] (c) [right of=b, node distance=4.2cm] {3. Setup supporting docs/functions};
\draw[->] (b) -- (c);}
\onslide<5->{
\node[block] (d) [below of=a, node distance=2.5cm] {4. Import with \texttt{gdata}, summarize};
\draw[->] (c) -- (d);}
\onslide<6->{
\node[block] (e) [right of=d, node distance=4.2cm] {5. Create HTML document using knitr Markdown};
\draw[->] (d) -- (e);}
\onslide<7->{
\node[block] (f) [right of=e, node distance=4.2cm] {6. Convert HTML doc to Word with Pandoc};
\draw[->] (e) -- (f);}
\end{tikzpicture}
\end{center}
\end{frame}
%%%%%%
\begin{frame}[shrink]{The example}
You are sent an Excel file of data to summarize and report but you love R and want to do everything in RStudio...
% latex table generated in R 3.0.3 by xtable 1.7-3 package
% Thu Jun 05 09:05:24 2014
\begin{table}[ht]
\centering
{\scriptsize
\begin{tabular}{lrrrlrr}
\hline
SiteName & Year & Restoration & Reference & Observer.Names & Precipitation & Temperature \\
\hline
IGH & 2005 & 3 & 3 & Tyler\_Amanda & 0 & 48 \\
Kelly & 2005 & 4 & 2 & Patrick\_Chelsea & 0 & 48 \\
Carlton & 2005 & 2 & 3 & David\_Megan & 0 & 48 \\
IGH & 2006 & 9 & 6 & Tyler\_Amanda & 0 & 52 \\
Kelly & 2006 & 9 & 1 & David\_Megan & 0 & 52 \\
Carlton & 2006 & 7 & 3 & Patrick\_Chelsea & 0 & 52 \\
IGH & 2007 & 12 & 7 & David\_Megan & 12 & 41 \\
Kelly & 2007 & 2 & 18 & Jeremy\_Lucy & 12 & 41 \\
Carlton & 2007 & 11 & 2 & Patrick\_Chelsea & 12 & 41 \\
IGH & 2008 & 9 & 4 & Tyler\_Amanda & 0 & 54 \\
Kelly & 2008 & 14 & 5 & David\_Megan & 0 & 54 \\
Carlton & 2008 & 13 & 3 & Patrick\_Chelsea & 0 & 54 \\
IGH & 2009 & 18 & 7 & Patrick\_Chelsea & 0 & 55 \\
Kelly & 2009 & 16 & 5 & David\_Megan & 0 & 55 \\
Carlton & 2009 & 20 & 1 & Tyler\_Amanda & 0 & 55 \\
IGH & 2010 & 12 & 2 & David\_Megan & 0 & 61 \\
Kelly & 2010 & 15 & 3 & Patrick\_Chelsea & 0 & 61 \\
Carlton & 2010 & 24 & 4 & Tyler\_Amanda & 0 & 61 \\
\hline
\end{tabular}
}
\end{table}
\end{frame}
%%%%%%
\begin{frame}{Step 1}
Install necessary software and Packages \\~\\
\onslide<1->
\begin{itemize}
\onslide<2->
\item R and RStudio (can do with other R editors)\\~\\
\item Microsoft Office\\~\\
\onslide<3->
\item Strawberry Perl for using \texttt{gdata} package\\~\\
\item Pandoc\\~\\
\onslide<4->
\item Packages: \texttt{gdata}, \texttt{knitr}, \texttt{utils}, \texttt{xtable}, others as needed...
\end{itemize}
\end{frame}
%%%%%%
\begin{frame}{Step 2}
Create a project in RStudio \\~\\
\begin{itemize}
\item Create a folder or use existing on local machine \\~\\
\item Add .Rprofile file to the folder for custom startup \\~\\
\item Move all data you are working with to the folder \\~\\
\item Literally create project in RStudio \\~\\
\item Set options within RStudio \\~\\
\end{itemize}
\end{frame}
%%%%%%
\begin{frame}[fragile]{Step 3}
Setup supporting docs/functions, i.e., .Rprofile, functions, report, master
\scriptsize
\begin{block}{.Rprofile}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlcom{# library path}
\hlkwd{.libPaths}\hlstd{(}\hlstr{"C:\textbackslash{}\textbackslash{}Users\textbackslash{}\textbackslash{}mbeck\textbackslash{}\textbackslash{}R\textbackslash{}\textbackslash{}library"}\hlstd{)}
\hlcom{# startup message}
\hlkwd{cat}\hlstd{(}\hlstr{"My project...\textbackslash{}n"}\hlstd{)}
\hlcom{# packages to use}
\hlkwd{library}\hlstd{(utils)} \hlcom{# for system commands}
\hlkwd{library}\hlstd{(knitr)} \hlcom{# for markdown}
\hlkwd{library}\hlstd{(gdata)} \hlcom{# for import xls}
\hlkwd{library}\hlstd{(reshape2)} \hlcom{# data format conversion}
\hlkwd{library}\hlstd{(xtable)} \hlcom{# easy tables}
\hlkwd{library}\hlstd{(ggplot2)} \hlcom{# plotting}
\hlcom{# perl path for gdata}
\hlstd{prl_pth} \hlkwb{<-} \hlstr{"C:/strawberry/perl/bin/perl.exe"}
\hlcom{# functions to use}
\hlkwd{source}\hlstd{(}\hlstr{"my_funcs.r"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{block}
\end{frame}
%%%%%%
\begin{frame}[t, fragile]{Step 3}
Setup supporting docs/functions, i.e., .Rprofile, functions, report, master
\scriptsize
\begin{block}{my\_funcs.r}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlcom{###### functions for creating report, created May 2014, M. Beck}
\hlcom{###### processes data for creating output in report, 'dat_in' is input data as}
\hlcom{###### data frame, output is data frame with converted variables}
\hlstd{proc_fun} \hlkwb{<-} \hlkwa{function}\hlstd{(}\hlkwc{dat_in}\hlstd{) \{}
\hlcom{# convert temp to C}
\hlstd{dat_in}\hlopt{$}\hlstd{Temperature} \hlkwb{<-} \hlkwd{round}\hlstd{((dat_in}\hlopt{$}\hlstd{Temperature} \hlopt{-} \hlnum{32}\hlstd{)} \hlopt{*} \hlnum{5}\hlopt{/}\hlnum{9}\hlstd{)}
\hlcom{# convert data to long format}
\hlstd{dat_in} \hlkwb{<-} \hlkwd{melt}\hlstd{(dat_in,} \hlkwc{measure.vars} \hlstd{=} \hlkwd{c}\hlstd{(}\hlstr{"Restoration"}\hlstd{,} \hlstr{"Reference"}\hlstd{))}
\hlkwd{return}\hlstd{(dat_in)}
\hlstd{\}}
\hlcom{###### creates linear model for data, 'proc_dat' is processed data returned from}
\hlcom{###### 'proc_fun', output is linear model object}
\hlstd{mod_fun} \hlkwb{<-} \hlkwa{function}\hlstd{(}\hlkwc{proc_in}\hlstd{)} \hlkwd{lm}\hlstd{(value} \hlopt{~} \hlstd{variable} \hlopt{+} \hlstd{Year,} \hlkwc{dat} \hlstd{= proc_in)}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{block}
\end{frame}
%%%%%%
\begin{frame}[fragile,shrink]{Step 3}
Setup supporting docs/functions, i.e., .Rprofile, functions, report, master
\scriptsize
\begin{block}{report.Rmd}
\begin{verbatim}
======================
Here's a report I made for `r gsub('/|.xlsx','',name)`
----------------------
```{r echo=F, include=F}
# import data
url <- paste0('http://beckmw.files.wordpress.com/2014/05', name)
dat <- read.xls(xls = url, sheet = 'Sheet1', perl = prl_pth)
# process data for tables/figs
dat <- proc_fun(dat)
# model of data
mod <- mod_fun(dat)
```
### Model summary
```{r results='asis', echo=F}
print.xtable(xtable(mod, digits = 2), type = 'html')
```
### Figure of restoration and reference by year
```{r reg_fig, echo = F, fig.width = 5, fig.height = 3, dpi=200}
ggplot(dat, aes(x = Year, y = value, colour = variable)) +
geom_point() +
stat_smooth(method = 'lm')
```
\end{verbatim}
\end{block}
\end{frame}
%%%%%%
\begin{frame}[t, fragile]{Step 3}
Setup supporting docs/functions, i.e., .Rprofile, functions, report, master
\scriptsize
\begin{block}{master.r}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlcom{# file to process}
\hlstd{name} \hlkwb{<-} \hlstr{"/my_data.xlsx"}
\hlcom{# rmd to html}
\hlkwd{knit2html}\hlstd{(}\hlstr{"report.Rmd"}\hlstd{)}
\hlcom{# pandoc conversion of html to word doc}
\hlkwd{system}\hlstd{(}\hlkwd{paste0}\hlstd{(}\hlstr{"pandoc -o report.docx report.html"}\hlstd{))}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{block}
\end{frame}
%%%%%%
\begin{frame}[fragile]{Steps 4 - 6}
\small
After creating supporting documents in Project directory, final steps are completed by running `master.r'
\begin{itemize}
\item Step 4 - xls file imported using \texttt{gdata} package, implemented in `report.Rmd'
\item Step 5 - HTML document created by converting `report.Rmd' with \texttt{knit2html} in `master.r'
\item Step 6 - HTML document converted to Word with Pandoc by invoking system command
\end{itemize}
\begin{block}{master.r}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlcom{# file to process}
\hlstd{name} \hlkwb{<-} \hlstr{"/my_data.xlsx"}
\hlcom{# rmd to html}
\hlkwd{knit2html}\hlstd{(}\hlstr{"report.Rmd"}\hlstd{)}
\hlcom{# pandoc conversion of html to word doc}
\hlkwd{system}\hlstd{(}\hlkwd{paste0}\hlstd{(}\hlstr{"pandoc -o report.docx report.html"}\hlstd{))}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{block}
\end{frame}
\end{document}