diff --git a/R/utils.R b/R/utils.R index 6554198..50210d2 100644 --- a/R/utils.R +++ b/R/utils.R @@ -18,13 +18,13 @@ #' #' @examples #' -#' array_task = 5 # suppose this is the fifth task in an array job -#' index = 2 # will refer to the 'feature' element of 'loops' below -#' loops = list( +#' array_task <- 5 # suppose this is the fifth task in an array job +#' index <- 2 # will refer to the 'feature' element of 'loops' below +#' loops <- list( #' region = c("DLPFC", "HIPPO"), feature = c("gene", "exon", "tx", "jxn") #' ) -#' indexing = get_list_indexing(loops, index) -#' this_feature = this_list[[index]][[ +#' indexing <- get_list_indexing(loops, index) +#' this_feature <- this_list[[index]][[ #' array_task %/% indexing$divisor %% indexing$modulus #' ]] #' sprintf( @@ -63,13 +63,13 @@ get_list_indexing <- function(this_list, index) { #' @examples #' #' # Simple example where initials are as expected: 'a', 'b', 'c' -#' fruits = c('apple', 'banana', 'cherry') -#' initials = get_short_flags(fruits) +#' fruits <- c("apple", "banana", "cherry") +#' initials <- get_short_flags(fruits) #' print(fruits) -#' +#' #' # Example with duplicates with some arbitrary initials: 'c', 'a', 'b', 'd' -#' fruits = c('coconut', 'cherry', 'banana', 'berry') -#' initials = get_short_flags(fruits) +#' fruits <- c("coconut", "cherry", "banana", "berry") +#' initials <- get_short_flags(fruits) #' print(fruits) #' get_short_flags <- function(vec) { @@ -103,7 +103,7 @@ get_short_flags <- function(vec) { #' #' @examples #' -#' fruits = c('apple', 'banana', 'cherry') +#' fruits <- c("apple", "banana", "cherry") #' message(vector_as_code(fruits)) #' vector_as_code <- function(vec) { diff --git a/man/get_list_indexing.Rd b/man/get_list_indexing.Rd new file mode 100644 index 0000000..c4853a9 --- /dev/null +++ b/man/get_list_indexing.Rd @@ -0,0 +1,47 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{get_list_indexing} +\alias{get_list_indexing} +\title{Subset a specific list index given a particular array task index} +\usage{ +get_list_indexing(this_list, index) +} +\arguments{ +\item{this_list}{A \code{`list`} of character vectors} + +\item{index}{\code{integer(1)} specifying the index of \code{this_list} to be +subsetted downstream of this function} +} +\value{ +A length-2 named list of integer(1) vectors containing a divisor and +modulus +} +\description{ +Given \code{this_list} and its \code{index}, determine a "divisor" and +"modulus" instructing how to subset the character vector at that index +given a particular array task index. Downstream, selecting +\code{this_list[[index]][[x]]} for each index yields a combination of values +that is unique across array indices. Here \code{x = array_index / divisor \% modulus}. +This is a helper function for \code{job_loop}. +} +\examples{ + +array_task <- 5 # suppose this is the fifth task in an array job +index <- 2 # will refer to the 'feature' element of 'loops' below +loops <- list( + region = c("DLPFC", "HIPPO"), feature = c("gene", "exon", "tx", "jxn") +) +indexing <- get_list_indexing(loops, index) +this_feature <- this_list[[index]][[ + array_task \%/\% indexing$divisor \%\% indexing$modulus +]] +sprintf( + 'The \%ith array task will have "\%s" as its feature.', + array_task, + this_feature +) + +} +\author{ +Nicholas J. Eagles +} diff --git a/man/get_short_flags.Rd b/man/get_short_flags.Rd new file mode 100644 index 0000000..ea3adef --- /dev/null +++ b/man/get_short_flags.Rd @@ -0,0 +1,37 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{get_short_flags} +\alias{get_short_flags} +\title{Find initials for a character vector} +\usage{ +get_short_flags(vec) +} +\arguments{ +\item{vec}{A character vector, assumed to start with alphabetical characters} +} +\value{ +A character vector with the same length as \code{vec} giving unique +one-letter initials +} +\description{ +Given a character vector 'vec', return an equal-length character vector of +unique one-letter initials. The first letter of each string will be used, +except when this results in duplicates; unique letters will be arbitrarily +assigned in alphabetical order in the case of duplicates. +} +\examples{ + +# Simple example where initials are as expected: 'a', 'b', 'c' +fruits <- c("apple", "banana", "cherry") +initials <- get_short_flags(fruits) +print(fruits) + +# Example with duplicates with some arbitrary initials: 'c', 'a', 'b', 'd' +fruits <- c("coconut", "cherry", "banana", "berry") +initials <- get_short_flags(fruits) +print(fruits) + +} +\author{ +Nicholas J. Eagles +} diff --git a/man/vector_as_code.Rd b/man/vector_as_code.Rd new file mode 100644 index 0000000..ed4fd78 --- /dev/null +++ b/man/vector_as_code.Rd @@ -0,0 +1,28 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{vector_as_code} +\alias{vector_as_code} +\title{Get the segment of code used to construct a given character vector} +\usage{ +vector_as_code(vec) +} +\arguments{ +\item{vec}{A character vector} +} +\value{ +A \code{character(1)} representing the segment of code constructing +\code{vec} +} +\description{ +Given a character vector 'vec', return a character(1) representing the line +of code used to generate 'vec' +} +\examples{ + +fruits <- c("apple", "banana", "cherry") +message(vector_as_code(fruits)) + +} +\author{ +Nicholas J. Eagles +} diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index ca0908d..fe358ee 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -87,8 +87,8 @@ test_that( test_that( "get_short_flags", { - fruits = c('coconut', 'cherry', 'banana', 'berry') - expect_equal(get_short_flags(fruits), c('c', 'a', 'b', 'd')) + fruits <- c("coconut", "cherry", "banana", "berry") + expect_equal(get_short_flags(fruits), c("c", "a", "b", "d")) # Should fail for length > 26 expect_error( @@ -101,8 +101,8 @@ test_that( test_that( "vector_as_code", { - fruits = c('coconut', 'cherry', 'banana', 'berry') - initials = vector_as_code(fruits) + fruits <- c("coconut", "cherry", "banana", "berry") + initials <- vector_as_code(fruits) expect_equal( vector_as_code(fruits), 'c("coconut", "cherry", "banana", "berry")'