/
read_fgeo.R
198 lines (187 loc) · 5.43 KB
/
read_fgeo.R
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
read_fgeo <- function(col_types) {
function(file, delim = NULL, na = c("", "NA", "NULL"), ...) {
delim <- delim %||% guess_comma_or_tab(file, names(col_types))
# The point of this function is to handle rather than warn/inform issues
dfm <- suppressMessages(suppressWarnings(
read_delim_(delim, file = file, col_types = col_types, na = na, ...)
))
# If input has rownames, we end up with more columns than we need
result <- dfm[names(col_types)]
readr::type_convert(result, col_types = col_types)
}
}
read_delim_ <- function(delim, file, col_types, na, ...) {
dfm <- switch(delim,
"," = readr::read_csv(
file = file, col_types = readr::cols(.default = "c"), na = na, ...
),
"\t" = readr::read_tsv(
file = file, col_types = readr::cols(.default = "c"), na = na, ...
),
abort("Unexpected `delim`.")
)
}
#' Import _ViewFullTable_ or _ViewTaxonomy_ data from a .tsv or .csv file.
#'
#' [read_vft()] and [read_taxa()] help you to read _ViewFullTable_ and
#' _ViewTaxonomy_ data from text files delivered by the ForestGEO database.
#' These functions avoid common problems about column separators, missing
#' values, column names, and column types.
#'
#' @section Acknowledgments:
#' Thanks to Shameema Jafferjee Esufali for inspiring the feature that
#' automatically detects `delim` (issue 65).
#'
#' @param file A path to a file.
#' @inheritParams readr::read_delim
#' @param delim Single character used to separate fields within a record. The
#' default (`delim = NULL`) is to guess between comma or tab (`","` or `"\t"`).
#' @param ... Other arguments passed to [readr::read_delim()].
#'
#' @seealso [readr::read_delim()], [type_vft()], [type_taxa()].
#'
#' @return A [tibble][tibble::tibble-package].
#'
#' @examples
#' assert_is_installed("fgeo.x")
#' library(fgeo.x)
#'
#' example_path()
#'
#' file_vft <- example_path("view/vft_4quad.csv")
#' read_vft(file_vft)
#'
#' file_taxa <- example_path("view/taxa.csv")
#' read_taxa(file_taxa)
#' @family functions to read text files delivered by ForestgGEO's database
#' @family functions to import a single file of ForestGEO data
#' @family functions to import ForestGEO data
#' @export
read_vft <- read_fgeo(col_types = type_vft())
#' @rdname read_vft
#' @export
read_taxa <- read_fgeo(col_types = type_taxa())
#' Help to read ForestGEO data safely, with consistent columns type.
#'
#' @description
#' A common cause of problems is feeding functions with data which columns are
#' not all of the expected type. The problem often begins when reading data from
#' a text file with functions such as [utils::read.csv()],
#' [utils::read.delim()], and friends -- which commonly guess wrongly the column
#' type that you more likely expect. These common offenders are strongly
#' discouraged; instead consider using `readr::read_csv()`, `readr::read_tsv()`,
#' and friends, which guess column types correctly much more often than their
#' analogs from the __utils__ package.
#'
#' @description
#' `type_vft()` and `type_taxa()` help you to read data more safely by
#' explicitly specifying what type to expect from each column of known datasets.
#' These functions output the specification of column types used internally by
#' [read_vft()] and [read_taxa()]:
#' * `type_vft():` Type specification for _ViewFullTable_.
#' * `type_taxa():` Type specification for _ViewFullTaxonomy_.
#'
#' @details
#' Types reference (for more details see [read_delim()]):
#' * c = character,
#' * i = integer,
#' * n = number,
#' * d = double,
#' * l = logical,
#' * D = date,
#' * T = date time,
#' * t = time,
#' * ? = guess,
#' * or _/- to skip the column.'.
#'
#' @seealso [readr::read_delim()].
#'
#' @return A list.
#'
#' @examples
#' assert_is_installed("fgeo.x")
#' library(fgeo.x)
#' library(readr)
#'
#' str(type_vft())
#'
#' read_csv(example_path("view/vft_4quad.csv"), col_types = type_vft())
#'
#' str(type_taxa())
#'
#' read_csv(example_path("view/taxa.csv"), col_types = type_taxa())
#' @family functions to operate on column types
#' @family functions to read text files delivered by ForestgGEO's database
#' @family functions to import/export ForestGEO data
#' @keywords internal
#' @export
type_vft <- function() {
list(
DBHID = "i",
PlotName = "c",
PlotID = "i",
Family = "c",
Genus = "c",
SpeciesName = "c",
Mnemonic = "c",
Subspecies = "c",
SpeciesID = "i",
SubspeciesID = "c",
QuadratName = "c",
QuadratID = "i",
PX = "d",
PY = "d",
QX = "d",
QY = "d",
TreeID = "i",
Tag = "c",
StemID = "i",
StemNumber = "i",
StemTag = "i",
PrimaryStem = "c",
CensusID = "i",
PlotCensusNumber = "i",
DBH = "d",
HOM = "d",
ExactDate = "D",
Date = "i",
ListOfTSM = "c",
HighHOM = "i",
LargeStem = "c",
Status = "c"
)
}
#' @rdname type_vft
#' @export
type_taxa <- function() {
list(
ViewID = "i",
SpeciesID = "i",
SubspeciesID = "c",
Family = "c",
Mnemonic = "c",
Genus = "c",
SpeciesName = "c",
Rank = "c",
Subspecies = "c",
Authority = "c",
IDLevel = "c",
subspMnemonic = "c",
subspAuthority = "c",
FieldFamily = "c",
Lifeform = "c",
Description = "c",
wsg = "d",
wsglevel = "c",
ListOfOldNames = "c",
Specimens = "c",
Reference = "c"
)
}
guess_comma_or_tab <- function(file, nms) {
comma <- suppressWarnings(suppressMessages(readr::read_csv(file, n_max = 0)))
if (all(nms %in% names(comma))) {
return(",")
}
"\t"
}