R/write_kspace.R

#' Write a knowledge space to file
#' 
#' @param x Knowledge space (in set (\code{kspace}) or matrix (\code{kmspace}) format)
#' @param filename Name of the file to be written
#' @param format File format
#' @param sep Cell separator for CSV files
#' @param enforce Should knowledge space properties be enforced (only for matrix form)?
#' 
#' If the file format is not specified, \code{write_kspace()} looks at the filename
#' extension. If it is some spreadsheet type (CSV, ODS, or XLSX), the
#' respective file format is chosen, otherwise SRBT is selected.
#' 
#' For larger knowledge spaces, it might be better suitable and way more 
#' efficient to save and later load them with the \code{save()} and 
#' \code{load()} functions - if they are to be used only with R.
#' 
#' @examples
#' kstMatrix::xpl$space
#' tempfile <- paste0(tempdir(), "/test.spc")
#' write_kspace(kstMatrix::xpl$space, tempfile, format="matrix")
#' cat(readLines(tempfile), sep='\n')
#' # Using a non-space object
#' write_kspace(kstMatrix::xpl$basis, tempfile, format="matrix")
#' cat(readLines(tempfile), sep='\n')
#' 
#' 
#' @family Functions for reading and writing families of sets
#' 
#' @importFrom tools file_ext
#' @importFrom utils write.table 
#' @importFrom readODS write_ods
#' @importFrom openxlsx2 write_xlsx
#' @importFrom kstMatrix kmspace
#' 
#' @export 
write_kspace <- function (x, filename, format=NULL, sep=',', enforce = TRUE) {
  if (inherits(x, "kspace"))
    mat <- as.binaryMatrix(x)
  else if (inherits(x, "kmspace")) {
    enforce <- FALSE
    mat <- x
  }
  else {
    mat <- x
    if (enforce)
      warning(sprintf("Attempt to store '%s' object as 'kmspace'. Space determination is enforced.", class(mat)[1]))
    else
      warning(sprintf("Storing '%s' object as Space.", class(mat)[1]))
    if (!inherits(mat, "matrix"))
      stop("'x' must be a matrix.")
    if (enforce) mat <- kmspace(kmfamset(mat))
  }
  
  ext <- file_ext(filename)
  if (is.null(format)) {
    if (ext == "csv") format <- "CSV"
    else if (ext == "xlsx") format <- "XLSX"
    else if (ext == "ods") format <- "ODS"
    else format <- "SRBT"
  } else {
    if ((ext == "csv") && (format != "CSV"))
      warning(sprintf('Storing file in "%s" format in .csv file!', format))
    else if ((ext == "ods") && (format != "ODS"))
      warning(sprintf('Storing file in "%s" format in .ods file!', format))
    else if ((ext == "xlsx") && (format != "XLSX"))
      warning(sprintf('Storing file in "%s" format in .xlsx file!', format))
    else if (format %in% c("CSV", "ODS", "XLSX") && tolower(format) != ext)
      warning(sprintf("Format specification '%s' and filename extension '%s' do nto fit together!",
                      format, ext))
  }
  
  size <- dim(mat)
  
  if (format == "CSV") {
    if (sep == ',') dec <- '.'
    else dec <- ','
    if (is.null(colnames(mat)))
      write.table(mat, filename, sep=sep, row.names=FALSE, col.names=FALSE)
    else
      write.table(mat, filename, sep=sep, row.names=FALSE, col.names=TRUE)
  } else if (format == "XLSX") {
    if (is.null(colnames(mat)))
      write_xlsx(as.data.frame(mat), file = filename, col.names=FALSE)
    else write_xlsx(as.data.frame(mat), file = filename)
  } else if (format == "ODS") {
    if (is.null(colnames(mat)))
      write_ods(as.data.frame(mat), path = filename, col_names=FALSE)
    else write_ods(as.data.frame(mat), path = filename)
  } else {
    con <- file(filename)
    if (is.null(con))
      stop(sprintf("Unable to open file %s.", dQuote(filename)))
    open(con, open="w")
    
    if (format == "SRBT")
      cat("#SRBT v2.0 space\n", file=con)
    
    if ((format == "SRBT") | (format == "KST")) {
      cat(sprintf("%d\n", size[2]), file=con)
      cat(sprintf("%d\n", size[1]), file=con)
    }
    else if (format != "matrix") {
      close(con)
      unlink(filename)
      warning(format)
      stop(sprintf("Space file must have format %s, %s %s, or %s.",
                   dQuote("SRBT"),
                   dQuote("KST"),
                   dQuote("matrix"),
                   "spreadsheet format"
      ))
    }
    
    colnames(mat) <- NULL
    write.table(mat, sep="", file=con, col.names=FALSE, row.names=FALSE)
    
    close(con)
  }
}

Try the kstIO package in your browser

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

kstIO documentation built on Sept. 7, 2026, 9:06 a.m.