From 5b66d1958bf589eec8415fb8a49e6b236d60ed55 Mon Sep 17 00:00:00 2001 From: hanneoberman Date: Fri, 19 Jul 2024 17:01:57 +0200 Subject: [PATCH] code style edits (see review) --- NAMESPACE | 2 -- R/plot_trace.R | 24 +++++++++--------------- tests/testthat/test-plot_trace.R | 2 +- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index d3b3a8ae..5a976a40 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -13,5 +13,3 @@ export(stripplot) export(xyplot) importFrom(magrittr,"%>%") importFrom(rlang,.data) -importFrom(rlang,enexpr) -importFrom(utils,tail) diff --git a/R/plot_trace.R b/R/plot_trace.R index 2a24321c..402eacaa 100644 --- a/R/plot_trace.R +++ b/R/plot_trace.R @@ -35,8 +35,6 @@ #' for(v in vars) #' plot_trace(imp, !!v) |> print() #' -#' @importFrom utils tail -#' @importFrom rlang enexpr #' @export plot_trace <- function(data, vrb = "all") { verify_data(data, imp = TRUE) @@ -49,17 +47,18 @@ plot_trace <- function(data, vrb = "all") { sm <- sqrt(data$chainVar) # select variable to plot from list of imputed variables - vrb <- enexpr(vrb) - if(is.call(vrb)) - vrb <- as.character(vrb) |> tail(-1) - else if(is.symbol(vrb)) + vrb <- rlang::enexpr(vrb) + if (is.call(vrb)) + vrb <- as.character(vrb) |> utils::tail(-1) + if (is.symbol(vrb)) vrb <- as.character(vrb) varlist <- names(data$imp)[apply(!(is.nan(mn) | is.na(mn)), 1, all)] if (length(vrb) == 1 && as.character(vrb) == "all") { vrb <- varlist - } else if (any(vrb %nin% colnames(data$data))) { + } + if (any(vrb %nin% colnames(data$data))) { cli::cli_abort( c( "x" = "The following variables are not present in 'data':", @@ -81,8 +80,7 @@ plot_trace <- function(data, vrb = "all") { if (any(vrb %in% varlist)) { vrb <- vrb[which(vrb %in% varlist)] } else { - cli::cli_abort(c("x" = "None of the variables are imputed.", - "No plots can be produced.")) + cli::cli_abort(c("x" = "None of the variables are imputed.", "No plots can be produced.")) } } @@ -95,8 +93,7 @@ plot_trace <- function(data, vrb = "all") { vrb = rep(vrb, each = m * it, times = 2), val = c(matrix(aperm(mn[vrb, , , drop = FALSE], c( 2, 3, 1 - )), nrow = m * it * p), - matrix(aperm(sm[vrb, , , drop = FALSE], c( + )), nrow = m * it * p), matrix(aperm(sm[vrb, , , drop = FALSE], c( 2, 3, 1 )), nrow = m * it * p)) )) @@ -121,9 +118,7 @@ plot_trace <- function(data, vrb = "all") { list(do.call(paste, c(labels, list(sep = "\n")))) } ) + - ggplot2::labs(x = "Iteration", - y = "Imputation parameter", - color = "Imputation number") + + ggplot2::labs(x = "Iteration", y = "Imputation parameter", color = "Imputation number") + theme_mice() + ggplot2::theme( strip.background = ggplot2::element_blank(), @@ -131,4 +126,3 @@ plot_trace <- function(data, vrb = "all") { strip.switch.pad.wrap = ggplot2::unit(0, "cm") ) } - diff --git a/tests/testthat/test-plot_trace.R b/tests/testthat/test-plot_trace.R index f047008e..e294ebcb 100644 --- a/tests/testthat/test-plot_trace.R +++ b/tests/testthat/test-plot_trace.R @@ -1,7 +1,7 @@ # create test objects dat <- mice::nhanes imp <- mice::mice(dat, printFlag = FALSE) -v <- c("bmi", "hyp") +v <- c("bmi", "hyp") # tests test_that("plot_trace creates ggplot object", {