[Rcpp-devel] Incorrect result of NA_REAL and NA_INTEGER
Akhila Chowdary Kolla
akhilakollasrinu424jf at gmail.com
Wed Sep 9 01:55:13 CEST 2020
Hello Bill,
Thanks for the information.When I run the specified commands on the
generated mat.RDs file I get the following:
> file.exists("mat.RDs")
[1] TRUE
> file.size("mat.RDs")
[1] 37
> readBin("mat.RDs", what=raw(), n=20)
[1] 8b e0 62 60 60 60 66 60 66 03 62 56 20 93 81 35 34 c4 4d d7
> readBin(gzfile("mat.RDs","rb"), what=raw(), n=20)
[1] 8b e0 62 60 60 60 66 60 66 03 62 56 20 93 81 35 34 c4 4d d7
>
I used compress=TRUE in saveRDS function call while generating the
file(mat.RDs) it still gives me the result starting with 8b instead of 1f.
Please Advice.
Thanks,
Akhila Chowdary
On Tue, Sep 8, 2020 at 4:29 PM Bill Dunlap <williamwdunlap at gmail.com> wrote:
> a) Does the 'rds' file you tried to make exist?
> file.exists("mat.RDs")
> b) It should have a more than c. 10 bytes in it.
> file.size("mat.RDs")
> c) It should start with the with the 2-byte "magic number" for gzip files,
> 1f 8b:
> readBin("mat.RDs", what=raw(), n=20)
> [1] 1f 8b 08 00 00 00 00 00 00 06 8b e0 62 60 60 60 66 60 61 60
> d) When unzipped it should start with the rds header, 58 0a, then version
> 3,
> then the version of R that made it (4 0 2), and the earliest version of R
> that can
> read it (3 5 0):
> readBin(gzfile("mat.RDs","rb"), what=raw(), n=20)
> [1] 58 0a 00 00 00 03 00 04 00 02 00 03 05 00 00 00 00 06 43 50
>
> Failure of d will give you the "unknown input format" error'. Failure
> of c means you used compress=FALSE in the call to saveRDS.
>
> -Bill
>
> On Tue, Sep 8, 2020 at 4:11 PM Dirk Eddelbuettel <edd at debian.org> wrote:
>
>>
>> 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
>> _______________________________________________
>> Rcpp-devel mailing list
>> Rcpp-devel at lists.r-forge.r-project.org
>> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20200908/87f73833/attachment.html>
More information about the Rcpp-devel
mailing list