Learn R Programming

epmfd (version 1.1.1)

export_epmfd: Export epmfd objects to disk

Description

export_epmfd() writes commonly used tables from epmfd_* objects to CSV / Excel / SPSS files, and (optionally) saves the object itself as an RDS.

Usage

export_epmfd(
  object,
  dir = NULL,
  prefix = NULL,
  format = c("csv", "xlsx", "sav"),
  save_rds = FALSE,
  include_misfit = FALSE
)

Value

If dir is NULL, a named list containing the tables that would be written (e.g., clean, misfit, scale). If dir is non-NULL, (invisibly) a character vector of file paths that were written.

Arguments

object

One of: epmfd_scaled, epmfd_misfit, epmfd_clean.

dir

Target directory. If NULL (default), no files are written; instead, the function returns the tables as a named list. If provided, the directory must exist or will be created.

prefix

File name prefix (without extension). If NULL, the first class name of object is used (e.g., "epmfd_clean").

format

Output format; one of "csv" (default), "xlsx", or "sav".

save_rds

Logical; if TRUE and dir is provided, also saves the object as <prefix>.rds in dir via saveRDS().

include_misfit

Logical; if TRUE, writes/returns misfit tables when available (see Details). Default = FALSE.

File naming (when <code>dir</code> is provided)

Files are named <prefix>_<name>.<format> under dir. For example: study1_clean.csv, study1_misfit.xlsx, or study1_scale.sav.

Details

What is produced depends on the object class:

  • epmfd_clean: cleaned person-by-item data (clean); if include_misfit = TRUE and a misfit object is attached, also misfit.

  • epmfd_misfit: if include_misfit = TRUE, misfit.

  • epmfd_scaled: item status summary (scale).

When format = "sav", logical columns are converted to labelled factors (FALSE/TRUE) for SPSS compatibility. Writing .sav does not support list columns; the function aborts if such columns are present.

See Also

saveRDS(), readr, openxlsx, haven

Examples

Run this code
if (FALSE) {
# \donttest{
  # Minimal toy objects created inside the example ----
  set.seed(1)
  toy_clean <- data.frame(
    I1 = sample(0:1, 6, TRUE),
    I2 = sample(0:1, 6, TRUE)
  )
  toy_misfit <- data.frame(
    person = 1:6, Gpn = runif(6), U3p = runif(6)
  )

  clean_obj <- structure(
    list(clean_data = toy_clean,
         misfit     = list(table = toy_misfit)),
    class = "epmfd_clean"
  )

  misfit_obj <- structure(
    list(table = toy_misfit, method = "mokken"),
    class = "epmfd_misfit"
  )

  scaled_obj <- structure(
    list(kept = c("I1", "I2"), removed = character()),
    class = "epmfd_scaled"
  )

  # 1) No writing: return list
  lst <- export_epmfd(clean_obj, dir = NULL, include_misfit = TRUE)
  str(lst)

  # 2) Write to a temporary directory (CRAN policy)
  tmpdir <- tempdir()
  export_epmfd(clean_obj,  dir = tmpdir, prefix = "study1", format = "csv",
               save_rds = TRUE)

  # Optional formats guarded by Suggests (run only if installed)
  if (requireNamespace("haven", quietly = TRUE)) {
    export_epmfd(misfit_obj, dir = tmpdir, format = "sav",
                 include_misfit = TRUE)
  }
  if (requireNamespace("openxlsx", quietly = TRUE)) {
    export_epmfd(scaled_obj, dir = tmpdir, prefix = "scaleA",
                 format = "xlsx")
  }
# }
}

Run the code above in your browser using DataLab