Skip to content

Commit

Permalink
fix: merge user input data in optim multi loc p-rep design
Browse files Browse the repository at this point in the history
  • Loading branch information
DidierMurilloF committed Apr 11, 2024
1 parent 76dcba7 commit de388b0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 31 deletions.
17 changes: 9 additions & 8 deletions R/fct_do_optim.R
Original file line number Diff line number Diff line change
Expand Up @@ -680,18 +680,19 @@ merge_user_data <- function(
) |>
dplyr::select(USER_ENTRY, ENTRY, NAME) |>
dplyr::left_join(y = iter_loc, by = "ENTRY")

if (inherits(optim_out, "MultiPrep")) {
data_input_mutated <- data_input_mutated |>
dplyr::select(.data = ., USER_ENTRY, NAME.x, REPS) |>
dplyr::arrange(dplyr::desc(REPS)) |>
dplyr::rename(ENTRY = USER_ENTRY, NAME = NAME.x)
dplyr::select(USER_ENTRY, NAME.x, REPS) |> # Just specify columns directly
dplyr::arrange(dplyr::desc(REPS)) |> # Arrange rows
dplyr::rename(ENTRY = USER_ENTRY, NAME = NAME.x) # Rename columns
} else if (inherits(optim_out, "Sparse")) {
data_input_mutated <- data_input_mutated |>
dplyr::filter(.data = ., !is.na(NAME.y)) |>
dplyr::select(USER_ENTRY, NAME.x) |>
dplyr::rename(ENTRY = USER_ENTRY, NAME = NAME.x)
}
dplyr::filter(!is.na(NAME.y)) |> # Filter rows
dplyr::select(USER_ENTRY, NAME.x) |> # Select columns
dplyr::rename(ENTRY = USER_ENTRY, NAME = NAME.x) # Rename columns
}

# Store the number of plots (It does not include checks)
df_to_check <- data_input_mutated[(input_checks + 1):nrow(data_input_mutated), ]
if (inherits(optim_out, "MultiPrep")) {
Expand Down
34 changes: 11 additions & 23 deletions R/utils_diagonals_checks.R
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,22 @@ total_elements <- function(alist) {
length(unlist(alist))
}

#' @title Split Matrix Into Blocks
#' @title Split matrix Into sub matrices
#'
#' @description
#' Splits a matrix into a list of blocks, either by rows or by columns, based on the specified sizes of the blocks.
#'
#' @param Matrix A matrix to be split.
#' @param matrix_object A matrix to be split.
#' @param blocks Either a list or a vector indicating the sizes of the blocks to be split into.
#' If \code{blocks} is a list of vectors, each vector's length defines the size of the blocks.
#' If \code{blocks} is a vector, each element represents the size of a block.
#' @param byrow A logical value. If \code{TRUE} (the default), the matrix is split
#' by rows; otherwise, it is split by columns.
#' @return A list of matrices, each representing a block.
#' @noRd
split_matrix_into_blocks <- function(Matrix, blocks, byrow = TRUE) {
split_matrix_into_blocks <- function(matrix_object, blocks, byrow = TRUE) {

if (!is.matrix(Matrix)) {
if (!is.matrix(matrix_object)) {
stop("Input must be a matrix.")
}

Expand All @@ -156,32 +156,20 @@ split_matrix_into_blocks <- function(Matrix, blocks, byrow = TRUE) {
blocks_list = vector(mode="list", length=num_blocks)

# Validate the total size against the matrix dimension before the loop
if (byrow && size != nrow(Matrix)) {
stop("Number of rows in 'Matrix' does not match 'blocks'")
} else if (!byrow && size != ncol(Matrix)) {
stop("Number of columns in 'Matrix' does not match 'blocks'")
if (byrow && size != nrow(matrix_object)) {
stop("Number of rows in 'matrix_object' does not match 'blocks'")
} else if (!byrow && size != ncol(matrix_object)) {
stop("Number of columns in 'matrix_object' does not match 'blocks'")
}

# Use a loop to populate the blocks_list based on the 'byrow' flag
for (k in 1:num_blocks) {
if (byrow) {
blocks_list[[k]] = Matrix[from[k]:to[k], , drop = FALSE] # Ensuring the result is always a matrix
blocks_list[[k]] = matrix_object[from[k]:to[k], , drop = FALSE] # Ensuring the result is always a matrix
} else {
blocks_list[[k]] = Matrix[, from[k]:to[k], drop = FALSE] # Ensuring the result is always a matrix
blocks_list[[k]] = matrix_object[, from[k]:to[k], drop = FALSE] # Ensuring the result is always a matrix
}
}

return(blocks_list)
}

# for (k in 1:num_blocks) {
# if (byrow) {
# if (size != nrow(Matrix))
# stop("\nNumber of rows in 'Matrix' doesn't match 'blocks'")
# blocks_list[[k]] = Matrix[from[k]:to[k],]
# } else {
# if (size != ncol(Matrix))
# stop("\nNumber of columns in 'Matrix' doesn't match 'blocks'")
# blocks_list[[k]] = Matrix[,from[k]:to[k]]
# }
# }
}

0 comments on commit de388b0

Please sign in to comment.