R/validate_labelled_conversion.R

Defines functions validate_labelled_conversion

#' Evaluate semantic stabilization readiness
#'
#' @description
#' Evaluate whether a semantically enriched observational vector
#' can be coerced into stricter semantic representations.
#'
#' The function currently evaluates whether a vector with
#' provisional semantic overlays can be represented as a
#' [labelled::labelled()] vector.
#'
#' @details
#' Semantic stabilization is interpreted progressively:
#'
#' - observational vectors preserve raw observed values;
#' - `prelabelled` vectors attach provisional semantic overlays;
#' - stricter semantic representations may require additional
#'   semantic normalization or stabilization.
#'
#' The function therefore does not evaluate semantic correctness,
#' but rather structural readiness for coercion into stricter
#' semantic representations.
#'
#' @param x A semantically enriched vector, typically a
#'   `prelabelled` vector.
#'
#' @return
#' A named list containing:
#'
#' \describe{
#'   \item{valid}{Logical. `TRUE` if coercion succeeded.}
#'   \item{message}{Diagnostic warning or error message, if any.}
#' }
#'
#' @importFrom labelled labelled
#'
#' @keywords internal
#' @noRd

validate_labelled_conversion <- function(x) {
  out <- tryCatch(
    {
      labelled::labelled(
        x = unclass(x),
        labels = attr(x, "labels")
      )

      list(
        valid = TRUE,
        message = NULL
      )
    },
    warning = function(w) {
      list(
        valid = FALSE,
        message = conditionMessage(w)
      )
    },
    error = function(e) {
      list(
        valid = FALSE,
        message = conditionMessage(e)
      )
    }
  )

  out
}

Try the fscontext package in your browser

Any scripts or data that you put into this service are public.

fscontext documentation built on Nov. 19, 2026, 5:11 p.m.