[Rcpp-devel] Incorrect result of NA_REAL and NA_INTEGER

Akhila Chowdary Kolla akhilakollasrinu424jf at gmail.com
Wed Sep 9 01:03:51 CEST 2020


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

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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20200908/24e264f4/attachment.html>


More information about the Rcpp-devel mailing list