/
xl_sheets_to_output.html
298 lines (238 loc) · 15 KB
/
xl_sheets_to_output.html
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
<!-- Generated by pkgdown: do not edit by hand -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Combine spreadsheets from excel workbooks and output common data structures. — xl_sheets_to_output • fgeo.tool</title>
<!-- jquery -->
<script src="https://code.jquery.com/jquery-3.1.0.min.js" integrity="sha384-nrOSfDHtoPMzJHjVTdCopGqIqeYETSXhZDFyniQ8ZHcVy08QesyHcnOUpMpqnmWq" crossorigin="anonymous"></script>
<!-- Bootstrap -->
<link href="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.7/united/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<!-- Font Awesome icons -->
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-T8Gy5hrqNKT+hzMclPo118YTQO6cYprQmhrYwIiQ/3axmI1hQomh7Ud2hPOy8SP1" crossorigin="anonymous">
<!-- clipboard.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.7.1/clipboard.min.js" integrity="sha384-cV+rhyOuRHc9Ub/91rihWcGmMmCXDeksTtCihMupQHSsi8GIIRDG0ThDc3HGQFJ3" crossorigin="anonymous"></script>
<!-- pkgdown -->
<link href="../pkgdown.css" rel="stylesheet">
<script src="../jquery.sticky-kit.min.js"></script>
<script src="../pkgdown.js"></script>
<meta property="og:title" content="Combine spreadsheets from excel workbooks and output common data structures. — xl_sheets_to_output" />
<meta property="og:description" content="These functions combine spreadsheets from excel workbooks into common data
structures. xl_sheets_to_csv() and xl_sheets_to_xl() write a .csv or
excel (.xlsx) file per workbook -- combining all spreadsheets.
xl_sheets_to_df outputs a list where each dataframes combines all
spreadsheeets of a workbook." />
<meta name="twitter:card" content="summary" />
<!-- mathjax -->
<script src='https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'></script>
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container template-reference-topic">
<header>
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<span class="navbar-brand">
<a class="navbar-link" href="../index.html">fgeo.tool</a>
<span class="label label-default" data-toggle="tooltip" data-placement="bottom" title="Released package">0.0.0.9002</span>
</span>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="../index.html">
<span class="fa fa-home fa-lg"></span>
</a>
</li>
<li>
<a href="../reference/index.html">Reference</a>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
Articles
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="../articles/read_df_write.html">Read, combine, identify, and write multiple files</a>
</li>
</ul>
</li>
<li>
<a href="../news/index.html">Changelog</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<a href="https://github.com/maurolepore">
<span class="fa fa-gavel"></span>
</a>
</li>
<li>
<a href="https://github.com/forestgeo/fgeo.tool">
<span class="fa fa-github"></span>
</a>
</li>
<li>
<a href="https://forestgeo.github.io/fgeo/">fgeo</a>
</li>
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
</header>
<div class="row">
<div class="col-md-9 contents">
<div class="page-header">
<h1>Combine spreadsheets from excel workbooks and output common data structures.</h1>
<small class="dont-index">Source: <a href='https://github.com/forestgeo/fgeo.tool/blob/master/R/xl_sheets_to_output.R'><code>R/xl_sheets_to_output.R</code></a></small>
<div class="hidden name"><code>xl_sheets_to_output.Rd</code></div>
</div>
<div class="ref-description">
<p>These functions combine spreadsheets from excel workbooks into common data
structures. <code>xl_sheets_to_csv()</code> and <code>xl_sheets_to_xl()</code> write a .csv or
excel (.xlsx) file per workbook -- combining all spreadsheets.
<code>xl_sheets_to_df</code> outputs a list where each dataframes combines all
spreadsheeets of a workbook.</p>
</div>
<pre class="usage"><span class='fu'>xl_sheets_to_csv</span>(<span class='no'>input_dir</span>, <span class='kw'>output_dir</span> <span class='kw'>=</span> <span class='st'>"./"</span>, <span class='kw'>first_census</span> <span class='kw'>=</span> <span class='fl'>FALSE</span>)
<span class='fu'>xl_sheets_to_xl</span>(<span class='no'>input_dir</span>, <span class='kw'>output_dir</span> <span class='kw'>=</span> <span class='st'>"./"</span>, <span class='kw'>first_census</span> <span class='kw'>=</span> <span class='fl'>FALSE</span>)
<span class='fu'>xl_sheets_to_df</span>(<span class='no'>input_dir</span>, <span class='kw'>first_census</span> <span class='kw'>=</span> <span class='fl'>FALSE</span>)</pre>
<h2 class="hasAnchor" id="arguments"><a class="anchor" href="#arguments"></a>Arguments</h2>
<table class="ref-arguments">
<colgroup><col class="name" /><col class="desc" /></colgroup>
<tr>
<th>input_dir</th>
<td><p>String giving the directory containing the excel workbooks
to read from.</p></td>
</tr>
<tr>
<th>output_dir</th>
<td><p>String giving the directory where to write .csv files to.</p></td>
</tr>
<tr>
<th>first_census</th>
<td><p>This argument tells these functions what sheets to expect
in the input.</p><ul>
<li><p>Use <code>TRUE</code> if this is your first census. The expected input must have
sheets (1) "root", (2) "multi_stems", (3) "secondary_stems", and
(4) "single_stems".</p></li>
<li><p>Use <code>FALSE</code> (default) if this is not your first census. The expected
input must have sheets (1) "root", (2) "original_stems", (3)
"new_secondary_stems", and (4) "recruits".</p></li>
</ul></td>
</tr>
</table>
<h2 class="hasAnchor" id="value"><a class="anchor" href="#value"></a>Value</h2>
<p>Writes one .csv file for each workbook.</p>
<h2 class="hasAnchor" id="details"><a class="anchor" href="#details"></a>Details</h2>
<p>This is a rigid function with a very specific goal: To process data from a
specific sampling software -- FastField. Specifically, this is what this
function does:</p><ul>
<li><p>Reads each spreadsheet from each workbook and map it to a dataframe.</p></li>
<li><p>Lowercases and links the names of each dataframe.</p></li>
<li><p>Keeps only these dataframes: (1) original_stems, (2) new_secondary_stems,
and (3) "recruits".</p></li>
<li><p>Dates the data by <code>submission_id</code> (<code>date</code> comes from the spreadsheet
<code>root</code>).</p></li>
<li><p>Lowercases and links the names of each dataframe-variable.</p></li>
<li><p>Drops fake stems.</p></li>
<li><p>Output a common data structure of your choice.</p></li>
</ul>
<h2 class="hasAnchor" id="acknowledgment"><a class="anchor" href="#acknowledgment"></a>Acknowledgment</h2>
<p>Sabrina Russo helped to make these functions useful with first censuses.</p>
<h2 class="hasAnchor" id="examples"><a class="anchor" href="#examples"></a>Examples</h2>
<pre class="examples"><div class='input'><span class='fu'>library</span>(<span class='no'>fs</span>)
<span class='fu'>library</span>(<span class='no'>readr</span>)
<span class='fu'>library</span>(<span class='no'>readxl</span>)
<span class='co'># NOT A FIRST CENSUS</span>
<span class='co'># Path to the folder I want to read excel files from</span>
<span class='no'>input_dir</span> <span class='kw'><-</span> <span class='fu'>dirname</span>(<span class='fu'><a href='example_path.html'>example_path</a></span>(<span class='st'>"two_files/new_stem_1.xlsx"</span>))
<span class='no'>input_dir</span></div><div class='output co'>#> [1] "C:/Users/LeporeM/Dropbox/git_repos/fgeo.tool/inst/extdata/two_files"</div><div class='input'>
<span class='co'># Files I want to read</span>
<span class='fu'>dir</span>(<span class='no'>input_dir</span>, <span class='kw'>pattern</span> <span class='kw'>=</span> <span class='st'>"xlsx"</span>)</div><div class='output co'>#> [1] "new_stem_1.xlsx" "new_stem_2.xlsx"</div><div class='input'>
<span class='co'># Path to the folder I want to write .csv files to</span>
<span class='no'>output_dir</span> <span class='kw'><-</span> <span class='fu'>tempdir</span>()
<span class='co'># Output a csv file</span>
<span class='fu'>xl_sheets_to_csv</span>(<span class='no'>input_dir</span>, <span class='no'>output_dir</span>)
<span class='co'># Confirm</span>
<span class='fu'><a href='http://fs.r-lib.org/reference/path_file.html'>path_file</a></span>(<span class='fu'><a href='http://fs.r-lib.org/reference/dir_ls.html'>dir_ls</a></span>(<span class='no'>output_dir</span>, <span class='kw'>regexp</span> <span class='kw'>=</span> <span class='st'>"new_stem.*csv$"</span>))</div><div class='output co'>#> new_stem_1.csv new_stem_2.csv </div><div class='input'>
<span class='co'># Also possible to output excel and a list of dataframe. See next section.</span>
<span class='co'># FIRST CENSUS</span>
<span class='no'>input_dir</span> <span class='kw'><-</span> <span class='fu'>dirname</span>(<span class='fu'><a href='example_path.html'>example_path</a></span>(<span class='st'>"first_census/census.xlsx"</span>))
<span class='co'># As a reminder you'll get a warning of missing sheets</span>
<span class='co'># Output list of dataframes (one per input workbook -- here only one)</span>
<span class='fu'>xl_sheets_to_df</span>(<span class='no'>input_dir</span>, <span class='kw'>first_census</span> <span class='kw'>=</span> <span class='fl'>TRUE</span>)</div><div class='output co'>#> <span class='warning'>Warning: `.x` has no dataframe new_secondary_stems</span></div><div class='output co'>#> <span class='warning'>Warning: `.x` has no dataframe recruits</span></div><div class='output co'>#> $census.xlsx
#> # A tibble: 49 x 15
#> submission_id quadrat tag stem_tag species species_code dbh status codes
#> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
#> 1 c01b37d5-2c3~ 0816 007 1 Americ~ TILIAM 51.2 LI M
#> 2 efd8e991-2e3~ 0916 004 1 Americ~ TILIAM 31.6 LI M
#> 3 efd8e991-2e3~ 0916 007 1 Ironwo~ OSTRVI 10.2 LI M
#> 4 efd8e991-2e3~ 0916 008 1 Ironwo~ OSTRVI 2.7 LI M;L
#> 5 efd8e991-2e3~ 0916 010 1 Hackbe~ CELTOC 25.9 LI M
#> 6 efd8e991-2e3~ 0916 012 1 Bur Oak QUERMA 35.2 LI M;FS~
#> 7 7997596a-c94~ 1017 001 1 Americ~ TILIAM 65.3 LI M;FS~
#> 8 572ddae1-d02~ 0816 002 1 Hackbe~ CELTOC 45.1 LI M
#> 9 c01b37d5-2c3~ 0816 007 2 TILIAM <NA> 42.4 LI S;L
#> 10 c01b37d5-2c3~ 0816 007 3 TILIAM <NA> 39.8 LI S
#> # ... with 39 more rows, and 6 more variables: notes <chr>, pom <chr>,
#> # sheet <chr>, section_id <chr>, unique_stem <chr>, date <chr>
#> </div><div class='input'>
<span class='co'># Output excel</span>
<span class='fu'>xl_sheets_to_xl</span>(<span class='no'>input_dir</span>, <span class='no'>output_dir</span>, <span class='kw'>first_census</span> <span class='kw'>=</span> <span class='fl'>TRUE</span>)</div><div class='output co'>#> <span class='warning'>Warning: `.x` has no dataframe new_secondary_stems</span></div><div class='output co'>#> <span class='warning'>Warning: `.x` has no dataframe recruits</span></div><div class='input'><span class='co'># Read back</span>
<span class='no'>filename</span> <span class='kw'><-</span> <span class='fu'><a href='http://fs.r-lib.org/reference/path.html'>path</a></span>(<span class='no'>output_dir</span>, <span class='st'>"census.xlsx"</span>)
<span class='no'>out</span> <span class='kw'><-</span> <span class='fu'><a href='http://www.rdocumentation.org/packages/readxl/topics/read_excel'>read_excel</a></span>(<span class='no'>filename</span>)
<span class='fu'>str</span>(<span class='no'>out</span>, <span class='kw'>give.attr</span> <span class='kw'>=</span> <span class='fl'>FALSE</span>)</div><div class='output co'>#> Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 49 obs. of 15 variables:
#> $ submission_id: chr "c01b37d5-2c30-4086-b4a1-78327dfa0a9a" "efd8e991-2e34-40c8-b503-44def6e542d9" "efd8e991-2e34-40c8-b503-44def6e542d9" "efd8e991-2e34-40c8-b503-44def6e542d9" ...
#> $ quadrat : chr "0816" "0916" "0916" "0916" ...
#> $ tag : chr "007" "004" "007" "008" ...
#> $ stem_tag : chr "1" "1" "1" "1" ...
#> $ species : chr "American Basswood/Linden" "American Basswood/Linden" "Ironwood" "Ironwood" ...
#> $ species_code : chr "TILIAM" "TILIAM" "OSTRVI" "OSTRVI" ...
#> $ dbh : chr "51.2" "31.6" "10.2" "2.7" ...
#> $ status : chr "LI" "LI" "LI" "LI" ...
#> $ codes : chr "M" "M" "M" "M;L" ...
#> $ notes : chr NA NA NA "In brush pile" ...
#> $ pom : chr "1.3" "1.3" "1.3" "1.3" ...
#> $ sheet : chr "multi_stems" "multi_stems" "multi_stems" "multi_stems" ...
#> $ section_id : chr NA NA NA NA ...
#> $ unique_stem : chr "007_1" "004_1" "007_1" "008_1" ...
#> $ date : chr "2018.05.14" "2018.05.14" "2018.05.14" "2018.05.14" ...</div></pre>
</div>
<div class="col-md-3 hidden-xs hidden-sm" id="sidebar">
<h2>Contents</h2>
<ul class="nav nav-pills nav-stacked">
<li><a href="#arguments">Arguments</a></li>
<li><a href="#value">Value</a></li>
<li><a href="#details">Details</a></li>
<li><a href="#acknowledgment">Acknowledgment</a></li>
<li><a href="#examples">Examples</a></li>
</ul>
<h2>Author</h2>
Mauro Lepore and Jessica Shue.
</div>
</div>
<footer>
<div class="copyright">
<p>Developed by <a href='https://github.com/maurolepore'>Mauro Lepore</a>, <a href='http://www.forestgeo.si.edu/'>CTFS-ForestGEO</a>.</p>
</div>
<div class="pkgdown">
<p>Site built with <a href="http://pkgdown.r-lib.org/">pkgdown</a>.</p>
</div>
</footer>
</div>
</body>
</html>