[Rcpp-devel] Incorrect result of NA_REAL and NA_INTEGER
Dirk Eddelbuettel
edd at debian.org
Wed Sep 9 01:11:42 CEST 2020
On 8 September 2020 at 16:03, Akhila Chowdary Kolla wrote:
| Thanks a lot for your quick response, Dirk. Instead of printing the vector
| values using std::cout I am planning to serialize those objects using
| saveRDS and trying to retrieve the stored values using readRDS. I am trying
| to make use of the RInside instance to make a call to R functions. Although
| the class variable is recognized as an integer in the below example, when I
| use readRDS it says unknown input format. Is there a method to serialize
| Rcpp objects using RInside from a testharness?
|
| *Example:*
| TEST(deepstate_test,datatype){
| RInside R;
| R["ms"] = 5;
| std::string cmd = "cat(class(ms));saveRDS(ms,\"mtest.Rds\");
| readRDS(\"mtest.Rds\")";
| R.parseEval(cmd);
| }
| *Output:*
| akhila at akhila-VirtualBox:~$ ./newtest
|
|
|
|
|
|
|
| *TRACE: Running: deepstate_test_datatype from newtest.cpp(6)EXTERNAL:
| integerEXTERNAL: �EXTERNAL: Error in readRDS("mtest.Rds") : unknown input
| formatterminate called after throwing an instance of 'std::runtime_error'
| what(): Error evaluating: cat(class(ms));saveRDS(ms,"mtest.Rds");
| readRDS("mtest.Rds")ERROR: Failed: deepstate_test_datatype*
|
| Also, I tried using the below code in my Cpp function. I still get the same
| unknown input format error.
|
| Rcpp::NumericMatrix mat = RcppDeepState_NumericMatrix();
| Environment base("package:base");
| Function saveRDS = base["saveRDS"];
| saveRDS(mat,Named("file","mat.RDs"));
| Function readRDS = base["readRDS"];
| readRDS("mat.RDs");
|
| Any help is appreciated. Thanks in advance
That leaves me a little puzzled. No immediate suggestion.
Restoring an R-written RDS with another R session should always work. Strange.
Dirk
| Regards,
| Akhila Chowdary Kolla
|
| On Fri, Sep 4, 2020 at 3:15 PM Dirk Eddelbuettel <edd at debian.org> wrote:
|
| >
| > On 4 September 2020 at 11:24, Akhila Chowdary Kolla wrote:
| > | Hello Everyone,
| > |
| > | I am trying to use NA_REAL, NA_INTEGER, and R_NaN in my CPP code(doesn't
| > | use R main). When I compile and run my code,* NA_REAL* it gives the value
| > | as *nan* and *NA_INTEGER* gives the value as *-2147483648, and R_NaN as
| > 0*.
| > |
| > | I used the following code(from Rcpp FAQ):
| > | Rcpp::IntegerVector Missing_I() {
| > | Rcpp::IntegerVector v(1);
| > | v[0] = NA_INTEGER; // NA
| > | return v;
| > | }
| > | Rcpp::NumericVector Missing_N() {
| > | Rcpp::NumericVector v(4);
| > | v[0] = R_NegInf; // -Inf
| > | v[1] = NA_REAL; // NA
| > | v[2] = R_PosInf; // Inf
| > | v[3] = R_NaN; // nan
| > | return v;
| > | }
| > |
| > | When I compile the functions using sourceCpp() I get the output as
| > expected:
| > | > sourceCpp("~/R/RcppDeepState/inst/extdata/filesave.cpp")
| > | > Missing_I()
| > | [1] NA
| > | > Missing_N()
| > | [1] -Inf NA Inf NaN
| > |
| > | But when I compile the code using the TestHarness it gives me the
| > following
| > | output:
| > | missing_n values: -inf nan inf 0
| > | missing_i values: -2147483648
| > |
| > | I saved the above functions(Missing_I, Missing_N) in a header file and
| > made
| > | a call to those functions from the testharness:
| > |
| > | TEST(deepstate_test,datatype){
| > | RInside();
| > | Rcpp::IntegerVector missing_i = Missing_I();
| > | std::cout <<"missing_i values: "<< missing_i << std::endl;
| > | Rcpp::NumericVector missing_n = Missing_N();
| > | std::cout <<"missing_n values: "<< missing_n << std::endl;
| > | }
| > |
| > | How can I get the results as expected? Any help is appreciated.
| >
| > Am I understanding you correctly that you would like _the R behaviour_ but
| > in
| > a non-R context (such as a catch or Google gtest harness) ? You can't as
| > easily as R actively adds some extensions. I.e. IEEE 754 defines this for
| > doubles, but "nobody" besides R does it for Int. So you would have to add
| > your print function.
| >
| > Dirk
| >
| > --
| > https://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org
| >
--
https://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org
More information about the Rcpp-devel
mailing list