Skip to content

Commit 1535182

Browse files
committed
Add mode
1 parent 81f7e70 commit 1535182

5 files changed

Lines changed: 98 additions & 24 deletions

File tree

R/allocate.basic.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ allocate.basic <- function(data,
8686
dist.mat = NULL,
8787
quantitative = NULL,
8888
qualitative = NULL,
89-
log.base = log.base)
89+
log.base = log.base,
90+
mode = "alloc")
9091

9192
method <- match.arg(method)
9293

R/allocate.distance.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ allocate.distance <- function(data, names, group,
154154
dist.mat = dist.mat,
155155
quantitative = NULL,
156156
qualitative = NULL,
157-
log.base = log.base)
157+
log.base = log.base,
158+
mode = "alloc")
158159

159160
method <- match.arg(method)
160161
metric <- match.arg(metric)

R/allocate.diversity.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ allocate.diversity <- function(data, names, group,
9090
dist.mat = dist.mat,
9191
quantitative = NULL,
9292
qualitative = NULL,
93-
log.base = log.base)
93+
log.base = log.base,
94+
mode = "alloc")
9495

9596
method <- match.arg(method)
9697
metric <- match.arg(metric)

R/checks.sample.core.R

Lines changed: 78 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@
99
#'
1010
#' @template general-arg
1111
#' @template qualquant-arg
12+
#' @template dist-arg
13+
#' @template log-arg
1214
#
13-
checks.sample.core <- function(data, names, quantitative = NULL,
15+
checks.sample.core <- function(data, names,
16+
size, group,
17+
quantitative = NULL,
1418
qualitative = NULL,
1519
dist.mat = NULL,
16-
method, size, group,
17-
log.base = NULL
18-
# selected
19-
) {
20+
log.base = NULL,
21+
always.selected = NULL,
22+
mode = C("alloc", "sel")) {
2023

2124
# Declare nulls ----
2225

@@ -80,7 +83,7 @@ checks.sample.core <- function(data, names, quantitative = NULL,
8083
}
8184

8285
# check if 'size' argument is numeric vector of unit length
83-
if (!is.null(size)) {
86+
if (mode == "alloc") {
8487
if (!(is.numeric(size) && length(size) == 1)) {
8588
stop('"size" should be a numeric vector of unit length.')
8689
}
@@ -211,22 +214,83 @@ checks.sample.core <- function(data, names, quantitative = NULL,
211214
# Size ----
212215

213216
# check if 'size' is a proportion between 0 and 1
214-
if (size <= 0 || size >= 1) {
215-
stop('"size" should be a proportion between 0 and 1.')
217+
if (mode == "alloc") {
218+
if (size <= 0 || size >= 1) {
219+
stop('"size" should be a proportion between 0 and 1.')
220+
}
216221
}
217222

218223
# Log base ----
219224

220-
if (!is.numeric(log.base) || length(log.base) != 1 || is.na(log.base)) {
221-
stop('"log.base" must be a single numeric value.')
225+
if (!is.null(log.base)) {
226+
227+
if (!is.numeric(log.base) || length(log.base) != 1 || is.na(log.base)) {
228+
stop('"log.base" must be a single numeric value.')
229+
}
230+
231+
if (log.base <= 0) {
232+
stop('"base" must be positive.')
233+
}
234+
235+
if (log.base == 1) {
236+
stop('"log.base" of 1 is undefined.')
237+
}
238+
222239
}
223240

224-
if (log.base <= 0) {
225-
stop('"base" must be positive.')
241+
# Always selected ----
242+
243+
if (mode == "sel" & !is.null(always.selected)) {
244+
# check if 'always.selected' is a character vector
245+
if (!is.character(always.selected)) {
246+
stop('"always.selected" should be a character vector.')
247+
}
248+
249+
# check if always.selected is present in the entire set
250+
if (any(!(always.selected %in% data[, names]))) {
251+
alsel_miss <- always.selected[!(always.selected %in% data[, names])]
252+
stop(paste('The following accession(s) specified in "always.selected" ',
253+
'are not present in "data":\n',
254+
paste(alsel_miss, collapse = ", "),
255+
sep = ""))
256+
}
226257
}
227258

228-
if (log.base == 1) {
229-
stop('"log.base" of 1 is undefined.')
259+
# Allocation vector ----
260+
261+
if (mode == "sel") {
262+
263+
nm <- names(alloc)
264+
lv <- levels(data[, group])
265+
266+
if (!setequal(nm, lv)) {
267+
268+
only_in_alloc <- setdiff(nm, lv)
269+
only_in_group <- setdiff(lv, nm)
270+
271+
msg <- paste0(
272+
'Mismatch between "alloc" names and levels of "group" column in data.\n\n',
273+
if (length(only_in_alloc) > 0)
274+
paste0(
275+
'Present in "alloc" but not in "group" column levels: ',
276+
paste(shQuote(only_in_alloc), collapse = ', '),
277+
'\n'
278+
) else '',
279+
if (length(only_in_group) > 0)
280+
paste0(
281+
'Present in "group" column levels but not in "alloc": ',
282+
paste(shQuote(only_in_group), collapse = ', '),
283+
'\n'
284+
) else ''
285+
)
286+
287+
stop(msg, call. = FALSE)
288+
289+
}
230290
}
231291

232292
}
293+
294+
295+
296+

man/checks.sample.core.Rd

Lines changed: 14 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)