Skip to content

Commit

Permalink
matrices closes #10
Browse files Browse the repository at this point in the history
  • Loading branch information
SymbolixAU committed Oct 10, 2018
1 parent 0eee292 commit e747e21
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 7 deletions.
12 changes: 12 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ rcpp_list_to_json <- function(lst) {
.Call(`_jsonify_rcpp_list_to_json`, lst)
}

rcpp_integer_matrix_to_json <- function(m) {
.Call(`_jsonify_rcpp_integer_matrix_to_json`, m)
}

rcpp_numeric_matrix_to_json <- function(m) {
.Call(`_jsonify_rcpp_numeric_matrix_to_json`, m)
}

rcpp_character_matrix_to_json <- function(m) {
.Call(`_jsonify_rcpp_character_matrix_to_json`, m)
}

rcpp_validate_json <- function(geojson) {
.Call(`_jsonify_rcpp_validate_json`, geojson)
}
Expand Down
14 changes: 12 additions & 2 deletions R/to_json.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,13 @@ to_json.logical <- function( x, ... ) rcpp_logical_to_json( x )
#' @export
to_json.complex <- function( x, ... ) rcpp_character_to_json( x )

#' @rdname to_json
#' @export
to_json.matrix <- function( x, ... ) stop("currently matrices are not supported")
to_json.matrix <- function( x, ... ) {
if( is.integer( x ) ) return( rcpp_integer_matrix_to_json( x ) )
if( is.numeric( x ) ) return( rcpp_numeric_matrix_to_json( x ) )
return( rcpp_character_matrix_to_json( x ) )
}

#' @rdname to_json
#' @export
Expand Down Expand Up @@ -87,4 +92,9 @@ handle_dates <- function( x ) {
dte <- date_columns( x )
for ( i in dte ) x[[i]] <- as.character( x[[i]] )
return( x )
}
}





2 changes: 1 addition & 1 deletion inst/include/jsonify/to_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
#include "to_json/dataframe.hpp"
#include "to_json/vectors.hpp"
#include "to_json/writers.hpp"

