R/subset_survey_file.R

Defines functions subset_survey_file

#' Subset a survey from a file
#'
#' Read a survey file and retain the requested variables.
#'
#' @inheritParams subset_surveys
#' @param file_path Path to a single survey file.
#' @param id Optional survey identifier.
#'
#' @return A subsetted survey object or an exported filename.
#'
#' @keywords internal
#' @noRd
subset_survey_file <- function(file_path,
                               subset_vars,
                               subset_name = "subset",
                               id = NULL,
                               export_path = NULL,
                               rowid = "rowid") {
  file_ext <- tolower(fs::path_ext(file_path))

  if (is.null(id)) {
    id <- fs::path_ext_remove(basename(file_path))
  }

  this_survey <- switch(file_ext,
    sav = read_spss(file_path, id = id),
    por = read_spss(file_path, id = id),
    rds = read_rds(file = file_path, id = id),
    dta = read_dta(file = file_path, id = id),
    csv = read_csv(file = file_path, id = id),
    stop(
      paste0("Unsupported survey file format: ", file_ext),
      call. = FALSE
    )
  )

  subset_survey_memory(
    this_survey = this_survey,
    subset_vars = subset_vars,
    subset_name = subset_name,
    export_path = export_path,
    rowid = rowid
  )
}

Try the retroharmonize package in your browser

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

retroharmonize documentation built on Nov. 29, 2026, 5:06 p.m.