Skip to content

Commit

Permalink
Some minor edits to use_measurements()
Browse files Browse the repository at this point in the history
* Add "success" message detailing that a new nested column has been created
* Divided a function in an attempt to figure out why it's memory load is exceeded sometimes
  • Loading branch information
daxkellie committed Dec 20, 2024
1 parent 312131c commit e78ed9b
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions R/use_measurements.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#' @importFrom dplyr mutate
#' @importFrom rlang abort
#' @importFrom rlang enquos
#' @importFrom cli cli_progress_step
#' @importFrom dplyr row_number
#' @importFrom purrr map_dfr
#' @export
use_measurements <- function(
.df,
Expand All @@ -34,19 +37,27 @@ use_measurements <- function(
fn_quos <- enquos(cols)

# Creates measurementOrFact column, nests measurement columns
cli::cli_progress_step("Adding measurement columns")
cli_progress_step("Adding measurement columns")

nested_df <- .df |>
df_split <- .df |>
# add row number for id
mutate(padded_row_number = sequential_id()) |>
mutate(
padded_row_number = stringr::str_pad(row_number(),
floor(log10(row_number())) + 1,
pad = '0')
) |>
group_split(dplyr::row_number(), .keep = FALSE)
# NOTE: Must use group_split to preserve grouping by row, not an unexpected grouping (ie force rowwise)
group_split(dplyr::row_number(), .keep = FALSE) |>
purrr::map_dfr( ~ .x |>
nest(measurementOrFact = c(padded_row_number, !!!fn_quos)))


nested_df <- purrr::map_dfr(df_split,
~ .x |>
nest(measurementOrFact = c(padded_row_number, !!!fn_quos))
)

# Pivots each row's data to long
# Adds rowwise `unit` and `type` information to each nested tibble
cli::cli_progress_step("Converting measurements to Darwin Core")
cli_progress_step("Converting measurements to Darwin Core")

result <- nested_df |>
dplyr::mutate(
Expand Down Expand Up @@ -79,6 +90,8 @@ use_measurements <- function(
#
# check_eventDate(result, level = "abort")

cli::cli_progress_step("Successfully nested measurements in column {.field measurementOrFact}.")

return(result)
}

Expand Down

0 comments on commit e78ed9b

Please sign in to comment.