diff --git a/R/RcppExports.R b/R/RcppExports.R index 206ece8..b3a2530 100644 --- a/R/RcppExports.R +++ b/R/RcppExports.R @@ -1,12 +1,12 @@ # Generated by using Rcpp::compileAttributes() -> do not edit by hand # Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393 -rcpp_colour_values_hex <- function(x, palette, alpha, na_colour = "#808080", include_alpha = TRUE, format = FALSE, format_type = "numeric", digits = 2L, summary = FALSE, n_summaries = 0L) { - .Call(`_colourvalues_rcpp_colour_values_hex`, x, palette, alpha, na_colour, include_alpha, format, format_type, digits, summary, n_summaries) +rcpp_colour_values_hex <- function(x, palette, alpha, na_colour = "#808080", include_alpha = TRUE, format = FALSE, digits = 2L, summary = FALSE, n_summaries = 0L) { + .Call(`_colourvalues_rcpp_colour_values_hex`, x, palette, alpha, na_colour, include_alpha, format, digits, summary, n_summaries) } -rcpp_colour_values_rgb <- function(x, palette, alpha, na_colour = "#808080", include_alpha = TRUE, format = FALSE, format_type = "numeric", digits = 2L, summary = FALSE, n_summaries = 0L) { - .Call(`_colourvalues_rcpp_colour_values_rgb`, x, palette, alpha, na_colour, include_alpha, format, format_type, digits, summary, n_summaries) +rcpp_colour_values_rgb <- function(x, palette, alpha, na_colour = "#808080", include_alpha = TRUE, format = FALSE, digits = 2L, summary = FALSE, n_summaries = 0L) { + .Call(`_colourvalues_rcpp_colour_values_rgb`, x, palette, alpha, na_colour, include_alpha, format, digits, summary, n_summaries) } rcpp_convert_hex_to_rgb <- function(hex_strings) { @@ -21,12 +21,20 @@ rcpp_convert_rgb_vec_to_hex <- function(rgb) { .Call(`_colourvalues_rcpp_convert_rgb_vec_to_hex`, rgb) } +rcpp_get_format_type <- function(palette_type) { + .Call(`_colourvalues_rcpp_get_format_type`, palette_type) +} + +rcpp_get_r_class <- function(obj) { + .Call(`_colourvalues_rcpp_get_r_class`, obj) +} + rcpp_vector_type <- function(new_type, existing_type) { .Call(`_colourvalues_rcpp_vector_type`, new_type, existing_type) } -rcpp_list_size <- function(lst, total_size, existing_type) { - .Call(`_colourvalues_rcpp_list_size`, lst, total_size, existing_type) +rcpp_list_size <- function(lst, total_size, existing_type, format_type) { + .Call(`_colourvalues_rcpp_list_size`, lst, total_size, existing_type, format_type) } rcpp_refil_list <- function(lst_sizes, colours, vector_position) { diff --git a/R/colour_values_hex.R b/R/colour_values_hex.R index 0513328..988c061 100644 --- a/R/colour_values_hex.R +++ b/R/colour_values_hex.R @@ -147,7 +147,6 @@ colour_values_to_hex.Date <- function( x, palette, alpha, na_colour, include_al , include_alpha = include_alpha , n_summaries = n_summaries , format = format - , format_type = "Date" , digits = 0 ) } @@ -165,7 +164,6 @@ colour_values_to_hex.POSIXct <- function( x, palette, alpha, na_colour, include , include_alpha = include_alpha , n_summaries = n_summaries , format = format - , format_type = "POSIXct" , digits = 0 ) } @@ -182,7 +180,6 @@ colour_values_to_hex.POSIXlt <- function( x, palette, na_colour, alpha, include , include_alpha = include_alpha , n_summaries = n_summaries , format = format - , format_type = "POSIXct" , digits = 0 ) } @@ -196,7 +193,6 @@ colour_values_to_hex.default <- function( na_colour, include_alpha, format = TRUE, - format_type = "numeric", digits = 2, summary = FALSE, n_summaries = 0 @@ -213,7 +209,6 @@ colour_values_to_hex.default <- function( , na_colour = na_colour , include_alpha = include_alpha , format = format - , format_type = format_type , digits = digits , summary = summary , n_summaries = n_summaries diff --git a/R/colour_values_rgb.R b/R/colour_values_rgb.R index 6bb0bcf..b2d5371 100644 --- a/R/colour_values_rgb.R +++ b/R/colour_values_rgb.R @@ -106,7 +106,6 @@ colour_values_to_rgb.Date <- function( x, palette, alpha, na_colour, include_al , include_alpha = include_alpha , n_summaries = n_summaries , format = format - , format_type = "Date" , digits = 0 ) } @@ -124,7 +123,6 @@ colour_values_to_rgb.POSIXct <- function( x, palette, alpha, na_colour, include , include_alpha = include_alpha , n_summaries = n_summaries , format = format - , format_type = "POSIXct" , digits = 0 ) } @@ -141,7 +139,6 @@ colour_values_to_rgb.POSIXlt <- function( x, palette, na_colour, alpha, include , include_alpha = include_alpha , n_summaries = n_summaries , format = format - , format_type = "POSIXct" , digits = 0 ) } @@ -155,7 +152,6 @@ colour_values_to_rgb.default <- function( na_colour, include_alpha, format = TRUE, - format_type = "numeric", digits = 2, summary = FALSE, n_summaries = 0 @@ -172,7 +168,6 @@ colour_values_to_rgb.default <- function( , na_colour = na_colour , include_alpha = include_alpha , format = format - , format_type = format_type , digits = digits , summary = summary , n_summaries = n_summaries diff --git a/inst/include/colourvalues/api/api.hpp b/inst/include/colourvalues/api/api.hpp index 26811e2..a3c5c2a 100644 --- a/inst/include/colourvalues/api/api.hpp +++ b/inst/include/colourvalues/api/api.hpp @@ -1,6 +1,8 @@ #ifndef R_COLOURVALUES_API_H #define R_COLOURVALUES_API_H +#include + #include "colourvalues/api/api_hex.hpp" #include "colourvalues/api/api_rgb.hpp" diff --git a/inst/include/colourvalues/api/api_hex.hpp b/inst/include/colourvalues/api/api_hex.hpp index 82c3142..a86957e 100644 --- a/inst/include/colourvalues/api/api_hex.hpp +++ b/inst/include/colourvalues/api/api_hex.hpp @@ -13,14 +13,13 @@ namespace api { /* * list with matrix palette */ - SEXP colour_values_hex( + inline SEXP colour_values_hex( Rcpp::List lst, Rcpp::NumericMatrix& palette, Rcpp::NumericVector& alpha, std::string na_colour = "#808080", bool include_alpha = true, bool format = false, - std::string format_type = "numeric", int digits = 2, bool summary = false, int n_summaries = 0 @@ -30,7 +29,12 @@ namespace api { int position = 0; - Rcpp::List lst_sizes = colourvalues::list::list_size( lst, total_size, existing_type ); + + // TODO: get format_type depending on the type of list elements + std::string format_type = "numeric"; + // follow heirarchy of classes: + // + Rcpp::List lst_sizes = colourvalues::list::list_size( lst, total_size, existing_type, format_type ); switch( existing_type ) { case INTSXP: { } // 13 @@ -45,7 +49,7 @@ namespace api { } SEXP coloured_values = colourvalues::colours_hex::colour_value_hex( - colours, palette, na_colour, include_alpha, n_summaries, format, format_type, digits + colours, palette, na_colour, include_alpha, format_type, n_summaries, format, digits ); position = 0; @@ -89,14 +93,13 @@ namespace api { /* * list with string palette */ - SEXP colour_values_hex( + inline SEXP colour_values_hex( Rcpp::List lst, std::string& palette, Rcpp::NumericVector& alpha, std::string na_colour = "#808080", bool include_alpha = true, bool format = false, - std::string format_type = "numeric", int digits = 2, bool summary = false, int n_summaries = 0 @@ -106,7 +109,12 @@ namespace api { int position = 0; - Rcpp::List lst_sizes = colourvalues::list::list_size( lst, total_size, existing_type ); + + // TODO: get format_type depending on the type of list elements + std::string format_type = "numeric"; + + Rcpp::List lst_sizes = colourvalues::list::list_size( lst, total_size, existing_type, format_type ); + switch( existing_type ) { case INTSXP: { } // 13 case REALSXP: { // 14 @@ -120,7 +128,7 @@ namespace api { } SEXP coloured_values = colourvalues::colours_hex::colour_value_hex( - colours, palette, na_colour, alpha, include_alpha, n_summaries, format, format_type, digits + colours, palette, na_colour, alpha, include_alpha, format_type, n_summaries, format, digits ); position = 0; @@ -141,9 +149,9 @@ namespace api { default: { if( n_summaries > 0 ) { - Rcpp::warning("n_summaries not valid for character values, using summary = T"); - summary = true; - } + Rcpp::warning("n_summaries not valid for character values, using summary = T"); + summary = true; + } Rcpp::StringVector colours( total_size ); colourvalues::list::unlist_list( lst, lst_sizes, colours, position ); @@ -175,37 +183,37 @@ namespace api { * when palette is unknown, but vector is numeric */ inline SEXP colour_values_hex( - Rcpp::NumericVector& x, - SEXP palette, - Rcpp::NumericVector& alpha, - std::string& na_colour, - bool include_alpha = true, - bool format = false, - std::string format_type = "numeric", - int digits = 2, - int n_summaries = 0 + Rcpp::NumericVector& x, + SEXP palette, + Rcpp::NumericVector& alpha, + std::string& format_type, + std::string& na_colour, + bool include_alpha = true, + bool format = false, + int digits = 2, + int n_summaries = 0 ) { //Rcpp::Rcout << "NumericVector x, SEXP palette " << std::endl; switch( TYPEOF( palette ) ) { - // STringVector - needs to get std::string + // STringVector - needs to get std::string case STRSXP: { Rcpp::StringVector sv = Rcpp::as< Rcpp::StringVector >( palette ); Rcpp::String s = sv[0]; std::string pal = s; return colourvalues::colours_hex::colour_value_hex( - x, pal, na_colour, alpha, include_alpha, n_summaries, format, format_type, digits + x, pal, na_colour, alpha, include_alpha, format_type, n_summaries, format, digits ); } case INTSXP: {} case REALSXP: { if( !Rf_isMatrix( palette ) ) { - Rcpp::stop("Unknown palette type - expecting a matrix"); - } + Rcpp::stop("Unknown palette type - expecting a matrix"); + } Rcpp::NumericMatrix pal = Rcpp::as< Rcpp::NumericMatrix >( palette ); return colourvalues::colours_hex::colour_value_hex( - x, pal, na_colour, include_alpha, n_summaries, format, format_type, digits + x, pal, na_colour, include_alpha, format_type, n_summaries, format, digits ); } default: { @@ -222,10 +230,10 @@ namespace api { Rcpp::StringVector& x, SEXP palette, Rcpp::NumericVector& alpha, + std::string& format_type, std::string na_colour = "#808080", bool include_alpha = true, bool format = false, - std::string format_type = "numeric", int digits = 2, bool summary = false ) { @@ -267,25 +275,25 @@ namespace api { std::string na_colour = "#808080", bool include_alpha = true, bool format = false, - std::string format_type = "numeric", int digits = 2, bool summary = false, int n_summaries = 0 ) { //Rcpp::Rcout << "SEXP x, NumericMatrix palette " << std::endl; //Rcpp::Rcout << "include_alpha: " << include_alpha << std::endl; + std::string format_type = colourvalues::format::get_format_type( x ); switch( TYPEOF( x ) ) { case INTSXP: {} case REALSXP: { Rcpp::NumericVector nv = Rcpp::as< Rcpp::NumericVector >( x ); return colourvalues::colours_hex::colour_value_hex( - nv, palette, na_colour, include_alpha, n_summaries, format, format_type, digits + nv, palette, na_colour, include_alpha, format_type, n_summaries, format, digits ); } case VECSXP: { // list Rcpp::List lst = Rcpp::as< Rcpp::List >( x ); - return colour_values_hex( lst, palette, alpha, na_colour, include_alpha, format, format_type, digits, summary, n_summaries ); + return colour_values_hex( lst, palette, alpha, na_colour, include_alpha, format, digits, summary, n_summaries ); } case LGLSXP: {} // as.character @@ -309,7 +317,6 @@ namespace api { std::string na_colour = "#808080", bool include_alpha = true, bool format = false, - std::string format_type = "numeric", int digits = 2, bool summary = false, int n_summaries = 0 @@ -317,6 +324,7 @@ namespace api { //Rcpp::Rcout << "SEXP x, StringVector palette " << std::endl; //Rcpp::Rcout << "typeof x: " << TYPEOF( x ) << std::endl; + std::string format_type = colourvalues::format::get_format_type( x ); Rcpp::String p = palette[0]; std::string pal = p; @@ -331,7 +339,7 @@ namespace api { } else { Rcpp::NumericVector nv = Rcpp::clone(x); return colourvalues::colours_hex::colour_value_hex( - nv, pal, na_colour, alpha, include_alpha, n_summaries, format, format_type, digits + nv, pal, na_colour, alpha, include_alpha, format_type, n_summaries, format, digits ); } } @@ -339,12 +347,12 @@ namespace api { //Rcpp::NumericVector nv = Rcpp::as< Rcpp::NumericVector >( x ); Rcpp::NumericVector nv = Rcpp::clone(x); return colourvalues::colours_hex::colour_value_hex( - nv, pal, na_colour, alpha, include_alpha, n_summaries, format, format_type, digits + nv, pal, na_colour, alpha, include_alpha, format_type, n_summaries, format, digits ); } case VECSXP: { // list Rcpp::List lst = Rcpp::as< Rcpp::List >( x ); - return colour_values_hex( lst, pal, alpha, na_colour, include_alpha, format, format_type, digits, summary, n_summaries ); + return colour_values_hex( lst, pal, alpha, na_colour, include_alpha, format, digits, summary, n_summaries ); } case LGLSXP: {} // as.character default: { @@ -369,27 +377,28 @@ namespace api { std::string na_colour = "#808080", bool include_alpha = true, bool format = false, - std::string format_type = "numeric", int digits = 2, bool summary = false, int n_summaries = 0 ) { //Rcpp::Rcout << "SEXP x, SEXP palette " << std::endl; + std::string format_type = colourvalues::format::get_format_type( x ); + //Rcpp::Rcout << "format_type: " << format_type << std::endl; switch( TYPEOF( palette ) ) { case INTSXP: {} case REALSXP: { Rcpp::NumericMatrix pal = Rcpp::as< Rcpp::NumericMatrix >( palette ); return colour_values_hex( - x, pal, alpha, na_colour, include_alpha, format, format_type, digits, summary, n_summaries + x, pal, alpha, na_colour, include_alpha, format, digits, summary, n_summaries ); break; } case STRSXP: { Rcpp::StringVector sv = Rcpp::as< Rcpp::StringVector >( palette ); return colour_values_hex( - x, sv, alpha, na_colour, include_alpha, format, format_type, digits, summary, n_summaries + x, sv, alpha, na_colour, include_alpha, format, digits, summary, n_summaries ); break; } @@ -403,7 +412,6 @@ namespace api { } - } // api } // colourvalues diff --git a/inst/include/colourvalues/api/api_rgb.hpp b/inst/include/colourvalues/api/api_rgb.hpp index 52329d1..411bb27 100644 --- a/inst/include/colourvalues/api/api_rgb.hpp +++ b/inst/include/colourvalues/api/api_rgb.hpp @@ -13,14 +13,13 @@ namespace api { /* * list with string palette */ - SEXP colour_values_rgb( + inline SEXP colour_values_rgb( Rcpp::List lst, Rcpp::NumericMatrix& palette, Rcpp::NumericVector& alpha, std::string na_colour = "#808080", bool include_alpha = true, bool format = false, - std::string format_type = "numeric", int digits = 2, bool summary = false, int n_summaries = 0 @@ -30,7 +29,12 @@ namespace api { int position = 0; - Rcpp::List lst_sizes = colourvalues::list::list_size( lst, total_size, existing_type ); + + // TODO: get format_type depending on the type of list elements + std::string format_type = "character"; + + Rcpp::List lst_sizes = colourvalues::list::list_size( lst, total_size, existing_type, format_type ); + switch( existing_type ) { case INTSXP: { } // 13 case REALSXP: { // 14 @@ -45,7 +49,7 @@ namespace api { } SEXP coloured_values = colourvalues::colours_rgb::colour_value_rgb( - colours, palette, na_colour, include_alpha, n_summaries, format, format_type, digits + colours, palette, na_colour, include_alpha, format_type, n_summaries, format, digits ); position = 0; @@ -100,14 +104,13 @@ namespace api { /* * list with string palette */ - SEXP colour_values_rgb( + inline SEXP colour_values_rgb( Rcpp::List lst, std::string& palette, Rcpp::NumericVector& alpha, std::string na_colour = "#808080", bool include_alpha = true, bool format = false, - std::string format_type = "numeric", int digits = 2, bool summary = false, int n_summaries = 0 @@ -119,7 +122,12 @@ namespace api { //Rcpp::Rcout << "now doing list " << std::endl; - Rcpp::List lst_sizes = colourvalues::list::list_size( lst, total_size, existing_type ); + + // TODO: get format_type depending on the type of list elements + std::string format_type = "character"; + + Rcpp::List lst_sizes = colourvalues::list::list_size( lst, total_size, existing_type, format_type ); + switch( existing_type ) { case INTSXP: { } // 13 case REALSXP: { // 14 @@ -135,7 +143,7 @@ namespace api { //Rcpp::Rcout << "colouring values" << std::endl; SEXP coloured_values = colourvalues::colours_rgb::colour_value_rgb( - colours, palette, na_colour, alpha, include_alpha, n_summaries, format, format_type, digits + colours, palette, na_colour, alpha, include_alpha, format_type, n_summaries, format, digits ); //Rcpp::Rcout << "coloured values" << std::endl; @@ -157,9 +165,9 @@ namespace api { default: { if( n_summaries > 0 ) { - Rcpp::warning("n_summaries not valid for character values, using summary = T"); - summary = true; - } + Rcpp::warning("n_summaries not valid for character values, using summary = T"); + summary = true; + } Rcpp::StringVector colours( total_size ); colourvalues::list::unlist_list( lst, lst_sizes, colours, position ); @@ -194,10 +202,10 @@ namespace api { Rcpp::NumericVector& x, SEXP palette, Rcpp::NumericVector& alpha, + std::string& format_type, std::string& na_colour, bool include_alpha = true, bool format = false, - std::string format_type = "numeric", int digits = 2, int n_summaries = 0 ) { @@ -211,7 +219,7 @@ namespace api { Rcpp::String s = sv[0]; std::string pal = s; return colourvalues::colours_rgb::colour_value_rgb( - x, pal, na_colour, alpha, include_alpha, n_summaries, format, format_type, digits + x, pal, na_colour, alpha, include_alpha, format_type, n_summaries, format, digits ); } case INTSXP: {} @@ -221,7 +229,7 @@ namespace api { } Rcpp::NumericMatrix pal = Rcpp::as< Rcpp::NumericMatrix >( palette ); return colourvalues::colours_rgb::colour_value_rgb( - x, pal, na_colour, include_alpha, n_summaries, format, format_type, digits + x, pal, na_colour, include_alpha, format_type, n_summaries, format, digits ); } default: { @@ -238,10 +246,10 @@ namespace api { Rcpp::StringVector& x, SEXP palette, Rcpp::NumericVector& alpha, + std::string& format_type, std::string na_colour = "#808080", bool include_alpha = true, bool format = false, - std::string format_type = "numeric", int digits = 2, bool summary = false ) { @@ -283,7 +291,6 @@ namespace api { std::string na_colour = "#808080", bool include_alpha = true, bool format = false, - std::string format_type = "numeric", int digits = 2, bool summary = false, int n_summaries = 0 @@ -291,12 +298,14 @@ namespace api { //Rcpp::Rcout << "SEXP x, NumericMatrix palette " << std::endl; //Rcpp::Rcout << "include_alpha: " << include_alpha << std::endl; + std::string format_type = colourvalues::format::get_format_type( x ); + switch( TYPEOF( x ) ) { case INTSXP: {} case REALSXP: { Rcpp::NumericVector nv = Rcpp::as< Rcpp::NumericVector >( x ); return colourvalues::colours_rgb::colour_value_rgb( - nv, palette, na_colour, include_alpha, n_summaries, format, format_type, digits + nv, palette, na_colour, include_alpha, format_type, n_summaries, format, digits ); } case VECSXP: { // list @@ -326,7 +335,6 @@ namespace api { std::string na_colour = "#808080", bool include_alpha = true, bool format = false, - std::string format_type = "numeric", int digits = 2, bool summary = false, int n_summaries = 0 @@ -334,6 +342,7 @@ namespace api { //Rcpp::Rcout << "SEXP x, StringVector palette " << std::endl; //Rcpp::Rcout << "typeof x: " << TYPEOF( x ) << std::endl; + std::string format_type = colourvalues::format::get_format_type( x ); Rcpp::String p = palette[0]; std::string pal = p; @@ -348,7 +357,7 @@ namespace api { } else { Rcpp::NumericVector nv = Rcpp::clone(x); return colourvalues::colours_rgb::colour_value_rgb( - nv, pal, na_colour, alpha, include_alpha, n_summaries, format, format_type, digits + nv, pal, na_colour, alpha, include_alpha, format_type, n_summaries, format, digits ); } } @@ -356,13 +365,13 @@ namespace api { //Rcpp::NumericVector nv = Rcpp::as< Rcpp::NumericVector >( x ); Rcpp::NumericVector nv = Rcpp::clone(x); return colourvalues::colours_rgb::colour_value_rgb( - nv, pal, na_colour, alpha, include_alpha, n_summaries, format, format_type, digits + nv, pal, na_colour, alpha, include_alpha, format_type, n_summaries, format, digits ); } case VECSXP: { // list Rcpp::List lst = Rcpp::as< Rcpp::List >( x ); //Rcpp::Rcout << "starting list " << std::endl; - return colour_values_rgb( lst, pal, alpha, na_colour, include_alpha, format, format_type, digits, summary, n_summaries ); + return colour_values_rgb( lst, pal, alpha, na_colour, include_alpha, format, digits, summary, n_summaries ); } case LGLSXP: {} // as.character @@ -388,7 +397,6 @@ namespace api { std::string na_colour = "#808080", bool include_alpha = true, bool format = false, - std::string format_type = "numeric", int digits = 2, bool summary = false, int n_summaries = 0 @@ -401,14 +409,14 @@ namespace api { case REALSXP: { Rcpp::NumericMatrix pal = Rcpp::as< Rcpp::NumericMatrix >( palette ); return colour_values_rgb( - x, pal, alpha, na_colour, include_alpha, format, format_type, digits, summary, n_summaries + x, pal, alpha, na_colour, include_alpha, format, digits, summary, n_summaries ); break; } case STRSXP: { Rcpp::StringVector sv = Rcpp::as< Rcpp::StringVector >( palette ); return colour_values_rgb( - x, sv, alpha, na_colour, include_alpha, format, format_type, digits, summary, n_summaries + x, sv, alpha, na_colour, include_alpha, format, digits, summary, n_summaries ); break; } diff --git a/inst/include/colourvalues/colours/colours_hex.hpp b/inst/include/colourvalues/colours/colours_hex.hpp index 2e1fe3a..7814dc5 100644 --- a/inst/include/colourvalues/colours/colours_hex.hpp +++ b/inst/include/colourvalues/colours/colours_hex.hpp @@ -25,9 +25,6 @@ namespace colours_hex { bool& include_alpha ) { - //Rcpp::Rcout << "colour_values_to_hex " << std::endl; - //Rcpp::Rcout << "include_alpha: " << include_alpha << std::endl; - int n = x.size(); double colours = red.size(); @@ -80,14 +77,41 @@ namespace colours_hex { return hex_strings; } + // combining: + // - 'format' + + inline Rcpp::List create_summary_output( + Rcpp::StringVector& full_hex, + Rcpp::StringVector& summary_values, + Rcpp::StringVector& summary_hex + ) { + return Rcpp::List::create( + _["colours"] = full_hex, + _["summary_values"] = summary_values, + _["summary_colours"] = summary_hex + ); + } + + inline Rcpp::List create_summary_output( + Rcpp::StringVector& full_hex, + SEXP& summary_values, + Rcpp::StringVector& summary_hex + ) { + return Rcpp::List::create( + _["colours"] = full_hex, + _["summary_values"] = summary_values, + _["summary_colours"] = summary_hex + ); + } + inline SEXP colour_value_hex( Rcpp::NumericVector& x, Rcpp::NumericMatrix& palette, std::string& na_colour, bool include_alpha, + std::string& format_type, int n_summaries = 0, bool format = false, - std::string format_type = "numeric", int digits = 2 ) { @@ -120,11 +144,8 @@ namespace colours_hex { Rcpp::StringVector summary_hex = colour_values_to_hex( summary, red, green, blue, alpha_summary, alpha_type, na_colour, include_alpha ); // uses full opacity Rcpp::StringVector full_hex = colour_values_to_hex( x, red, green, blue, alpha_full, alpha_type, na_colour, include_alpha ); - return Rcpp::List::create( - _["colours"] = full_hex, - _["summary_values"] = summary_values, - _["summary_colours"] = summary_hex - ); + + return create_summary_output(full_hex, summary_values, summary_hex ); } return colour_values_to_hex( x, red, green, blue, alpha_full, alpha_type, na_colour, include_alpha ); @@ -136,10 +157,11 @@ namespace colours_hex { std::string& na_colour, Rcpp::NumericVector& alpha, bool include_alpha, + std::string& format_type, int n_summaries = 0, bool format = false, - std::string format_type = "numeric", - int digits = 2) { + int digits = 2 + ) { int x_size = x.size(); int alpha_type = colourvalues::alpha::make_alpha_type( alpha.size(), x_size, 0 ); @@ -168,11 +190,7 @@ namespace colours_hex { Rcpp::StringVector summary_hex = colour_values_to_hex(summary, red, green, blue, alpha_summary, alpha_type, na_colour, include_alpha); Rcpp::StringVector full_hex = colour_values_to_hex(x, red, green, blue, alpha_full, alpha_type, na_colour, include_alpha); - return Rcpp::List::create( - _["colours"] = full_hex, - _["summary_values"] = summary_values, - _["summary_colours"] = summary_hex - ); + return create_summary_output(full_hex, summary_values, summary_hex ); } return colour_values_to_hex(x, red, green, blue, alpha_full, alpha_type, na_colour, include_alpha); } @@ -184,9 +202,6 @@ namespace colours_hex { bool include_alpha, bool summary = false) { - //Rcpp::Rcout << "colour_value_hex" << std::endl; - //Rcpp::Rcout << "include_alpha: " << include_alpha << std::endl; - colourvalues::utils::matrix_palette_check( palette ); int alpha_type = colourvalues::alpha::make_alpha_type( 0, x.size(), palette.ncol() ); @@ -210,11 +225,8 @@ namespace colours_hex { Rcpp::StringVector summary_hex = colour_values_to_hex( nv, red, green, blue, alpha_summary, alpha_type, na_colour, include_alpha ); Rcpp::StringVector full_hex = colour_values_to_hex( out_nv, red, green, blue, alpha, alpha_type, na_colour, include_alpha ); - return Rcpp::List::create( - _["colours"] = full_hex, - _["summary_values"] = lvls, - _["summary_colours"] = summary_hex - ); + + return create_summary_output(full_hex, lvls, summary_hex ); } //Rcpp::Rcout << "about to do the conversion - include_alpha: " << include_alpha << std::endl; return colour_values_to_hex( out_nv, red, green, blue, alpha, alpha_type, na_colour, include_alpha ); @@ -228,8 +240,6 @@ namespace colours_hex { bool include_alpha, bool summary = false) { - //Rcpp::Rcout << "inside colours_hex::colour_value_hex" << std::endl; - int x_size = x.size(); int alpha_type = colourvalues::alpha::make_alpha_type( alpha.size(), x_size, 0 ); @@ -246,9 +256,7 @@ namespace colours_hex { Rcpp::NumericVector out_nv = colourvalues::utils::resolve_string_vector( x, lvls ); if ( summary ) { - //Rcpp::Rcout << "making summary " << std::endl; Rcpp::IntegerVector summary_values = Rcpp::seq_len( lvls.length() ); - //Rcpp::Rcout << "summary values " << summary_values << std::endl; Rcpp::NumericVector nv = Rcpp::as< Rcpp::NumericVector >( summary_values ); int x_size = x.size(); @@ -256,15 +264,9 @@ namespace colours_hex { Rcpp::NumericVector alpha_summary( n_alpha_summary, 255.0 ); Rcpp::StringVector summary_hex = colour_values_to_hex( nv, red, green, blue, alpha_summary, alpha_type, na_colour, include_alpha ); - //Rcpp::Rcout << "summary_hex " << summary_hex << std::endl; Rcpp::StringVector full_hex = colour_values_to_hex( out_nv, red, green, blue, alpha_full, alpha_type, na_colour, include_alpha ); - //Rcpp::Rcout << "full_hex: " << full_hex << std::endl; - return Rcpp::List::create( - _["colours"] = full_hex, - _["summary_values"] = lvls, - _["summary_colours"] = summary_hex - ); + return create_summary_output(full_hex, lvls, summary_hex ); } return colour_values_to_hex( out_nv, red, green, blue, alpha_full, alpha_type, na_colour, include_alpha ); diff --git a/inst/include/colourvalues/colours/colours_rgb.hpp b/inst/include/colourvalues/colours/colours_rgb.hpp index 292d0f1..d3145f3 100644 --- a/inst/include/colourvalues/colours/colours_rgb.hpp +++ b/inst/include/colourvalues/colours/colours_rgb.hpp @@ -86,9 +86,9 @@ namespace colours_rgb { Rcpp::NumericMatrix& palette, std::string& na_colour, bool include_alpha, + std::string& format_type, int n_summaries = 0, bool format = false, - std::string format_type = "numeric", int digits = 2 ) { @@ -137,9 +137,9 @@ namespace colours_rgb { std::string& na_colour, Rcpp::NumericVector& alpha, bool include_alpha, + std::string& format_type, int n_summaries = 0, bool format = false, - std::string format_type = "numeric", int digits = 2) { int x_size = x.size(); diff --git a/inst/include/colourvalues/format/format.hpp b/inst/include/colourvalues/format/format.hpp index ca030ef..b8d828d 100644 --- a/inst/include/colourvalues/format/format.hpp +++ b/inst/include/colourvalues/format/format.hpp @@ -4,6 +4,9 @@ #include #include #include + +#include "colourvalues/utils/utils.hpp" + // #include // [[Rcpp::depends(BH)]] @@ -11,6 +14,34 @@ namespace colourvalues { namespace format { + /* + * Determins the type of formatting requried for summary values (legend) + * + */ + inline std::string get_format_type( SEXP x ) { + + std::string format_type; + + Rcpp::CharacterVector cls = colourvalues::utils::getRClass( x ); + + //Rcpp::Rcout << "cls: " << cls << std::endl; + + if( colourvalues::utils::is_in( "Date", cls ) ) { + format_type = "Date"; + } else if ( colourvalues::utils::is_in("POSIXct", cls) ) { + format_type = "POSIXct"; + } else if ( colourvalues::utils::is_in("POSIXlt", cls) ) { + format_type = "POSIXct"; + } else if ( colourvalues::utils::is_in("logical", cls) ) { + format_type = "character"; + } else if ( colourvalues::utils::is_in("character", cls) ) { + format_type = "character"; + } else { + format_type = "numeric"; + } + return format_type; + } + inline Rcpp::StringVector date_to_string( SEXP v, int n ) { int i; @@ -19,7 +50,7 @@ namespace format { for ( i = 0; i < n; i++ ) { Rcpp::Date d = nv[i]; - boost::gregorian::date gd = boost::gregorian::date(d.getYear(), d.getMonth(), d.getDay()); + boost::gregorian::date gd = boost::gregorian::date( d.getYear(), d.getMonth(), d.getDay() ); std::string s = boost::gregorian::to_iso_extended_string( gd ); sv[i] = s.c_str(); } @@ -61,7 +92,10 @@ namespace format { return sv; } - inline SEXP format_summary( SEXP summary_values, std::string format_type, int n_summaries, int digits ) { + inline SEXP format_summary( SEXP summary_values, std::string& format_type, int n_summaries, int digits ) { + + //std::string format_type = get_format_type( summary_values ); + if (format_type == "numeric" ) { return colourvalues::format::numeric_to_string( summary_values, n_summaries, digits); // } else if ( format_type == "integer" ) { diff --git a/inst/include/colourvalues/list/list.hpp b/inst/include/colourvalues/list/list.hpp index 8975c3e..3119ee9 100644 --- a/inst/include/colourvalues/list/list.hpp +++ b/inst/include/colourvalues/list/list.hpp @@ -3,6 +3,7 @@ #define R_COLOURVALUES_LIST #include +#include "colourvalues/format/format.hpp" //#include "colourvalues/colours/colours_hex.hpp" /*colouring a list @@ -57,19 +58,24 @@ namespace list { return 16; } - inline Rcpp::List list_size( const Rcpp::List& lst, int& total_size, int& existing_type ) { + inline Rcpp::List list_size( const Rcpp::List& lst, int& total_size, int& existing_type, std::string& existing_format ) { std::size_t n = lst.size(); Rcpp::List res( n ); // create a list to store the size corresponding to each list element std::size_t i; for( i = 0; i < n; i++ ) { switch( TYPEOF( lst[i] ) ) { case VECSXP: { - res[ i ] = list_size( lst[i], total_size, existing_type ); + res[ i ] = list_size( lst[i], total_size, existing_type, existing_format ); break; } default: { int n_elements = Rf_length( lst[i] ); int new_type = TYPEOF( lst[i] ); + std::string new_format = colourvalues::format::get_format_type( lst[i] ); + //Rcpp::Rcout << "format_type : " << format_type << std::endl; + if( new_format != existing_format ) { + existing_format = "character"; + } existing_type = vector_type( new_type, existing_type ); res[i] = n_elements; total_size += n_elements; @@ -161,7 +167,7 @@ namespace list { /* * @param lst - the original input list * @param lst_sizes - the dimensions of the list - * @param colorus - vector of values which will go into colour_values_hex() + * @param colours - vector of values which will go into colour_values_hex() */ inline void unlist_list( const Rcpp::List& lst, const Rcpp::List& lst_sizes, diff --git a/inst/include/colourvalues/utils/utils.hpp b/inst/include/colourvalues/utils/utils.hpp index f4efbf0..dbadd79 100644 --- a/inst/include/colourvalues/utils/utils.hpp +++ b/inst/include/colourvalues/utils/utils.hpp @@ -6,6 +6,47 @@ namespace colourvalues { namespace utils { + inline bool is_in( const char* x, Rcpp::CharacterVector v ) { + int n = v.size(); + int i; + for( i = 0; i < n; i++ ) { + if( v[i] == x ) { + return true; + } + } + return false; + } + + template < int RTYPE > + inline Rcpp::CharacterVector rClass( Rcpp::Vector< RTYPE > v ) { + if( Rf_isNull( v.attr("class")) ) { + return ""; + } + return v.attr("class"); + } + + inline Rcpp::CharacterVector getRClass( SEXP obj ) { + + switch( TYPEOF( obj ) ) { + case REALSXP: + return rClass< REALSXP >( obj ); + //return "numeric"; + case VECSXP: + return rClass< VECSXP >( obj ); + //return "character"; + case INTSXP: + return rClass< INTSXP >( obj ); + //return "numeric"; + case LGLSXP: + //return rClass< LGLSXP >( obj ); + return "logical"; + case STRSXP: + //return rClass< STRSXP >( obj ); + return "character"; + } + return ""; + } + inline void matrix_palette_check( Rcpp::NumericMatrix& pal ) { if( pal.nrow() < 5 ) { Rcpp::stop("Matrix palettes must have at least 5 rows"); diff --git a/src/ColourValues.cpp b/src/ColourValues.cpp index c2ab4a5..7429e42 100644 --- a/src/ColourValues.cpp +++ b/src/ColourValues.cpp @@ -1,8 +1,6 @@ #include -#include "colourvalues/api/api_hex.hpp" -#include "colourvalues/api/api_rgb.hpp" - +#include "colourvalues/api/api.hpp" // [[Rcpp::export]] SEXP rcpp_colour_values_hex( @@ -12,7 +10,6 @@ SEXP rcpp_colour_values_hex( std::string na_colour = "#808080", bool include_alpha = true, bool format = false, - std::string format_type = "numeric", int digits = 2, bool summary = false, int n_summaries = 0 @@ -23,7 +20,7 @@ SEXP rcpp_colour_values_hex( //Rcpp::Rcout << "summary " << summary << std::endl; return colourvalues::api::colour_values_hex( - x, palette, alpha, na_colour, include_alpha, format, format_type, digits, summary, n_summaries + x, palette, alpha, na_colour, include_alpha, format, digits, summary, n_summaries ); } @@ -36,14 +33,13 @@ SEXP rcpp_colour_values_rgb( std::string na_colour = "#808080", bool include_alpha = true, bool format = false, - std::string format_type = "numeric", int digits = 2, bool summary = false, int n_summaries = 0 ) { return colourvalues::api::colour_values_rgb( - x, palette, alpha, na_colour, include_alpha, format, format_type, digits, summary, n_summaries + x, palette, alpha, na_colour, include_alpha, format, digits, summary, n_summaries ); } diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index 2b9d8b6..217c965 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -6,8 +6,8 @@ using namespace Rcpp; // rcpp_colour_values_hex -SEXP rcpp_colour_values_hex(SEXP x, SEXP palette, Rcpp::NumericVector& alpha, std::string na_colour, bool include_alpha, bool format, std::string format_type, int digits, bool summary, int n_summaries); -RcppExport SEXP _colourvalues_rcpp_colour_values_hex(SEXP xSEXP, SEXP paletteSEXP, SEXP alphaSEXP, SEXP na_colourSEXP, SEXP include_alphaSEXP, SEXP formatSEXP, SEXP format_typeSEXP, SEXP digitsSEXP, SEXP summarySEXP, SEXP n_summariesSEXP) { +SEXP rcpp_colour_values_hex(SEXP x, SEXP palette, Rcpp::NumericVector& alpha, std::string na_colour, bool include_alpha, bool format, int digits, bool summary, int n_summaries); +RcppExport SEXP _colourvalues_rcpp_colour_values_hex(SEXP xSEXP, SEXP paletteSEXP, SEXP alphaSEXP, SEXP na_colourSEXP, SEXP include_alphaSEXP, SEXP formatSEXP, SEXP digitsSEXP, SEXP summarySEXP, SEXP n_summariesSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::RNGScope rcpp_rngScope_gen; @@ -17,17 +17,16 @@ BEGIN_RCPP Rcpp::traits::input_parameter< std::string >::type na_colour(na_colourSEXP); Rcpp::traits::input_parameter< bool >::type include_alpha(include_alphaSEXP); Rcpp::traits::input_parameter< bool >::type format(formatSEXP); - Rcpp::traits::input_parameter< std::string >::type format_type(format_typeSEXP); Rcpp::traits::input_parameter< int >::type digits(digitsSEXP); Rcpp::traits::input_parameter< bool >::type summary(summarySEXP); Rcpp::traits::input_parameter< int >::type n_summaries(n_summariesSEXP); - rcpp_result_gen = Rcpp::wrap(rcpp_colour_values_hex(x, palette, alpha, na_colour, include_alpha, format, format_type, digits, summary, n_summaries)); + rcpp_result_gen = Rcpp::wrap(rcpp_colour_values_hex(x, palette, alpha, na_colour, include_alpha, format, digits, summary, n_summaries)); return rcpp_result_gen; END_RCPP } // rcpp_colour_values_rgb -SEXP rcpp_colour_values_rgb(SEXP x, SEXP palette, Rcpp::NumericVector& alpha, std::string na_colour, bool include_alpha, bool format, std::string format_type, int digits, bool summary, int n_summaries); -RcppExport SEXP _colourvalues_rcpp_colour_values_rgb(SEXP xSEXP, SEXP paletteSEXP, SEXP alphaSEXP, SEXP na_colourSEXP, SEXP include_alphaSEXP, SEXP formatSEXP, SEXP format_typeSEXP, SEXP digitsSEXP, SEXP summarySEXP, SEXP n_summariesSEXP) { +SEXP rcpp_colour_values_rgb(SEXP x, SEXP palette, Rcpp::NumericVector& alpha, std::string na_colour, bool include_alpha, bool format, int digits, bool summary, int n_summaries); +RcppExport SEXP _colourvalues_rcpp_colour_values_rgb(SEXP xSEXP, SEXP paletteSEXP, SEXP alphaSEXP, SEXP na_colourSEXP, SEXP include_alphaSEXP, SEXP formatSEXP, SEXP digitsSEXP, SEXP summarySEXP, SEXP n_summariesSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::RNGScope rcpp_rngScope_gen; @@ -37,11 +36,10 @@ BEGIN_RCPP Rcpp::traits::input_parameter< std::string >::type na_colour(na_colourSEXP); Rcpp::traits::input_parameter< bool >::type include_alpha(include_alphaSEXP); Rcpp::traits::input_parameter< bool >::type format(formatSEXP); - Rcpp::traits::input_parameter< std::string >::type format_type(format_typeSEXP); Rcpp::traits::input_parameter< int >::type digits(digitsSEXP); Rcpp::traits::input_parameter< bool >::type summary(summarySEXP); Rcpp::traits::input_parameter< int >::type n_summaries(n_summariesSEXP); - rcpp_result_gen = Rcpp::wrap(rcpp_colour_values_rgb(x, palette, alpha, na_colour, include_alpha, format, format_type, digits, summary, n_summaries)); + rcpp_result_gen = Rcpp::wrap(rcpp_colour_values_rgb(x, palette, alpha, na_colour, include_alpha, format, digits, summary, n_summaries)); return rcpp_result_gen; END_RCPP } @@ -78,6 +76,28 @@ BEGIN_RCPP return rcpp_result_gen; END_RCPP } +// rcpp_get_format_type +std::string rcpp_get_format_type(SEXP palette_type); +RcppExport SEXP _colourvalues_rcpp_get_format_type(SEXP palette_typeSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< SEXP >::type palette_type(palette_typeSEXP); + rcpp_result_gen = Rcpp::wrap(rcpp_get_format_type(palette_type)); + return rcpp_result_gen; +END_RCPP +} +// rcpp_get_r_class +Rcpp::CharacterVector rcpp_get_r_class(SEXP obj); +RcppExport SEXP _colourvalues_rcpp_get_r_class(SEXP objSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< SEXP >::type obj(objSEXP); + rcpp_result_gen = Rcpp::wrap(rcpp_get_r_class(obj)); + return rcpp_result_gen; +END_RCPP +} // rcpp_vector_type int rcpp_vector_type(int new_type, int existing_type); RcppExport SEXP _colourvalues_rcpp_vector_type(SEXP new_typeSEXP, SEXP existing_typeSEXP) { @@ -91,15 +111,16 @@ BEGIN_RCPP END_RCPP } // rcpp_list_size -Rcpp::List rcpp_list_size(const Rcpp::List& lst, int& total_size, int& existing_type); -RcppExport SEXP _colourvalues_rcpp_list_size(SEXP lstSEXP, SEXP total_sizeSEXP, SEXP existing_typeSEXP) { +Rcpp::List rcpp_list_size(const Rcpp::List& lst, int& total_size, int& existing_type, std::string& format_type); +RcppExport SEXP _colourvalues_rcpp_list_size(SEXP lstSEXP, SEXP total_sizeSEXP, SEXP existing_typeSEXP, SEXP format_typeSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::RNGScope rcpp_rngScope_gen; Rcpp::traits::input_parameter< const Rcpp::List& >::type lst(lstSEXP); Rcpp::traits::input_parameter< int& >::type total_size(total_sizeSEXP); Rcpp::traits::input_parameter< int& >::type existing_type(existing_typeSEXP); - rcpp_result_gen = Rcpp::wrap(rcpp_list_size(lst, total_size, existing_type)); + Rcpp::traits::input_parameter< std::string& >::type format_type(format_typeSEXP); + rcpp_result_gen = Rcpp::wrap(rcpp_list_size(lst, total_size, existing_type, format_type)); return rcpp_result_gen; END_RCPP } @@ -638,13 +659,15 @@ END_RCPP } static const R_CallMethodDef CallEntries[] = { - {"_colourvalues_rcpp_colour_values_hex", (DL_FUNC) &_colourvalues_rcpp_colour_values_hex, 10}, - {"_colourvalues_rcpp_colour_values_rgb", (DL_FUNC) &_colourvalues_rcpp_colour_values_rgb, 10}, + {"_colourvalues_rcpp_colour_values_hex", (DL_FUNC) &_colourvalues_rcpp_colour_values_hex, 9}, + {"_colourvalues_rcpp_colour_values_rgb", (DL_FUNC) &_colourvalues_rcpp_colour_values_rgb, 9}, {"_colourvalues_rcpp_convert_hex_to_rgb", (DL_FUNC) &_colourvalues_rcpp_convert_hex_to_rgb, 1}, {"_colourvalues_rcpp_convert_rgb_mat_to_hex", (DL_FUNC) &_colourvalues_rcpp_convert_rgb_mat_to_hex, 1}, {"_colourvalues_rcpp_convert_rgb_vec_to_hex", (DL_FUNC) &_colourvalues_rcpp_convert_rgb_vec_to_hex, 1}, + {"_colourvalues_rcpp_get_format_type", (DL_FUNC) &_colourvalues_rcpp_get_format_type, 1}, + {"_colourvalues_rcpp_get_r_class", (DL_FUNC) &_colourvalues_rcpp_get_r_class, 1}, {"_colourvalues_rcpp_vector_type", (DL_FUNC) &_colourvalues_rcpp_vector_type, 2}, - {"_colourvalues_rcpp_list_size", (DL_FUNC) &_colourvalues_rcpp_list_size, 3}, + {"_colourvalues_rcpp_list_size", (DL_FUNC) &_colourvalues_rcpp_list_size, 4}, {"_colourvalues_rcpp_refil_list", (DL_FUNC) &_colourvalues_rcpp_refil_list, 3}, {"_colourvalues_rcpp_viridis", (DL_FUNC) &_colourvalues_rcpp_viridis, 0}, {"_colourvalues_rcpp_inferno", (DL_FUNC) &_colourvalues_rcpp_inferno, 0}, diff --git a/src/format.cpp b/src/format.cpp new file mode 100644 index 0000000..52f68f8 --- /dev/null +++ b/src/format.cpp @@ -0,0 +1,16 @@ +#include +using namespace Rcpp; + +#include "colourvalues/format/format.hpp" +#include "colourvalues/utils/utils.hpp" + + +// [[Rcpp::export]] +std::string rcpp_get_format_type( SEXP palette_type ) { + return colourvalues::format::get_format_type( palette_type ); +} + +// [[Rcpp::export]] +Rcpp::CharacterVector rcpp_get_r_class( SEXP obj ) { + return colourvalues::utils::getRClass( obj ); +} diff --git a/src/list.cpp b/src/list.cpp index 0a4b0d8..f39b4c7 100644 --- a/src/list.cpp +++ b/src/list.cpp @@ -14,8 +14,8 @@ int rcpp_vector_type( int new_type, int existing_type ) { * Anything outside those numbers is converted to STRSXP */ // [[Rcpp::export]] -Rcpp::List rcpp_list_size( const Rcpp::List& lst, int& total_size, int& existing_type ) { - return colourvalues::list::list_size( lst, total_size, existing_type ); +Rcpp::List rcpp_list_size( const Rcpp::List& lst, int& total_size, int& existing_type, std::string& format_type ) { + return colourvalues::list::list_size( lst, total_size, existing_type, format_type ); } diff --git a/tests/testthat/test-colour_values_hex.R b/tests/testthat/test-colour_values_hex.R index a78100f..32ff29b 100644 --- a/tests/testthat/test-colour_values_hex.R +++ b/tests/testthat/test-colour_values_hex.R @@ -169,7 +169,7 @@ test_that("original vectors returned",{ y <- 1L:10L invisible( colour_values(x) ) expect_true( all(x == y) ) - invisible( colour_values_rgb(x)) + invisible( colour_values_rgb(x) ) expect_true( all(x == y)) set.seed(1) diff --git a/tests/testthat/test-format.R b/tests/testthat/test-format.R new file mode 100644 index 0000000..a011496 --- /dev/null +++ b/tests/testthat/test-format.R @@ -0,0 +1,20 @@ +context("format") + +test_that("correct format_type is determined",{ + + expect_equal( colourvalues:::rcpp_get_format_type("abc"), "character") + expect_equal( colourvalues:::rcpp_get_format_type(T), "character") + expect_equal( colourvalues:::rcpp_get_format_type(factor(1)), "numeric") + expect_equal( colourvalues:::rcpp_get_format_type(as.Date("2018-01-01")), "Date") + expect_equal( colourvalues:::rcpp_get_format_type(as.POSIXct("2018-01-01")), "POSIXct") + expect_equal( colourvalues:::rcpp_get_format_type(as.POSIXlt("2018-01-01")), "POSIXct") + expect_equal( colourvalues:::rcpp_get_format_type(123), "numeric") + + ## TODO + ## - does it make sense for a list to have a format type, should it only work on vectors? + #expect_equal( colourvalues:::rcpp_get_format_type( list("a") ), "character") + #colourvalues:::rcpp_get_r_class( list("a") ) + + + }) + diff --git a/tests/testthat/test-lists.R b/tests/testthat/test-lists.R index 7866b87..a7fe203 100644 --- a/tests/testthat/test-lists.R +++ b/tests/testthat/test-lists.R @@ -134,6 +134,15 @@ test_that("list produces summary",{ expect_true( all( res$summary_values == c("1.00000","2.25000","3.50000","4.75000","6.00000") ) ) expect_warning( colour_values( list(letters), n_summaries = 4) ) + + ## Mix + l <- list( x = 1, y = "a") + res <- colour_values( l, summary = T ) + expect_true( all( res$summary_values == c("1","a") ) ) + + l <- list( x = as.Date("2018-01-01"), y = as.POSIXct("2018-01-01", tz = "GMT")) + res <- colour_values( l, n_summaries = 3 ) + expect_true( all( res$summary_values == c("17532","1514764800"))) })