#include "to_json/matrix.hpp"
#endif
46 changes: 44 additions & 2 deletions inst/include/jsonify/to_json/matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,52 @@
namespace jsonify {
namespace matrix {

inline Rccp::StringVector to_json( Rcpp::NmericVector& nm ) {
// TODO( )
inline Rcpp::StringVector to_json( Rcpp::NumericMatrix& nm ) {
rapidjson::StringBuffer sb;
rapidjson::Writer < rapidjson::StringBuffer > writer( sb );
writer.StartArray();

int n = nm.nrow();
int i;
for ( i = 0; i < n; i++ ) {
Rcpp::NumericVector this_row = nm(i, Rcpp::_);
writers::write_value( writer, this_row );
}
writer.EndArray();
return jsonify::utils::finalise_json( sb );
}

inline Rcpp::StringVector to_json( Rcpp::IntegerMatrix& im ) {
rapidjson::StringBuffer sb;
rapidjson::Writer < rapidjson::StringBuffer > writer( sb );
writer.StartArray();

int n = im.nrow();
int i;
for ( i = 0; i < n; i++ ) {
Rcpp::IntegerVector this_row = im(i, Rcpp::_);
writers::write_value( writer, this_row );
}
writer.EndArray();
return jsonify::utils::finalise_json( sb );
}

inline Rcpp::StringVector to_json( Rcpp::CharacterMatrix& cm ) {
rapidjson::StringBuffer sb;
rapidjson::Writer < rapidjson::StringBuffer > writer( sb );
writer.StartArray();

int n = cm.nrow();
int i;
for ( i = 0; i < n; i++ ) {
Rcpp::StringVector this_row = cm(i, Rcpp::_);
writers::write_value( writer, this_row );
}
writer.EndArray();
return jsonify::utils::finalise_json( sb );
}


} // namespace matrix
} // namespace jsonify

Expand Down
3 changes: 3 additions & 0 deletions man/to_json.Rd

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

36 changes: 36 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,39 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// rcpp_integer_matrix_to_json
Rcpp::StringVector rcpp_integer_matrix_to_json(Rcpp::IntegerMatrix m);
RcppExport SEXP _jsonify_rcpp_integer_matrix_to_json(SEXP mSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< Rcpp::IntegerMatrix >::type m(mSEXP);
rcpp_result_gen = Rcpp::wrap(rcpp_integer_matrix_to_json(m));
return rcpp_result_gen;
END_RCPP
}
// rcpp_numeric_matrix_to_json
Rcpp::StringVector rcpp_numeric_matrix_to_json(Rcpp::NumericMatrix m);
RcppExport SEXP _jsonify_rcpp_numeric_matrix_to_json(SEXP mSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< Rcpp::NumericMatrix >::type m(mSEXP);
rcpp_result_gen = Rcpp::wrap(rcpp_numeric_matrix_to_json(m));
return rcpp_result_gen;
END_RCPP
}
// rcpp_character_matrix_to_json
Rcpp::StringVector rcpp_character_matrix_to_json(Rcpp::CharacterMatrix m);
RcppExport SEXP _jsonify_rcpp_character_matrix_to_json(SEXP mSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< Rcpp::CharacterMatrix >::type m(mSEXP);
rcpp_result_gen = Rcpp::wrap(rcpp_character_matrix_to_json(m));
return rcpp_result_gen;
END_RCPP
}
// rcpp_validate_json
Rcpp::LogicalVector rcpp_validate_json(Rcpp::StringVector geojson);
RcppExport SEXP _jsonify_rcpp_validate_json(SEXP geojsonSEXP) {
Expand All @@ -90,6 +123,9 @@ static const R_CallMethodDef CallEntries[] = {
{"_jsonify_rcpp_integer_to_json", (DL_FUNC) &_jsonify_rcpp_integer_to_json, 1},
{"_jsonify_rcpp_logical_to_json", (DL_FUNC) &_jsonify_rcpp_logical_to_json, 1},
{"_jsonify_rcpp_list_to_json", (DL_FUNC) &_jsonify_rcpp_list_to_json, 1},
{"_jsonify_rcpp_integer_matrix_to_json", (DL_FUNC) &_jsonify_rcpp_integer_matrix_to_json, 1},
{"_jsonify_rcpp_numeric_matrix_to_json", (DL_FUNC) &_jsonify_rcpp_numeric_matrix_to_json, 1},
{"_jsonify_rcpp_character_matrix_to_json", (DL_FUNC) &_jsonify_rcpp_character_matrix_to_json, 1},
{"_jsonify_rcpp_validate_json", (DL_FUNC) &_jsonify_rcpp_validate_json, 1},
{NULL, NULL, 0}
};
Expand Down
15 changes: 15 additions & 0 deletions src/to_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,18 @@ Rcpp::StringVector rcpp_logical_to_json( Rcpp::LogicalVector iv ) {
Rcpp::StringVector rcpp_list_to_json( SEXP lst ) {
return jsonify::vectors::to_json( lst );
}

// [[Rcpp::export]]
Rcpp::StringVector rcpp_integer_matrix_to_json( Rcpp::IntegerMatrix m ) {
return jsonify::matrix::to_json( m );
}

// [[Rcpp::export]]
Rcpp::StringVector rcpp_numeric_matrix_to_json( Rcpp::NumericMatrix m ) {
return jsonify::matrix::to_json( m );
}

// [[Rcpp::export]]
Rcpp::StringVector rcpp_character_matrix_to_json( Rcpp::CharacterMatrix m ) {
return jsonify::matrix::to_json( m );
}
6 changes: 4 additions & 2 deletions tests/testthat/test-to_json_matrix.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
context("matrix")

test_that("matrix stops",{
expect_error(to_json(matrix()))
test_that("matrix converted correctly",{
expect_equal(as.character(to_json(matrix(letters[1:6], ncol = 3))), "[[\"a\",\"c\",\"e\"],[\"b\",\"d\",\"f\"]]")
expect_equal(as.character(to_json(matrix(1L:6L,ncol=3))), "[[1,3,5],[2,4,6]]")
expect_equal(as.character(to_json(matrix(c(1.1,2.1,3.1,4.1,5.1,6.1),ncol=3))), "[[1.1,3.1,5.1],[2.1,4.1,6.1]]")
})

0 comments on commit e747e21

Please sign in to comment.