Skip to content

Commit 2a4eb71

Browse files
committed
refactored roxygen2 and improved error handling
1 parent 3179029 commit 2a4eb71

53 files changed

Lines changed: 1198 additions & 580 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

NEWS.md

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,7 @@
1-
# OmicFlow 1.6.0.9003
1+
# OmicFlow 1.6.0.9005
22

33
## Changed
4-
- Re-factoring `roxygen2` code & documentation style & error handling.
5-
- Follow order of `tests/testthat.R` file
6-
- Finished:
7-
- test-omics.R
8-
- test-omics-transformation.R
9-
- test-omics-subsetting.R
10-
- test-omics-rankstat.R
11-
- test-omics-ordination.R
12-
- test-omics-feature_merge.R
13-
- test-omics-composition.R
14-
- test-omics-alpha_diversity.R
15-
- test-omics-autoFlow.R [SKIP, finish later with issue #43]
16-
- test-omics-foldchange.R [SKIP, finish later with issue #43]
17-
- test-metagenomics.R
18-
- test-metagenomics-write_biom.R
19-
- test-proteomics.R
20-
- test-util-colormap.R
21-
- test-util-column_exists.R
22-
- test-util-combine_conditions.R
23-
- test-util-matrix_to_dtable.R
24-
- test-util-wholenumber.R
25-
- test-metrics-diversity.R
26-
- test-metrics-hill_taxa.R
27-
- test-metrics-distances.R
28-
- test-statistics-pairwise_stats.R
29-
- test-visualisation-diversity_plot.R [TODO]
30-
- test-visualisation-ordination_plot.R [TODO]
31-
- test-visualisation-plot_pairwise_stats.R [TODO]
32-
- test-visualisation-volcano_plot.R [TODO]
4+
- Re-factoring and improving `roxygen2` code, documentation style and error handling.
335
- TODO: Add `tryCatch` where neccessary
346
- Added `tryCatch` and better error handling in `omics$sample_subset`, `omics$feature_subset` and `omics$samplepair_subset`.
357
- Replaced `jsonlite` by `yyjsonr` functionality, much faster and cleaner code.

R/bray.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ bray <- function(x, weighted = TRUE, threads = 1) {
5959
if (length(threads) != 1) {
6060
cli::cli_abort("{.val threads} must be a single whole number.")
6161
} else if (!is.wholenumber(threads)) {
62-
cli::cli_abort("{.val {threads}} must be a whole number.")
62+
cli::cli_abort("{.val threads} must be a whole number.")
6363
}
6464

6565
## MAIN

R/canberra.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ canberra <- function(x, weighted = TRUE, threads = 1) {
5757
if (length(threads) != 1) {
5858
cli::cli_abort("{.val threads} must be a single whole number.")
5959
} else if (!is.wholenumber(threads)) {
60-
cli::cli_abort("{.val {threads}} must be a whole number.")
60+
cli::cli_abort("{.val threads} must be a whole number.")
6161
}
6262

6363
## MAIN

R/colormap.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,18 @@ colormap <- function(data,
2929
OPTIONS <- rownames(RColorBrewer::brewer.pal.info)
3030

3131
if (!inherits(data, "data.frame") && !inherits(data, "data.table"))
32-
cli::cli_abort("Data must be a {.cls data.frame} or {.cls data.table}.")
32+
cli::cli_abort("{.val data} must be a {.cls data.frame} or {.cls data.table}.")
3333

3434
if (!is.character(col_name) || length(col_name) != 1) {
35-
cli::cli_abort("{.val {col_name}} needs to contain characters with length of 1.")
35+
cli::cli_abort("{.val col_name} needs to contain characters with length of 1.")
3636
} else if (!column_exists(col_name, data)) {
37-
cli::cli_abort("The {.val {col_name}} column does not exist in the provided data.")
37+
cli::cli_abort("The {.val col_name} column does not exist in the provided data.")
3838
}
3939

4040
if (!is.character(Brewer.palID) || length(Brewer.palID) != 1) {
41-
cli::cli_abort("The {.val {Brewer.palID}} needs to contain characters with length of 1.")
41+
cli::cli_abort("The {.val Brewer.palID} needs to contain characters with length of 1.")
4242
} else if (!c(Brewer.palID %in% OPTIONS)) {
43-
cli::cli_abort("{.val {Brewer.palID}} is not a valid Brewer pal ID. \nValid options: {.val {OPTIONS}}.")
43+
cli::cli_abort("{.val Brewer.palID} is not a valid Brewer pal ID. \nValid options: {.val {OPTIONS}}.")
4444
}
4545

4646
## MAIN

R/composition_plot.R

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
#' The function is compatible with the class \link{omics} method \code{composition()}.
55
#'
66
#' @param data A \link[base]{data.frame} or \link[data.table]{data.table}.
7-
#' @param palette An object with names and hexcode or color names, see \link{colormap}.
8-
#' @param feature_rank A character variable of the feature column.
9-
#' @param title_name A character to set the \code{ggtitle} of the \link[ggplot2]{ggplot}, (Default: NULL).
10-
#' @param group_by A character variable to aggregate the stacked bars by group (Default: NULL).
7+
#' @param palette A \link[stats]{setNames}, see \link{colormap}.
8+
#' @param feature_rank A column name containing the feature names.
9+
#' @param title_name A character to set the \code{ggtitle} of the \link[ggplot2]{ggplot}, (default: \code{NULL}).
10+
#' @param group_by A character variable to aggregate the stacked bars by group (default: \code{NULL}).
1111
#' @return A \link[ggplot2]{ggplot2} object to be further modified
1212
#'
1313
#' @examples
@@ -75,30 +75,33 @@ composition_plot <- function(data,
7575
#--------------------------------------------------------------------#
7676

7777
if (!inherits(data, "data.frame") && !inherits(data, "data.table"))
78-
cli::cli_abort("Data must be a {.cls data.frame} or {.cls data.table}.")
78+
cli::cli_abort("{.val data} must be a {.cls data.frame} or {.cls data.table}.")
7979

8080
if (!is.character(palette))
81-
cli::cli_abort("{.val {palette}} needs to contain characters.")
81+
cli::cli_abort("{.val palette} needs to contain characters.")
8282

83-
if (!is.character(feature_rank) && length(feature_rank) != 1) {
84-
cli::cli_abort("{.val {feature_rank}} needs to contain characters with length of 1.")
83+
if (!is.character(feature_rank) || length(feature_rank) != 1) {
84+
cli::cli_abort("{.val feature_rank} needs to contain characters with length of 1.")
8585
} else if (!column_exists(feature_rank, data)) {
86-
cli::cli_abort("The {.val {feature_rank}} column does not exist in the provided {.arg data}.")
86+
cli::cli_abort("The {.val {feature_rank}} column does not exist in the provided {.val data}.")
8787
}
8888

89-
if (!is.null(title_name) && !is.character(title_name))
90-
cli::cli_abort("{.val {title_name}} needs to be of type character.")
89+
if (!is.null(title_name)) {
90+
if (!is.character(title_name) || length(title_name) != 1) {
91+
cli::cli_abort("{.val title_name} must be a character and of length 1")
92+
}
93+
}
9194

9295
if (!is.null(group_by)) {
93-
if (!is.character(group_by) && length(group_by) != 1) {
94-
cli::cli_abort("{.val {group_by}} must be a character and of length 1")
96+
if (!is.character(group_by) || length(group_by) != 1) {
97+
cli::cli_abort("{.val group_by} must be a character and of length 1")
9598
} else if (!column_exists(group_by, data)) {
96-
cli::cli_abort("The specified {.val {group_by}} does not exist in the provided {.arg data}.")
99+
cli::cli_abort("The specified {.val {group_by}} does not exist in the provided {.val data}.")
97100
}
98101
}
99102

100103
if (!column_exists("SAMPLE_ID", data))
101-
cli::cli_abort("{.arg SAMPLE_ID} needs to exist within the provided {.arg data}.")
104+
cli::cli_abort("{.arg SAMPLE_ID} needs to exist within the provided {.val data}.")
102105

103106
## MAIN
104107
#--------------------------------------------------------------------#

R/cosine.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ cosine <- function(x, weighted = TRUE, threads = 1) {
5757
if (length(threads) != 1) {
5858
cli::cli_abort("{.val threads} must be a single whole number.")
5959
} else if (!is.wholenumber(threads)) {
60-
cli::cli_abort("{.val {threads}} must be a whole number.")
60+
cli::cli_abort("{.val threads} must be a whole number.")
6161
}
6262

6363
## MAIN

R/diversity.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ diversity <- function(x,
6666

6767
OPTIONS <- c("shannon", "simpson", "invsimpson")
6868
if (!is.character(metric) || length(metric) != 1) {
69-
cli::cli_abort("{.val {metric}} needs to contain characters with length of 1.")
69+
cli::cli_abort("{.val metric} needs to contain characters with length of 1.")
7070
} else if (!metric %in% OPTIONS) {
71-
cli::cli_abort("{.val {metric}} is not a valid metric. Valid options: <{.val {OPTIONS}}>")
71+
cli::cli_abort("{.val {metric}} is not a valid {.arg metric}.\nValid options: <{.val {OPTIONS}}>")
7272
}
7373

7474
## MAIN

R/diversity_plot.R

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
#' @param data A \link[base]{data.frame} or \link[data.table]{data.table} computed from \link{diversity}.
66
#' @param values A column name of a continuous variable.
77
#' @param col_name A column name of a categorical variable.
8-
#' @param group_by A column name to perform grouped statistical test (default: NULL).
8+
#' @param group_by A column name to perform grouped statistical test (default: \code{NULL}).
99
#' @param palette An object with names and hexcode or color names, see \link{colormap}.
10-
#' @param method A character variable indicating what method is used to compute the diversity.
11-
#' @param paired A boolean value to perform paired analysis in \link[stats]{wilcox.test}.
10+
#' @param method A character variable indicating what method is used to compute the diversity (default: \code{"custom"}).
11+
#' @param paired A boolean value to perform paired analysis in \link[stats]{wilcox.test} (default: \code{FALSE}).
1212
#' @param p.adjust.method A character variable to specify the p.adjust.method to be used (Default: fdr).
1313
#' @return A \link[ggplot2]{ggplot2} object to be further modified
1414
#'
@@ -76,7 +76,7 @@ diversity_plot <- function(
7676
col_name,
7777
group_by = NULL,
7878
palette,
79-
method,
79+
method = "custom",
8080
paired = FALSE,
8181
p.adjust.method = "fdr"
8282
) {
@@ -85,45 +85,49 @@ diversity_plot <- function(
8585
#--------------------------------------------------------------------#
8686

8787
if (!inherits(data, "data.frame") && !inherits(data, "data.table"))
88-
cli::cli_abort("Data must be a {.cls data.frame} or {.cls data.table}.")
89-
90-
if (!is.character(palette))
91-
cli::cli_abort("{.val {palette}} needs to contain characters.")
92-
93-
if (!is.character(method)) {
94-
cli::cli_abort("{.val {method}} needs to be a character {.cls vector}.")
95-
}
96-
97-
if (!is.character(values) && length(values) != 1) {
98-
cli::cli_abort("{.val {values}} needs to contain characters with length of 1.")
88+
cli::cli_abort("{.val data} must be a {.cls data.frame} or {.cls data.table}.")
89+
90+
if (!is.character(values) || length(values) != 1) {
91+
cli::cli_abort("{.val values} needs to contain characters with length of 1.")
9992
} else if (!column_exists(values, data)) {
100-
cli::cli_abort("The {.val values}} column does not exist in the provided {.arg data}.")
93+
cli::cli_abort("The {.val values} column does not exist in the provided {.arg data}.")
10194
}
10295

103-
if (!is.character(col_name) && length(col_name) != 1) {
104-
cli::cli_abort("{.val {col_name}} needs to contain characters with length of 1.")
96+
if (!is.character(col_name) || length(col_name) != 1) {
97+
cli::cli_abort("{.val col_name} needs to contain characters with length of 1.")
10598
} else if (!column_exists(col_name, data)) {
106-
cli::cli_abort("The {.val {col_name}} column does not exist in the provided {.arg data}.")
99+
cli::cli_abort("The {.val col_name} column does not exist in the provided {.arg data}.")
107100
}
108101

109102
if (!is.null(group_by)) {
110-
if (!is.character(group_by) && length(group_by) != 1) {
111-
cli::cli_abort("{.val {group_by}} needs to contain characters with length of 1.")
103+
if (!is.character(group_by) || length(group_by) != 1) {
104+
cli::cli_abort("{.val group_by} needs to contain characters with length of 1.")
112105
} else if (!column_exists(group_by, data)) {
113-
cli::cli_abort("The {.val {group_by}} column does not exist in the provided {.arg data}.")
106+
cli::cli_abort("The {.val group_by} column does not exist in the provided {.arg data}.")
114107
}
115108
}
109+
110+
if (!is.character(palette))
111+
cli::cli_abort("{.val palette} needs to contain characters.")
116112

113+
if (!is.character(method)) {
114+
cli::cli_abort("{.val method} needs to be a character {.cls vector}.")
115+
}
116+
117+
if (!is.logical(paired))
118+
cli::cli_abort("{.val paired} needs to be either `TRUE` or `FALSE`.")
119+
117120
if (!c(p.adjust.method %in% stats::p.adjust.methods))
118121
cli::cli_abort("{.val {p.adjust.method}} is not a valid option. \nValid options: {.val {p.adjust.methods}}")
119122

120123
## MAIN
121124
#--------------------------------------------------------------------#
122125

126+
data_tmp <- data.table::copy(data)
123127
result <- list()
124128

125129
if (!is.null(group_by)) {
126-
pvalues_adjusted <- data[, {
130+
pvalues_adjusted <- data_tmp[, {
127131
tmp <- rstatix::pairwise_wilcox_test(
128132
data = .SD,
129133
formula = stats::reformulate(col_name, response = values),
@@ -136,17 +140,17 @@ diversity_plot <- function(
136140
}, by = group_by]
137141

138142
# Creates box_stats for half geom_box
139-
data.table::setnames(data, old = group_by, new = "group_col")
143+
data.table::setnames(data_tmp, old = group_by, new = "group_col")
140144
group_by <- "group_col"
141-
box_stats <- data[, .(
145+
box_stats <- data_tmp[, .(
142146
ymin = base::min(base::get(values)),
143147
ymax = base::max(base::get(values)),
144148
lower = stats::quantile(base::get(values), 0.25),
145149
middle = stats::median(base::get(values)),
146150
upper = stats::quantile(base::get(values), 0.75)
147151
), by = .(group_numeric = as.numeric(as.factor(base::get(col_name))), group_col)]
148152
} else {
149-
pvalues_adjusted <- data[, {
153+
pvalues_adjusted <- data_tmp[, {
150154
tmp <- rstatix::pairwise_wilcox_test(
151155
data = .SD,
152156
formula = stats::reformulate(col_name, response = values),
@@ -158,7 +162,7 @@ diversity_plot <- function(
158162
}]
159163

160164
# Creates box_stats for half geom_box
161-
box_stats <- data[, .(
165+
box_stats <- data_tmp[, .(
162166
ymin = base::min(base::get(values)),
163167
ymax = base::max(base::get(values)),
164168
lower = stats::quantile(base::get(values), 0.25),
@@ -169,7 +173,7 @@ diversity_plot <- function(
169173
pvalues_adjusted.filtered <- pvalues_adjusted[grepl("\\*", pvalues_adjusted$p.adj.signif) ,]
170174

171175
plt <- ggplot2::ggplot(
172-
data = data,
176+
data = data_tmp,
173177
mapping = ggplot2::aes(
174178
x = as.numeric(as.factor(.data[[col_name]])),
175179
y = .data[[values]]
@@ -276,8 +280,8 @@ diversity_plot <- function(
276280
) +
277281
# Restore proper x-axis labels
278282
ggplot2::scale_x_continuous(
279-
breaks = seq_along(unique(data[[col_name]])),
280-
labels = levels(as.factor(data[[col_name]]))
283+
breaks = seq_along(unique(data_tmp[[col_name]])),
284+
labels = levels(as.factor(data_tmp[[col_name]]))
281285
) +
282286
ggplot2::scale_colour_manual(
283287
name = "groups",

R/euclidean.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ euclidean <- function(x, weighted = TRUE, threads = 1) {
5555
if (length(threads) != 1) {
5656
cli::cli_abort("{.val threads} must be a single whole number.")
5757
} else if (!is.wholenumber(threads)) {
58-
cli::cli_abort("{.val {threads}} must be a whole number.")
58+
cli::cli_abort("{.val threads} must be a whole number.")
5959
}
6060

6161
## MAIN

R/hill_taxa.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ hill_taxa <- function (
5858
cli::cli_abort("{.val x} must be non-negative.")
5959

6060
if (length(q) != 1) {
61-
cli::cli_abort("{.val {q}} needs to be a single whole number.")
61+
cli::cli_abort("{.val q} needs to be a single whole number.")
6262
} else if (!is.wholenumber(q)) {
63-
cli::cli_abort("{.val {q}} is not a whole number.")
63+
cli::cli_abort("{.val q} is not a whole number.")
6464
} else if (!c(q %in% c(0, 1, 2))) {
65-
cli::cli_abort("{.val {q}} needs to be a whole number of either {.val 0}, {.val 1} or {.val 2}.")
65+
cli::cli_abort("{.val q} needs to be a whole number of either {.val 0}, {.val 1} or {.val 2}.")
6666
}
6767

6868
if (!is.logical(normalize))

0 commit comments

Comments
 (0)