/
finbif_informal_groups.R
161 lines (99 loc) · 3.06 KB
/
finbif_informal_groups.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
#' FinBIF informal groups
#'
#' Display the informal taxonomic groups used in the FinBIF database.
#'
#' @aliases fb_informal_groups
#'
#' @param group Character. Optional, if supplied only display this top-level
#' group and its subgroups.
#' @param limit Integer. The maximum number top-level informal groups (and their
#' sub-groups) to display.
#' @param quiet Logical. Return informal group names without displaying them.
#' @param locale Character. One of the supported two-letter ISO 639-1 language
#' codes. Current supported languages are English, Finnish and Swedish. For
#' data where more than one language is available the language denoted by
#' `locale` will be preferred while falling back to the other languages in the
#' order indicated above.
#' @return A character vector (invisibly).
#' @examples \dontrun{
#'
#' # Display the informal taxonomic groups used by FinBIF
#' finbif_informal_groups()
#'
#' }
#' @export
finbif_informal_groups <- function(
group,
limit = 5,
quiet = FALSE,
locale = getOption("finbif_locale")
) {
if (!locale %in% sysdata("supported_langs")) {
locale <- "en"
}
cache <- getOption("finbif_use_cache")
request <- list(
path = "informal-taxon-groups/tree",
query = list(pageSize = 1000L, lang = locale),
cache = infer_cache(cache)
)
informal_grps <- api_get(request)
informal_grps <- informal_grps[[c("content", "results")]]
grps <- vapply(informal_grps, getElement, "", "name")
if (!missing(group)) {
groups <- grps == group
stopifnot("Group not found" = any(groups))
grp <- which(groups)
informal_grps <- informal_grps[grp]
}
if (!quiet) {
obj <- list(informal_grps = informal_grps, cntr = 0L, limit = limit)
print_informal_group(obj)
}
informal_grps <- unlist(informal_grps)
informal_grps <- unname(informal_grps)
informal_grps <- grep("^MVL\\.", informal_grps, invert = TRUE, value = TRUE)
class(informal_grps) <- "translation"
invisible(informal_grps)
}
#' @noRd
print_informal_group <- function(obj) {
informal_grps <- obj[["informal_grps"]]
times <- max(obj[["cntr"]] - 1L, 0L)
pad <- rep(" ", times)
cntr1 <- obj[["cntr"]] + 1L
cntr2 <- 1L
len <- length(informal_grps)
limit <- min(obj[["limit"]], len)
for (informal_grp in informal_grps) {
branch <- ""
is_branch <- cntr1 > 1L
if (is_branch) {
branch <- " --"
}
if (!is_branch && cntr2 > limit) {
sq <- seq_len(cntr2 - 1L)
extras <- informal_grps[-sq]
extras <- unlist(extras)
extras <- grep("MVL\\.", extras)
extra <- length(extras)
s <- ""
if (extra > 1L) {
s <- "s"
}
cat("...", extra, " more group", s, sep = "")
break
}
cntr2 <- cntr2 + 1L
cat(pad, branch, informal_grp[["name"]], "\n", sep = "")
if ("hasSubGroup" %in% names(informal_grp)) {
obj_i <- list(
informal_grps = informal_grp[["hasSubGroup"]],
cntr = cntr1,
limit = limit
)
print_informal_group(obj_i)
}
}
invisible(NULL)
}