Skip to content

Commit

Permalink
Extract and export setInspectionId()
Browse files Browse the repository at this point in the history
  • Loading branch information
hsonne committed Nov 18, 2024
1 parent 585b30b commit 3e2dfe0
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 18 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export(plotObservations)
export(readAndMergeEuCodedFiles)
export(readEuCodedFile)
export(readEuCodedFiles)
export(setInspectionId)
export(writeEuCodedFile)
export(writeEuCodedFiles)
importFrom(kwb.utils,allAreEqual)
Expand All @@ -18,6 +19,7 @@ importFrom(kwb.utils,collapsed)
importFrom(kwb.utils,hsSafeName)
importFrom(kwb.utils,isTryError)
importFrom(kwb.utils,makeUnique)
importFrom(kwb.utils,moveColumnsToFront)
importFrom(kwb.utils,moveToFront)
importFrom(kwb.utils,orderBy)
importFrom(kwb.utils,printIf)
Expand Down
66 changes: 48 additions & 18 deletions R/setGlobalInspectionID.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,17 @@ setGlobalInspectionID <- function(
inspections <- checkOrAddInspectionTime(
data = inspections, column = columns[3L], hhmm = default.time, seed = 123L
)

# Create the inspection IDs and store them in column "inspection_id"
hashes <- createHashFromColumns(inspections, columns, silent = TRUE)

# Check for duplicates in the hashes
stop_on_hash_duplicates(hashes, error.file = error.file)

inspections[["inspection_id"]] <- hashes

i <- kwb.utils::selectColumns(observations, "inspno")

observations[["inspection_id"]] <- kwb.utils::selectColumns(
inspections, "inspection_id"
)[i]

observations <- kwb.utils::removeColumns(observations, "inspno")
# Create inspection_ids and check if they have duplicated values
inspection_ids <- createHashFromColumns(inspections, columns, silent = TRUE)
stop_on_hash_duplicates(inspection_ids, error.file = error.file)

id_first <- function(x) kwb.utils::moveColumnsToFront(x, "inspection_id")
updated <- setInspectionId(inspections, observations, inspection_ids)

list(
header.info = get_elements(inspection.data, "header.info"),
inspections = id_first(inspections),
observations = id_first(observations)
inspections = updated$inspections,
observations = updated$observations
)
}

Expand Down Expand Up @@ -136,3 +124,45 @@ stop_on_hash_duplicates <- function(hashes, error.file = NULL)
)
}
}

# setInspectionId --------------------------------------------------------------

#' Add column inspection_id to table of inspections and observations
#'
#' @param inspections data frame where each row represents an inspection
#' @param observations data frame where each row represents an observation. The
#' data frame must have a column "inspno" that refers to a row in the data
#' frame \code{inspections}.
#' @param inspection_ids vector of as many inspection ids as there are rows in
#' \code{inspections}
#' @return list with elements \code{inspections} and \code{observations} each of
#' which has a new column "inspection_id" as its first column.
#' @importFrom kwb.utils moveColumnsToFront removeColumns selectColumns
#' @export
#' @examples
#' inspections <- data.frame(pipe_id = 1:3)
#' observations <- data.frame(
#' inspno = c(1, 1, 2, 2, 3, 3),
#' observation = c("start", "end", "start", "end", "start", "end")
#' )
#' setInspectionId(inspections, observations, paste0("id_", 1:3))
#'
setInspectionId <- function(inspections, observations, inspection_ids)
{
stopifnot(length(inspection_ids) == nrow(inspections))
stopifnot(!anyDuplicated(inspection_ids))

INSPID <- "inspection_id"
INSPNO <- "inspno"

inspection_numbers <- kwb.utils::selectColumns(observations, INSPNO)
inspections[[INSPID]] <- inspection_ids
observations[[INSPID]] <- inspection_ids[inspection_numbers]

id_first <- function(x) kwb.utils::moveColumnsToFront(x, INSPID)

list(
inspections = id_first(inspections),
observations = id_first(kwb.utils::removeColumns(observations, INSPNO))
)
}
34 changes: 34 additions & 0 deletions man/setInspectionId.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3e2dfe0

Please sign in to comment.