diff --git a/DESCRIPTION b/DESCRIPTION index a3fce61..89f5014 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -30,7 +30,8 @@ Imports: graphics, grDevices, khroma (>= 1.14.0), - methods + methods, + utils Suggests: knitr, markdown, @@ -58,6 +59,7 @@ Collate: 'dimensio-deprecated.R' 'dimensio-internal.R' 'dimensio-package.R' + 'export.R' 'get_contributions.R' 'get_coordinates.R' 'get_correlations.R' diff --git a/NAMESPACE b/NAMESPACE index c452ef7..e10f492 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -43,6 +43,7 @@ exportMethods(cdt) exportMethods(colnames) exportMethods(dim) exportMethods(dimnames) +exportMethods(export) exportMethods(get_contributions) exportMethods(get_coordinates) exportMethods(get_correlations) diff --git a/NEWS.md b/NEWS.md index 5329744..78a009a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,6 @@ # dimensio 0.9.0.9000 +## New classes and methods +* Add `export()` to create a Zip archive of all results in CSV format. # dimensio 0.9.0 ## New classes and methods diff --git a/R/AllGenerics.R b/R/AllGenerics.R index 3f8c030..c8faffe 100644 --- a/R/AllGenerics.R +++ b/R/AllGenerics.R @@ -290,6 +290,24 @@ NULL NULL # Results ====================================================================== +#' Export Results +#' +#' Creates a Zip archive of all results in CSV format. +#' @param object A [`CA-class`], [`MCA-class`] or [`PCA-class`] object. +#' @param file A [`character`] string specifying the pathname of the zip file. +#' @param flags A [`character`] string of flags (see [utils::zip()]). +#' @param ... Currently not used. +#' @example inst/examples/ex-export.R +#' @seealso [utils::write.csv()], [utils::zip()] +#' @author N. Frerebeau +#' @docType methods +#' @family getters +#' @aliases export-method +setGeneric( + name = "export", + def = function(object, ...) standardGeneric("export") +) + ### Data ----------------------------------------------------------------------- #' Get Original Data #' diff --git a/R/export.R b/R/export.R new file mode 100644 index 0000000..cda971e --- /dev/null +++ b/R/export.R @@ -0,0 +1,77 @@ +# EXPORT +#' @include AllGenerics.R +NULL + +# The -r9X flags specify that the zip command should recursively search +# sub-directories, use maximum compression, and remove depreciated file fields. +# The -j flag allows the file names to be stored rather than the full file path. +#' @export +#' @rdname export +#' @aliases export,MultivariateAnalysis-method +setMethod( + f = "export", + signature = c(object = "MultivariateAnalysis"), + definition = function(object, file, flags = "-r9Xj", ...) { + ## Create temporary directory + dir_path <- tempfile(pattern = "export_") + dir.create(path = dir_path) + on.exit(unlink(x = dir_path)) + + ## Write results + utils::write.csv( + x = get_data(object), + file = make_file_name(dir_path, "data") + ) + utils::write.csv( + x = get_eigenvalues(object), + file = make_file_name(dir_path, "eigenvalues") + ) + export_results(object, path = dir_path, margin = 1) + export_results(object, path = dir_path, margin = 2) + + ## Zip + status <- utils::zip(zipfile = file, files = dir_path, flags = flags, ...) + invisible(status) + } +) + +export_results <- function(object, path, margin, sup_name = ".sup") { + ## Coordinates + coords <- get_coordinates( + x = object, + margin = margin, + principal = TRUE, + sup_name = sup_name + ) + + ## Contributions + contrib <- get_contributions( + x = object, + margin = margin + ) + + ## cos2 + cos2 <- get_cos2( + x = object, + margin = margin, + sup_name = sup_name + ) + + ## Write + utils::write.csv(x = coords, file = make_file_name(path, "coordinates", margin)) + utils::write.csv(x = contrib, file = make_file_name(path, "contributions", margin)) + utils::write.csv(x = cos2, file = make_file_name(path, "cos2", margin)) + + invisible(NULL) +} + +make_file_name <- function(path, name, margin = NULL) { + prefix <- "" + if (!is.null(margin) && margin == 1) prefix <- "row_" + if (!is.null(margin) && margin == 2) prefix <- "col_" + + file_name <- paste0(prefix, name, ".csv") + file_path <- file.path(path, file_name) + + file_path +} diff --git a/inst/examples/ex-export.R b/inst/examples/ex-export.R new file mode 100644 index 0000000..cd67ed5 --- /dev/null +++ b/inst/examples/ex-export.R @@ -0,0 +1,10 @@ +\dontrun{ +## Load data +data("iris") + +## Compute principal components analysis +X <- pca(iris) + +## Export results +export(X, file = "results.zip") +} diff --git a/man/export.Rd b/man/export.Rd new file mode 100644 index 0000000..40f2298 --- /dev/null +++ b/man/export.Rd @@ -0,0 +1,50 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/AllGenerics.R, R/export.R +\docType{methods} +\name{export} +\alias{export} +\alias{export-method} +\alias{export,MultivariateAnalysis-method} +\title{Export Results} +\usage{ +export(object, ...) + +\S4method{export}{MultivariateAnalysis}(object, file, flags = "-r9Xj", ...) +} +\arguments{ +\item{object}{A \code{\linkS4class{CA}}, \code{\linkS4class{MCA}} or \code{\linkS4class{PCA}} object.} + +\item{...}{Currently not used.} + +\item{file}{A \code{\link{character}} string specifying the pathname of the zip file.} + +\item{flags}{A \code{\link{character}} string of flags (see \code{\link[utils:zip]{utils::zip()}}).} +} +\description{ +Creates a Zip archive of all results in CSV format. +} +\examples{ +\dontrun{ +## Load data +data("iris") + +## Compute principal components analysis +X <- pca(iris) + +## Export results +export(X, file = "results.zip") +} +} +\seealso{ +\code{\link[utils:write.table]{utils::write.csv()}}, \code{\link[utils:zip]{utils::zip()}} + +Other getters: +\code{\link{get_contributions}()}, +\code{\link{get_coordinates}()}, +\code{\link{get_data}()}, +\code{\link{get_eigenvalues}()} +} +\author{ +N. Frerebeau +} +\concept{getters} diff --git a/man/get_contributions.Rd b/man/get_contributions.Rd index dde8eae..69cd456 100644 --- a/man/get_contributions.Rd +++ b/man/get_contributions.Rd @@ -58,6 +58,7 @@ Get Contributions } \seealso{ Other getters: +\code{\link{export}()}, \code{\link{get_coordinates}()}, \code{\link{get_data}()}, \code{\link{get_eigenvalues}()} diff --git a/man/get_coordinates.Rd b/man/get_coordinates.Rd index af9418e..9627e5d 100644 --- a/man/get_coordinates.Rd +++ b/man/get_coordinates.Rd @@ -73,6 +73,7 @@ head(augment(X, margin = 2, axes = c(1, 2))) } \seealso{ Other getters: +\code{\link{export}()}, \code{\link{get_contributions}()}, \code{\link{get_data}()}, \code{\link{get_eigenvalues}()} diff --git a/man/get_data.Rd b/man/get_data.Rd index 1abb93c..ce5d623 100644 --- a/man/get_data.Rd +++ b/man/get_data.Rd @@ -25,6 +25,7 @@ Get Original Data } \seealso{ Other getters: +\code{\link{export}()}, \code{\link{get_contributions}()}, \code{\link{get_coordinates}()}, \code{\link{get_eigenvalues}()} diff --git a/man/get_eigenvalues.Rd b/man/get_eigenvalues.Rd index 46b12fa..11951f4 100644 --- a/man/get_eigenvalues.Rd +++ b/man/get_eigenvalues.Rd @@ -67,6 +67,7 @@ Get Eigenvalues } \seealso{ Other getters: +\code{\link{export}()}, \code{\link{get_contributions}()}, \code{\link{get_coordinates}()}, \code{\link{get_data}()}