From 49b1620003c3fe2dd98fa86bbf7af32a058e87f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Llu=C3=ADs?= Date: Mon, 20 May 2024 21:12:59 +0200 Subject: [PATCH] Export position_name --- NAMESPACE | 1 + NEWS.md | 4 ++++ R/evaluate_category.R | 3 ++- R/follow_up.R | 2 +- R/utils.R | 14 ++++++++++++++ README.Rmd | 6 ++++-- README.md | 26 +++++++++++++++++++++----- _pkgdown.yml | 1 + cran-comments.md | 2 +- man/check_data.Rd | 5 ++++- man/follow_up.Rd | 2 +- man/position_name.Rd | 23 +++++++++++++++++++++++ tests/testthat/test-spatial.R | 9 +++++++++ 13 files changed, 86 insertions(+), 12 deletions(-) create mode 100644 man/position_name.Rd diff --git a/NAMESPACE b/NAMESPACE index 0e860ac..6050d6e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -22,6 +22,7 @@ export(follow_up2) export(inspect) export(optimum_batches) export(optimum_subset) +export(position_name) export(qcSubset) export(replicates) export(sizes_batches) diff --git a/NEWS.md b/NEWS.md index dd1454f..e465630 100644 --- a/NEWS.md +++ b/NEWS.md @@ -10,8 +10,12 @@ * Now it is possible to remove full rows or columns from `spatial()`: like `remove_positions = "A"` (#52). +* Fix a bug in `spatial()` that in some cases assigned multiple samples to the same position (#51). + * Spatial indexes are returned in row, column order: A1, A2, A3, ... A10, B1,. +* Function `position_name()` is now exported to facilitate generating designs. + # experDesign 0.3.0 * Fixed a bug in `spatial()` where multiple samples could be assigned to the diff --git a/R/evaluate_category.R b/R/evaluate_category.R index e95f79f..17de4e5 100644 --- a/R/evaluate_category.R +++ b/R/evaluate_category.R @@ -79,7 +79,8 @@ evaluate_independence <- function(i, pheno) { #' @param pheno Data.frame with the variables of each sample, one row one sample. #' @param omit Character vector with the names of the columns to omit. #' @param na.omit Check the effects of missing values too. -#' @return A logical value indicating if everything is alright (TRUE) or not (FALSE). +#' @return A logical value indicating if everything is alright (`TRUE` or not (`FALSE`). +#' @seealso [valid_followup()]. #' @export #' @examples #' rdata <- expand.grid(sex = c("M", "F"), class = c("lower", "median", "high")) diff --git a/R/follow_up.R b/R/follow_up.R index bfa64a3..049b28a 100644 --- a/R/follow_up.R +++ b/R/follow_up.R @@ -16,7 +16,7 @@ #' data(survey, package = "MASS") #' survey1 <- survey[1:118, ] #' survey2 <- survey[119:nrow(survey), ] -#' fu <- follow_up(survey1, survey2, size_subset = 50, iterations = 10) +#' folu <- follow_up(survey1, survey2, size_subset = 50, iterations = 10) follow_up <- function(original, follow_up, size_subset, omit = NULL, old_new = "batch", iterations = 500) { stopifnot(is.character(old_new) & length(old_new) == 1) diff --git a/R/utils.R b/R/utils.R index 2841452..973ed3a 100644 --- a/R/utils.R +++ b/R/utils.R @@ -68,7 +68,21 @@ translate_index <- function(index, index } + +#' Create position names for a grid. +#' +#' @param rows Names of the rows. +#' @param columns Names of the columns. +#' +#' @return A data.frame with the rows and columns and the resulting name row+column. +#' The name column is a factor for easier sorting in row, column order. +#' @export +#' @examples +#' position_name(c("A", "B"), 1:2) position_name <- function(rows, columns) { + if (length(intersect(rows, columns)) >= 1) { + stop("rows and columns shouldn't share names.") + } positions <- expand.grid(rows, columns, stringsAsFactors = FALSE) colnames(positions)[1:2] <- c("row", "column") positions <- positions[order(positions$row, positions$column), ] diff --git a/README.Rmd b/README.Rmd index 3bd2da8..e878549 100644 --- a/README.Rmd +++ b/README.Rmd @@ -26,8 +26,8 @@ knitr::opts_chunk$set( -The goal of experDesign is to help you manage your samples before an experiment but after they are collected. -For example, checking for common problems in the data, and help reduce or even prevent batch bias before performing an experiment, or measure once it is already done +The goal of experDesign is to help you distribute your samples before an experiment but after they are collected. +For example, checking for common problems in the data, and reducing or even preventing batch bias before performing an experiment, or measuring it once the experiment is performed. It provides four main functions: * `check_data()`: Check if there are any problems with the data. @@ -35,6 +35,8 @@ It provides four main functions: * `replicates()`: Selects some samples for replicates and randomizes the samples (highly recommended). * `spatial()`: Randomize the samples on a spatial grid. +There are other helpers. + ## Installation To install the latest version on [CRAN](https://CRAN.R-project.org/package=experDesign) use: diff --git a/README.md b/README.md index edfeac2..8a466aa 100644 --- a/README.md +++ b/README.md @@ -22,11 +22,11 @@ developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.re -The goal of experDesign is to help you manage your samples before an +The goal of experDesign is to help you distribute your samples before an experiment but after they are collected. For example, checking for -common problems in the data, and help reduce or even prevent batch bias -before performing an experiment, or measure once it is already done It -provides four main functions: +common problems in the data, and reducing or even preventing batch bias +before performing an experiment, or measuring it once the experiment is +performed. It provides four main functions: - `check_data()`: Check if there are any problems with the data. - `design()`: Randomize the samples according to their variables. @@ -34,6 +34,8 @@ provides four main functions: samples (highly recommended). - `spatial()`: Randomize the samples on a spatial grid. +There are other helpers. + ## Installation To install the latest version on @@ -118,6 +120,9 @@ combination with the original according to multiple statistics. omit <- c("Wr.Hnd", "NW.Hnd", "Fold", "Pulse", "Clap", "Exer", "Height", "M.I") (keep <- colnames(survey)[!colnames(survey) %in% omit]) #> [1] "Sex" "W.Hnd" "Smoke" "Age" +``` + +``` r head(survey[, keep]) #> Sex W.Hnd Smoke Age #> 1 Female Right Never 18.250 @@ -126,12 +131,18 @@ head(survey[, keep]) #> 4 Male Right Never 20.333 #> 5 Male Right Never 23.667 #> 6 Female Right Never 21.000 +``` + +``` r # Set a seed for reproducibility set.seed(87732135) # Looking for groups at most of 70 samples. index <- design(pheno = survey, size_subset = 70, omit = omit, iterations = 100) #> Warning: There might be some problems with the data use check_data(). +``` + +``` r index #> $SubSet1 #> [1] 3 9 10 14 16 21 23 24 25 30 44 46 56 57 59 62 63 68 69 @@ -164,6 +175,9 @@ a colleague with: ``` r head(batch_names(index)) #> [1] "SubSet3" "SubSet3" "SubSet1" "SubSet4" "SubSet2" "SubSet2" +``` + +``` r # Or via inspect() to keep it in a matrix format: head(inspect(index, survey[, keep])) #> Sex W.Hnd Smoke Age batch @@ -203,7 +217,9 @@ Two packages allow to distribute the samples on batches: If you are still designing the experiment and do not have collected any data [DeclareDesign](https://cran.r-project.org/package=DeclareDesign) -might be relevant for you. +might be relevant for you. But specially the +[randomizr](https://cran.r-project.org/package=randomizr) packages which +makes common forms of random assignment and sampling. Question in [Bioinformatics.SE](https://bioinformatics.stackexchange.com/q/4765/48) diff --git a/_pkgdown.yml b/_pkgdown.yml index 85ddc07..fa2dca8 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -53,6 +53,7 @@ reference: - valid_followup - title: "Other" - contents: + - position_name - create_subset - compare_index - entropy diff --git a/cran-comments.md b/cran-comments.md index 222d580..a24d340 100644 --- a/cran-comments.md +++ b/cran-comments.md @@ -1,2 +1,2 @@ -This updates adds some functions and fixes a bug found by a user. +This updates adds some functions and fixes a bug. Checks passed without notes locally in some other machines there was a note about examples above 5s. diff --git a/man/check_data.Rd b/man/check_data.Rd index dfcb64e..81649d4 100644 --- a/man/check_data.Rd +++ b/man/check_data.Rd @@ -14,7 +14,7 @@ check_data(pheno, omit = NULL, na.omit = FALSE) \item{na.omit}{Check the effects of missing values too.} } \value{ -A logical value indicating if everything is alright (TRUE) or not (FALSE). +A logical value indicating if everything is alright (\code{TRUE} or not (\code{FALSE}). } \description{ In order to run a successful experiment a good design is needed even before measuring the data. @@ -32,3 +32,6 @@ data(survey, package = "MASS") check_data(survey) } } +\seealso{ +\code{\link[=valid_followup]{valid_followup()}}. +} diff --git a/man/follow_up.Rd b/man/follow_up.Rd index c18897e..60b6987 100644 --- a/man/follow_up.Rd +++ b/man/follow_up.Rd @@ -40,7 +40,7 @@ with some other samples later on. data(survey, package = "MASS") survey1 <- survey[1:118, ] survey2 <- survey[119:nrow(survey), ] -fu <- follow_up(survey1, survey2, size_subset = 50, iterations = 10) +folu <- follow_up(survey1, survey2, size_subset = 50, iterations = 10) } \seealso{ \code{\link[=follow_up2]{follow_up2()}} diff --git a/man/position_name.Rd b/man/position_name.Rd new file mode 100644 index 0000000..476d9a8 --- /dev/null +++ b/man/position_name.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{position_name} +\alias{position_name} +\title{Create position names for a grid.} +\usage{ +position_name(rows, columns) +} +\arguments{ +\item{rows}{Names of the rows.} + +\item{columns}{Names of the columns.} +} +\value{ +A data.frame with the rows and columns and the resulting name row+column. +The name column is a factor for easier sorting in row, column order. +} +\description{ +Create position names for a grid. +} +\examples{ +position_name(c("A", "B"), 1:2) +} diff --git a/tests/testthat/test-spatial.R b/tests/testthat/test-spatial.R index 52855b7..78a5c64 100644 --- a/tests/testthat/test-spatial.R +++ b/tests/testthat/test-spatial.R @@ -65,4 +65,13 @@ test_that("Removing columns works", { expect_lte(length(index2), 9*12) expect_true(all(names(index2) %in% position_name(rows = LETTERS[1:9], 1:12)$name)) expect_false(any(table(batch_names(index), batch_names(index2)) > 1)) +}) + +test_that("position_name works", { + expect_error(position_name(1:2, 1:2)) + pn <- position_name(LETTERS[1:2], 1:2) + expect_s3_class(pn, "data.frame") + expect_equal(pn$row, rep(LETTERS[1:2], each = 2)) + expect_equal(pn$column, rep(1:2, length.out = 4)) + expect_equal(pn$name, as.factor(paste0(rep(LETTERS[1:2], each = 2), rep(1:2, length.out = 4)))) }) \ No newline at end of file