1
- # ' Explain a taxon name using formative elements
1
+ # ' @title Explain a taxon name using formative elements
2
2
# '
3
3
# ' @param x a Subgroup, Great Group, Suborder or Order-level taxonomic name; matching is exact and case-insensitive
4
4
# ' @param format output format: 'text' | 'html'
@@ -27,9 +27,16 @@ explainST <- function(x, format = c('text', 'html'), viewer = TRUE) {
27
27
x.lvl <- taxon_to_level(x )
28
28
29
29
# no-match NULL data object
30
- empty <- list (defs = data.frame (element = " " , derivation = " " ,
31
- connotation = " " , simplified = NA , link = NA ),
32
- char.index = 0 )
30
+ empty <- list (
31
+ defs = data.frame (
32
+ element = " " ,
33
+ derivation = " " ,
34
+ connotation = " " ,
35
+ simplified = NA ,
36
+ link = NA
37
+ ),
38
+ char.index = 0
39
+ )
33
40
34
41
if (! is.na(x.lvl ) && x.lvl == " order" ) {
35
42
# handle input of full order name e.g. "aridisols"
@@ -63,11 +70,11 @@ explainST <- function(x, format = c('text', 'html'), viewer = TRUE) {
63
70
} else {
64
71
x.sg <- empty
65
72
}
66
-
73
+
67
74
# TODO: family classes
68
75
69
- newline <- switch (format , text = ' \n ' , html = ' <br>' )
70
- whitespace <- switch (format , text = ' ' , html = ' ' )
76
+ newline <- switch (format , text = ' \n ' , html = ' <br>' )
77
+ whitespace <- switch (format , text = ' ' , html = ' ' )
71
78
72
79
main.style <- ' font-size: 85%; font-weight: bold;'
73
80
sub.style <- ' font-size: 85%; font-style: italic;'
@@ -81,10 +88,10 @@ explainST <- function(x, format = c('text', 'html'), viewer = TRUE) {
81
88
if (format == ' html' ) {
82
89
#
83
90
x.txt <- paste0(' <html><div style="padding: 5px; font-family: monospace; border: 1px solid grey; border-radius: 5px;">' ,
84
- ' <span style="' , main.style , ' ">' ,
85
- x ,
86
- ' </span>'
87
- )
91
+ ' <span style="' , main.style , ' ">' ,
92
+ x ,
93
+ ' </span>'
94
+ )
88
95
89
96
sg.txt <- paste0(' <span style="' , sub.style , ' ">' , sg.l , ' </span>' )
90
97
gg.txt <- paste0(' <span style="' , sub.style , ' ">' , gg.l , ' </span>' )
@@ -105,16 +112,16 @@ explainST <- function(x, format = c('text', 'html'), viewer = TRUE) {
105
112
# the taxon to explain, usually a subgroup
106
113
ex <- append(ex , x.txt )
107
114
108
- if (grepl(" [A-Za-z?]" , gsub(" " ," " ,sg.l [[2 ]])))
115
+ if (grepl(" [A-Za-z?]" , gsub(" " ," " , sg.l [[2 ]])))
109
116
ex <- append(ex , sg.txt )
110
117
111
- if (grepl(" [A-Za-z?]" , gsub(" " ," " ,gg.l [[2 ]])))
118
+ if (grepl(" [A-Za-z?]" , gsub(" " ," " , gg.l [[2 ]])))
112
119
ex <- append(ex , gg.txt )
113
120
114
- if (grepl(" [A-Za-z?]" , gsub(" " ," " ,so.l [[2 ]])))
121
+ if (grepl(" [A-Za-z?]" , gsub(" " ," " , so.l [[2 ]])))
115
122
ex <- append(ex , so.txt )
116
123
117
- if (grepl(" [A-Za-z?]" , gsub(" " ," " ,o.l [[2 ]])))
124
+ if (grepl(" [A-Za-z?]" , gsub(" " ," " , o.l [[2 ]])))
118
125
ex <- append(ex , o.txt )
119
126
120
127
if (format == ' html' ) {
@@ -126,13 +133,13 @@ explainST <- function(x, format = c('text', 'html'), viewer = TRUE) {
126
133
ex.char <- unlist(ex , recursive = TRUE )
127
134
128
135
# collapse to single character
129
- res <- paste(ex.char , collapse = newline )
136
+ res <- paste(ex.char , collapse = newline )
130
137
131
138
# put HTML output into viewer
132
139
if (format == ' html' && viewer ) {
133
140
viewer <- getOption(" viewer" , default = utils :: browseURL )
134
- tf <- tempfile(fileext = " .html" )
135
- cat(res , file = tf )
141
+ tf <- tempfile(fileext = " .html" )
142
+ cat(res , file = tf )
136
143
viewer(tf )
137
144
}
138
145
@@ -147,7 +154,7 @@ explainST <- function(x, format = c('text', 'html'), viewer = TRUE) {
147
154
148
155
# # TODO: wrap-text with newline if > width
149
156
150
- .printExplanation <- function (pos , txt , width = 100 , ws.char = ' ' ) {
157
+ .printExplanation <- function (pos , txt , width = 100 , ws.char = ' ' ) {
151
158
152
159
# convert factor to character if txt is factor
153
160
txt <- as.character(txt )
@@ -156,53 +163,53 @@ explainST <- function(x, format = c('text', 'html'), viewer = TRUE) {
156
163
# split explanation into a vector
157
164
txt <- strsplit(txt , split = ' ' )[[1 ]]
158
165
# placement of explanation
159
- idx <- seq(from = pos , to = pos + (length(txt ) - 1 ))
166
+ idx <- seq(from = pos , to = pos + (length(txt ) - 1 ))
160
167
# init whitespace, making room for very long explanation
161
- ws <- rep(ws.char , times = pmax(width , max(idx )))
168
+ ws <- rep(ws.char , times = pmax(width , max(idx )))
162
169
# insert text
163
170
ws [idx ] <- txt
164
171
} else {
165
172
return (" " )
166
173
}
167
174
# convert to character
168
- return (paste(ws , collapse = ' ' ))
175
+ return (paste(ws , collapse = ' ' ))
169
176
}
170
177
171
- .makeBars <- function (width = 100 , pos , ws.char = ' ' ) {
178
+ .makeBars <- function (width = 100 , pos , ws.char = ' ' ) {
172
179
# init whitespace
173
- ws <- rep(ws.char , times = width )
180
+ ws <- rep(ws.char , times = width )
174
181
# insert bars
175
182
ws [pos ] <- ' |'
176
183
177
184
# convert to character
178
- return (paste(ws , collapse = ' ' ))
185
+ return (paste(ws , collapse = ' ' ))
179
186
}
180
187
181
188
182
189
183
190
.soilOrderLines <- function (o , ws ) {
184
191
txt <- list ()
185
192
186
- txt [[1 ]] <- .makeBars(pos = o $ char.index , ws.char = ws )
187
- txt [[2 ]] <- .printExplanation(pos = o $ char.index , txt = o $ defs $ connotation , ws.char = ws )
193
+ txt [[1 ]] <- .makeBars(pos = o $ char.index , ws.char = ws )
194
+ txt [[2 ]] <- .printExplanation(pos = o $ char.index , txt = o $ defs $ connotation , ws.char = ws )
188
195
189
196
return (txt )
190
197
}
191
198
192
199
.subOrderLines <- function (o , so , ws ) {
193
200
txt <- list ()
194
201
195
- txt [[1 ]] <- .makeBars(pos = c(so $ char.index , o $ char.index ), ws.char = ws )
196
- txt [[2 ]] <- .printExplanation(pos = so $ char.index , txt = so $ defs $ connotation , ws.char = ws )
202
+ txt [[1 ]] <- .makeBars(pos = c(so $ char.index , o $ char.index ), ws.char = ws )
203
+ txt [[2 ]] <- .printExplanation(pos = so $ char.index , txt = so $ defs $ connotation , ws.char = ws )
197
204
198
205
return (txt )
199
206
}
200
207
201
208
.greatGroupLines <- function (o , so , gg , ws ) {
202
209
txt <- list ()
203
210
204
- txt [[1 ]] <- .makeBars(pos = c(gg $ char.index , so $ char.index , o $ char.index ), ws.char = ws )
205
- txt [[2 ]] <- .printExplanation(pos = gg $ char.index , txt = gg $ defs $ connotation , ws.char = ws )
211
+ txt [[1 ]] <- .makeBars(pos = c(gg $ char.index , so $ char.index , o $ char.index ), ws.char = ws )
212
+ txt [[2 ]] <- .printExplanation(pos = gg $ char.index , txt = gg $ defs $ connotation , ws.char = ws )
206
213
207
214
return (txt )
208
215
}
@@ -230,8 +237,8 @@ explainST <- function(x, format = c('text', 'html'), viewer = TRUE) {
230
237
while (i < length(sg.pos )+ 1 ) {
231
238
232
239
# add all bars
233
- txt [[j ]] <- .makeBars(pos = c(sg.pos.temp , gg $ char.index , so $ char.index , o $ char.index ), ws.char = ws )
234
- txt [[j + 1 ]] <- .printExplanation(pos = sg.pos.temp [1 ], txt = sg.defs [1 ], ws.char = ws )
240
+ txt [[j ]] <- .makeBars(pos = c(sg.pos.temp , gg $ char.index , so $ char.index , o $ char.index ), ws.char = ws )
241
+ txt [[j + 1 ]] <- .printExplanation(pos = sg.pos.temp [1 ], txt = sg.defs [1 ], ws.char = ws )
235
242
236
243
# nibble vectors
237
244
sg.pos.temp <- sg.pos.temp [- 1 ]
0 commit comments