Skip to content

Commit

Permalink
Allow to create a Zip archive of all results in CSV format
Browse files Browse the repository at this point in the history
  • Loading branch information
nfrerebeau committed Oct 21, 2024
1 parent 639d636 commit e515a5a
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 1 deletion.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ Imports:
graphics,
grDevices,
khroma (>= 1.14.0),
methods
methods,
utils
Suggests:
knitr,
markdown,
Expand Down Expand Up @@ -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'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ exportMethods(cdt)
exportMethods(colnames)
exportMethods(dim)
exportMethods(dimnames)
exportMethods(export)
exportMethods(get_contributions)
exportMethods(get_coordinates)
exportMethods(get_correlations)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
18 changes: 18 additions & 0 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
#'
Expand Down
77 changes: 77 additions & 0 deletions R/export.R
Original file line number Diff line number Diff line change
@@ -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(object, path = dir_path, margin = 1)
.export(object, path = dir_path, margin = 2)

## Zip
status <- utils::zip(zipfile = file, files = dir_path, flags = flags, ...)
invisible(status)
}
)

.export <- 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
}
10 changes: 10 additions & 0 deletions inst/examples/ex-export.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
\dontrun{
## Load data
data("iris")

## Compute principal components analysis
X <- pca(iris)

## Export results
export(X, file = "results.zip")
}
50 changes: 50 additions & 0 deletions man/export.Rd

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

1 change: 1 addition & 0 deletions man/get_contributions.Rd

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

1 change: 1 addition & 0 deletions man/get_coordinates.Rd

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

1 change: 1 addition & 0 deletions man/get_data.Rd

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

1 change: 1 addition & 0 deletions man/get_eigenvalues.Rd

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

0 comments on commit e515a5a

Please sign in to comment.