Skip to content

Commit

Permalink
Merge pull request #44 from best-practice-and-impact/20-plot-unit-tests
Browse files Browse the repository at this point in the history
20 plot unit tests
  • Loading branch information
CHCRowley authored Sep 21, 2023
2 parents 85bce67 + 4158cc9 commit 2b42d6f
Show file tree
Hide file tree
Showing 44 changed files with 1,726 additions and 1,402 deletions.
4 changes: 2 additions & 2 deletions R/derive_vars.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ derive_basic_rap_scores <- function(data) {
.x %in% high_vals ~ 1,
TRUE ~ 0),
.names = "{.col}_score")) %>%
mutate(doc_score = as.integer(.data$doc_comments_score & .data$doc_readme_score)) %>%
select(-c(.data$doc_comments_score, .data$doc_readme_score)) %>%
mutate(doc_score = as.integer(doc_comments_score & doc_readme_score)) %>%
select(-c(doc_comments_score, doc_readme_score)) %>%
rename_with(~ score_col_names[which(paste0(prac_cols, "_score") == .x)],
.cols = paste0(prac_cols,
"_score")) %>%
Expand Down
2 changes: 1 addition & 1 deletion R/frequency-tables.R
Original file line number Diff line number Diff line change
Expand Up @@ -1285,7 +1285,7 @@ calculate_multi_table_freqs <- function(data, col1, col2, levels1, levels2, prop
selected_data[col2] <- factor(selected_data[[col2]], levels = levels2)

frequencies <- selected_data %>%
count(across(c(col1, col2)), .drop=FALSE) %>%
count(across(all_of(c(col1, col2))), .drop=FALSE) %>%
drop_na() %>%
data.frame()

Expand Down
33 changes: 20 additions & 13 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
#'
#' @export

freq_subplots <- function(data, xlab, ylab, height, width, bar_colour, nrows = 3, y_margin = .1, x_margin = .1, font_size = 12, orientation = "h") {
freq_subplots <- function(data, xlab, ylab, height, width, bar_colour, nrows = 3,
y_margin = .1, x_margin = .1, font_size = 12, orientation = "h") {

if (nrows == 1) {
stop("Unexpected input: n_rows should be 2 or greater.")
Expand Down Expand Up @@ -132,7 +133,10 @@ freq_subplots <- function(data, xlab, ylab, height, width, bar_colour, nrows = 3
#'
#' @export

plot_freqs <- function(data, n, colour, break_q_names_col, type = c("bar", "line"), max_lines = 2, xlab = "", ylab = "", font_size = 12, orientation = c("v", "h"), ...) {

plot_freqs <- function(data, n, colour, break_q_names_col, type = c("bar", "line"),
max_lines = 2, xlab = "", ylab = "", font_size = 12,
orientation = c("v", "h"), ...) {

# Set default bar colour
if (missing(colour)) {
Expand Down Expand Up @@ -262,7 +266,7 @@ plot_stacked <- function(data, n, break_q_names_col, type = c("bar", "line"), ma
if (!is.data.frame(data)) {
stop("Unexpected input - data is not a data.frame.")
} else if (ncol(data) != 3) {
stop("Unexpected input - data should have three columns")
stop("Unexpected input - data should have three columns.")
}

# Validate labels
Expand Down Expand Up @@ -448,6 +452,7 @@ plot_grouped <- function(data, n, break_q_names_col, max_lines = 2, xlab = "", y
x_axis <- axes$cat_axis
y_axis <- axes$scale_axis
legend = list(traceorder = 'normal')
hovertext <- paste0(data[[1]], ": ", round(abs(y_vals) * 100, 1), "%", " <extra></extra>")
} else if (orientation == "h") {
data[[1]] <- factor(rev(data[[1]]), levels = rev(unique(data[[1]])))
data[[2]] <- factor(rev(data[[2]]), levels = rev(unique(data[[2]])))
Expand All @@ -458,14 +463,13 @@ plot_grouped <- function(data, n, break_q_names_col, max_lines = 2, xlab = "", y
y_axis <- axes$cat_axis
legend = list(traceorder = 'reversed')
ylab <- xlab
hovertext <- paste0(data[[1]], ": ", round(abs(x_vals) * 100, 1), "%", " <extra></extra>")
}

y_axis$title <- ""

sample <- ifelse(!missing(n), paste0("Sample size = ", n), "")

hovertext <- paste0(data[[1]], ": ", round(abs(x_vals) * 100, 1), "%", " <extra></extra>")

fig <- plotly::plot_ly(
x = x_vals,
y = y_vals,
Expand Down Expand Up @@ -536,20 +540,20 @@ plot_likert <- function(data, mid, n, break_q_names_col, max_lines = 2, xlab = "
n_questions <- length(unique(data[[1]]))
n_answers <- length(unique(data[[2]]))

# Validate neutral mid
if (!is.logical(neutral_mid)) {
stop("Unexpected input - neutral_mid is not logical.")
}

# Validate mid
if (!is.numeric(mid)) {
stop("Unexpected input - mid is not numeric.")
} else if (mid < 2) {
stop("Unexpected inout - mid is smaller than 2.")
} else if (neutral_mid & mid > n_answers) {
stop("Unexpected input - mid is smaller than 2.")
} else if (neutral_mid & mid >= n_answers) {
stop("Unexpected input - mid >= the number of answers.")
}

# Validate neutral mid
if (!is.logical(neutral_mid)) {
stop("Unexpected input - mid is not logical (TRUE/FALSE)")
}

# Apply break_q_names to a column
if(!missing(break_q_names_col)) {
data[[break_q_names_col]] <- break_q_names(data[[break_q_names_col]], max_lines)
Expand Down Expand Up @@ -682,7 +686,10 @@ calculate_bases <- function(data, mid, neutral_mid) {
}
}

negative_bases <- data %>% dplyr::group_by_at(1) %>% dplyr::mutate_at(3, get_neg_bases, mid = mid, neutral_mid = neutral_mid) %>% data.frame
negative_bases <- data %>%
dplyr::group_by_at(1) %>%
dplyr::mutate_at(3, get_neg_bases, mid = mid, neutral_mid = neutral_mid) %>%
data.frame()

bases <- bases - negative_bases[[3]]

Expand Down
Loading

0 comments on commit 2b42d6f

Please sign in to comment.