diff --git a/R/cpp11.R b/R/cpp11.R index 93d9a67..80ff3e5 100644 --- a/R/cpp11.R +++ b/R/cpp11.R @@ -1,15 +1,15 @@ # Generated by cpp11: do not edit by hand -fmm_to_vec <- function(filename) { - .Call(`_fastMatMR_fmm_to_vec`, filename) +cpp_fmm_to_vec <- function(filename) { + .Call(`_fastMatMR_cpp_fmm_to_vec`, filename) } -fmm_to_mat <- function(filename) { - .Call(`_fastMatMR_fmm_to_mat`, filename) +cpp_fmm_to_mat <- function(filename) { + .Call(`_fastMatMR_cpp_fmm_to_mat`, filename) } -fmm_to_sparse_Matrix <- function(filename) { - .Call(`_fastMatMR_fmm_to_sparse_Matrix`, filename) +cpp_fmm_to_sparse_Matrix <- function(filename) { + .Call(`_fastMatMR_cpp_fmm_to_sparse_Matrix`, filename) } vec_to_fmm <- function(r_vec, filename) { diff --git a/R/fastMatMR-package.R b/R/fastMatMR-package.R index a8617e6..083ceb6 100644 --- a/R/fastMatMR-package.R +++ b/R/fastMatMR-package.R @@ -15,7 +15,11 @@ #' \dontrun{ #' vec <- fmm_to_vec("matrix.mtx") #' } -NULL +fmm_to_vec <- function(filename) { + expanded_filename <- path.expand(filename) + result <- cpp_fmm_to_vec(expanded_filename) + return(result) +} #' @export fmm_to_mat #' @rdname fmm_to_mat @@ -29,7 +33,11 @@ NULL #' \dontrun{ #' mat <- fmm_to_mat("matrix.mtx") #' } -NULL +fmm_to_mat <- function(filename) { + expanded_filename <- path.expand(filename) + result <- cpp_fmm_to_mat(expanded_filename) + return(result) +} #' @export fmm_to_sparse_Matrix #' @rdname fmm_to_sparse_Matrix @@ -44,19 +52,22 @@ NULL #' \dontrun{ #' sparse_mat <- fmm_to_sparse_Matrix("sparse_matrix.mtx") #' } -NULL +fmm_to_sparse_Matrix <- function(filename) { + expanded_filename <- path.expand(filename) + result <- cpp_fmm_to_sparse_Matrix(expanded_filename) + return(result) +} #' @export vec_to_fmm #' @rdname vec_to_fmm #' @name vec_to_fmm #' @title Convert a Numeric Vector to Matrix Market Format #' @description This function takes a numeric vector and converts it into a -#' Matrix Market file. +#' Matrix Market output file. #' @param input A numeric vector to be converted. -#' @param fname The name of the output file where the Matrix Market formatted +#' @param filename The name of the output file where the Matrix Market formatted #' data will be saved. -#' @return This function has no return value. It writes a Matrix Market -#' formatted file to disk. +#' @return A boolean indicating success or failure. Writes a MTX file to disk. #' @examples #' \dontrun{ #' vec <- c(1, 2, 3) @@ -71,10 +82,9 @@ NULL #' @description This function takes a numeric matrix and converts it into a #' Matrix Market file. #' @param input A numeric matrix to be converted. -#' @param fname The name of the output file where the Matrix Market formatted +#' @param filename The name of the output file where the Matrix Market formatted #' data will be saved. -#' @return This function has no return value. It writes a Matrix Market -#' formatted file to disk. +#' @return A boolean indicating success or failure. Writes a MTX file to disk. #' @examples #' \dontrun{ #' mat <- matrix(c(1, 2, 3, 4), nrow = 2) @@ -89,10 +99,9 @@ NULL #' @description This function takes a sparse numeric matrix and converts it into #' a Matrix Market file. #' @param input A sparse numeric matrix to be converted. -#' @param fname The name of the output file where the Matrix Market formatted +#' @param filename The name of the output file where the Matrix Market formatted #' data will be saved. -#' @return This function has no return value. It writes a Matrix Market -#' formatted file to disk. +#' @return A boolean indicating success or failure. Writes a MTX file to disk. #' @examples #' \dontrun{ #' sparse_mat <- Matrix::Matrix(c(1, 0, 0, 2), nrow = 2, sparse = TRUE) @@ -108,12 +117,11 @@ NULL #' #' @param input A numeric object to be converted. This can be a numeric vector, #' a matrix, or a sparse matrix. -#' @param fname The name of the output file +#' @param filename The name of the output file #' where the Matrix Market formatted data will be saved. It is recommended to #' use a filename ending with ".mtx" for clarity. #' -#' @return This function has no return value. It writes a Matrix Market -#' formatted file to disk. +#' @return A boolean indicating success or failure. Writes a MTX file to disk. #' #' @examples #' \dontrun{ @@ -130,13 +138,15 @@ NULL #' } #' #' @export -write_fmm <- function(input, fname = "out.mtx") { +write_fmm <- function(input, filename = "out.mtx") { + # Expand the ~ character to the full path + expanded_fname <- path.expand(filename) if (is.vector(input)) { - return(vec_to_fmm(input, fname)) # nolint. C++ function. + return(vec_to_fmm(input, expanded_fname)) # nolint. C++ function. } else if (is.matrix(input)) { - return(mat_to_fmm(input, fname)) # nolint. C++ function. + return(mat_to_fmm(input, expanded_fname)) # nolint. C++ function. } else if (inherits(input, "sparseMatrix")) { - return(sparse_to_fmm(input, fname)) # nolint. C++ function. + return(sparse_to_fmm(input, expanded_fname)) # nolint. C++ function. } else { stop( paste( diff --git a/man/fmm_to_mat.Rd b/man/fmm_to_mat.Rd index 8e536b0..fcf4861 100644 --- a/man/fmm_to_mat.Rd +++ b/man/fmm_to_mat.Rd @@ -3,6 +3,9 @@ \name{fmm_to_mat} \alias{fmm_to_mat} \title{Convert Matrix Market File to Matrix} +\usage{ +fmm_to_mat(filename) +} \arguments{ \item{filename}{The name of the input Matrix Market file to be read.} } diff --git a/man/fmm_to_sparse_Matrix.Rd b/man/fmm_to_sparse_Matrix.Rd index cce562c..2e1da8f 100644 --- a/man/fmm_to_sparse_Matrix.Rd +++ b/man/fmm_to_sparse_Matrix.Rd @@ -3,6 +3,9 @@ \name{fmm_to_sparse_Matrix} \alias{fmm_to_sparse_Matrix} \title{Convert Matrix Market File to Sparse Matrix} +\usage{ +fmm_to_sparse_Matrix(filename) +} \arguments{ \item{filename}{The name of the input Matrix Market file to be read.} } diff --git a/man/fmm_to_vec.Rd b/man/fmm_to_vec.Rd index a587f74..8fc176b 100644 --- a/man/fmm_to_vec.Rd +++ b/man/fmm_to_vec.Rd @@ -3,6 +3,9 @@ \name{fmm_to_vec} \alias{fmm_to_vec} \title{Convert Matrix Market File to Numeric Vector} +\usage{ +fmm_to_vec(filename) +} \arguments{ \item{filename}{The name of the input Matrix Market file to be read.} } diff --git a/man/mat_to_fmm.Rd b/man/mat_to_fmm.Rd index dae61ea..5132b5b 100644 --- a/man/mat_to_fmm.Rd +++ b/man/mat_to_fmm.Rd @@ -6,7 +6,7 @@ \arguments{ \item{input}{A numeric matrix to be converted.} -\item{fname}{The name of the output file where the Matrix Market formatted +\item{filename}{The name of the output file where the Matrix Market formatted data will be saved.} } \value{ diff --git a/man/sparse_to_fmm.Rd b/man/sparse_to_fmm.Rd index e9d2278..2938ac6 100644 --- a/man/sparse_to_fmm.Rd +++ b/man/sparse_to_fmm.Rd @@ -6,7 +6,7 @@ \arguments{ \item{input}{A sparse numeric matrix to be converted.} -\item{fname}{The name of the output file where the Matrix Market formatted +\item{filename}{The name of the output file where the Matrix Market formatted data will be saved.} } \value{ diff --git a/man/vec_to_fmm.Rd b/man/vec_to_fmm.Rd index b7815d9..05b8323 100644 --- a/man/vec_to_fmm.Rd +++ b/man/vec_to_fmm.Rd @@ -6,7 +6,7 @@ \arguments{ \item{input}{A numeric vector to be converted.} -\item{fname}{The name of the output file where the Matrix Market formatted +\item{filename}{The name of the output file where the Matrix Market formatted data will be saved.} } \value{ diff --git a/man/write_fmm.Rd b/man/write_fmm.Rd index ed4a729..bc9bdf6 100644 --- a/man/write_fmm.Rd +++ b/man/write_fmm.Rd @@ -4,13 +4,13 @@ \alias{write_fmm} \title{Convert Various Numeric Types to Matrix Market Format} \usage{ -write_fmm(input, fname = "out.mtx") +write_fmm(input, filename = "out.mtx") } \arguments{ \item{input}{A numeric object to be converted. This can be a numeric vector, a matrix, or a sparse matrix.} -\item{fname}{The name of the output file +\item{filename}{The name of the output file where the Matrix Market formatted data will be saved. It is recommended to use a filename ending with ".mtx" for clarity.} } diff --git a/src/cpp11.cpp b/src/cpp11.cpp index 2bb2e2d..3daac4b 100644 --- a/src/cpp11.cpp +++ b/src/cpp11.cpp @@ -6,24 +6,24 @@ #include // from_file.cpp -cpp11::doubles fmm_to_vec(const std::string & filename); -extern "C" SEXP _fastMatMR_fmm_to_vec(SEXP filename) { +cpp11::doubles cpp_fmm_to_vec(const std::string & filename); +extern "C" SEXP _fastMatMR_cpp_fmm_to_vec(SEXP filename) { BEGIN_CPP11 - return cpp11::as_sexp(fmm_to_vec(cpp11::as_cpp>(filename))); + return cpp11::as_sexp(cpp_fmm_to_vec(cpp11::as_cpp>(filename))); END_CPP11 } // from_file.cpp -cpp11::doubles_matrix<> fmm_to_mat(const std::string & filename); -extern "C" SEXP _fastMatMR_fmm_to_mat(SEXP filename) { +cpp11::doubles_matrix<> cpp_fmm_to_mat(const std::string & filename); +extern "C" SEXP _fastMatMR_cpp_fmm_to_mat(SEXP filename) { BEGIN_CPP11 - return cpp11::as_sexp(fmm_to_mat(cpp11::as_cpp>(filename))); + return cpp11::as_sexp(cpp_fmm_to_mat(cpp11::as_cpp>(filename))); END_CPP11 } // from_file.cpp -cpp11::sexp fmm_to_sparse_Matrix(const std::string & filename); -extern "C" SEXP _fastMatMR_fmm_to_sparse_Matrix(SEXP filename) { +cpp11::sexp cpp_fmm_to_sparse_Matrix(const std::string & filename); +extern "C" SEXP _fastMatMR_cpp_fmm_to_sparse_Matrix(SEXP filename) { BEGIN_CPP11 - return cpp11::as_sexp(fmm_to_sparse_Matrix(cpp11::as_cpp>(filename))); + return cpp11::as_sexp(cpp_fmm_to_sparse_Matrix(cpp11::as_cpp>(filename))); END_CPP11 } // to_file.cpp @@ -50,12 +50,12 @@ extern "C" SEXP _fastMatMR_sparse_to_fmm(SEXP input, SEXP filename) { extern "C" { static const R_CallMethodDef CallEntries[] = { - {"_fastMatMR_fmm_to_mat", (DL_FUNC) &_fastMatMR_fmm_to_mat, 1}, - {"_fastMatMR_fmm_to_sparse_Matrix", (DL_FUNC) &_fastMatMR_fmm_to_sparse_Matrix, 1}, - {"_fastMatMR_fmm_to_vec", (DL_FUNC) &_fastMatMR_fmm_to_vec, 1}, - {"_fastMatMR_mat_to_fmm", (DL_FUNC) &_fastMatMR_mat_to_fmm, 2}, - {"_fastMatMR_sparse_to_fmm", (DL_FUNC) &_fastMatMR_sparse_to_fmm, 2}, - {"_fastMatMR_vec_to_fmm", (DL_FUNC) &_fastMatMR_vec_to_fmm, 2}, + {"_fastMatMR_cpp_fmm_to_mat", (DL_FUNC) &_fastMatMR_cpp_fmm_to_mat, 1}, + {"_fastMatMR_cpp_fmm_to_sparse_Matrix", (DL_FUNC) &_fastMatMR_cpp_fmm_to_sparse_Matrix, 1}, + {"_fastMatMR_cpp_fmm_to_vec", (DL_FUNC) &_fastMatMR_cpp_fmm_to_vec, 1}, + {"_fastMatMR_mat_to_fmm", (DL_FUNC) &_fastMatMR_mat_to_fmm, 2}, + {"_fastMatMR_sparse_to_fmm", (DL_FUNC) &_fastMatMR_sparse_to_fmm, 2}, + {"_fastMatMR_vec_to_fmm", (DL_FUNC) &_fastMatMR_vec_to_fmm, 2}, {NULL, NULL, 0} }; } diff --git a/src/from_file.cpp b/src/from_file.cpp index 2f8d728..e0c5bdf 100644 --- a/src/from_file.cpp +++ b/src/from_file.cpp @@ -17,7 +17,7 @@ namespace fmm = fast_matrix_market; [[cpp11::register]] // cpp11::doubles -fmm_to_vec(const std::string &filename) { +cpp_fmm_to_vec(const std::string &filename) { std::ifstream file_stream; std::vector vec; @@ -34,7 +34,7 @@ fmm_to_vec(const std::string &filename) { [[cpp11::register]] // cpp11::doubles_matrix<> -fmm_to_mat(const std::string &filename) { +cpp_fmm_to_mat(const std::string &filename) { std::ifstream file_stream; fmm::matrix_market_header header; std::vector vec; @@ -61,7 +61,7 @@ fmm_to_mat(const std::string &filename) { [[cpp11::register]] // cpp11::sexp -fmm_to_sparse_Matrix(const std::string &filename) { +cpp_fmm_to_sparse_Matrix(const std::string &filename) { // TODO: Can speed this up by constructing from SEXP instead of via Matrix // constructor later using namespace cpp11::literals; diff --git a/src/to_file.cpp b/src/to_file.cpp index fd2eaee..ad87136 100644 --- a/src/to_file.cpp +++ b/src/to_file.cpp @@ -17,13 +17,9 @@ bool vec_to_fmm(cpp11::doubles r_vec, std::string filename) { std::vector std_vec(r_vec.size()); std::copy(r_vec.begin(), r_vec.end(), std_vec.begin()); fmm::matrix_market_header header(1, std_vec.size()); - // header.comment = std::string("comment"); - // Use C++17 filesystem to construct ofstream std::filesystem::path file_path(filename); std::ofstream os(file_path); - if (!os.is_open()) { - // Handle error return false; } fmm::write_matrix_market_array(os, header, std_vec);