[Rd] S4SXP type vs S4 object bit?
I'm trying to understand the R internals a bit better and reading over the documentation. I see that there is a bit related to whether an object is S4 (S4_OBJECT_MASK), and also the object type S4SXP (25). The documentation makes clear that these two things aren't the same. But in practice, will the S4-bit and object type ever disagree for S4 objects? I know that one can set the bit manually in C; are there any practical applications for doing so? Thank you Travers [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] S4SXP type vs S4 object bit?
Yes, any object of a class that derives from a basic type, like an atomic vector for example, will be of the basic SEXP type, with the S4 bit set. This means that a class can extend "integer" and objects of that class can be treated as any ordinary integer vector. S4SXP is only for objects that do not derive from another basic type. Michael On Tue, Oct 22, 2019 at 1:28 AM Travers Ching wrote: > > I'm trying to understand the R internals a bit better and reading over the > documentation. > > I see that there is a bit related to whether an object is S4 > (S4_OBJECT_MASK), and also the object type S4SXP (25). The documentation > makes clear that these two things aren't the same. > > But in practice, will the S4-bit and object type ever disagree for S4 > objects? I know that one can set the bit manually in C; are there any > practical applications for doing so? > > Thank you > Travers > > [[alternative HTML version deleted]] > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel -- Michael Lawrence Scientist, Bioinformatics and Computational Biology Genentech, A Member of the Roche Group Office +1 (650) 225-7760 micha...@gene.com Join Genentech on LinkedIn | Twitter | Facebook | Instagram | YouTube __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] S4SXP type vs S4 object bit?
Hi Travers, Just an additional remarks to Michael's answer, if your S4 class inherits from R's basic types, say integer, the resulting object will be an INTSXP. If your S4 class does not inherit from any class, it will be an S4SXP. You can think about this question from the object-oriented framework: If one class inherits the integer class, what should R do to make all the integer related functions compatible with the new class at C level? Best, Jiefei On Tue, Oct 22, 2019 at 4:28 AM Travers Ching wrote: > I'm trying to understand the R internals a bit better and reading over the > documentation. > > I see that there is a bit related to whether an object is S4 > (S4_OBJECT_MASK), and also the object type S4SXP (25). The documentation > makes clear that these two things aren't the same. > > But in practice, will the S4-bit and object type ever disagree for S4 > objects? I know that one can set the bit manually in C; are there any > practical applications for doing so? > > Thank you > Travers > > [[alternative HTML version deleted]] > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] S4SXP type vs S4 object bit?
Thanks you Jiefei and Michael! Travers On Tue, Oct 22, 2019 at 8:14 AM Wang Jiefei wrote: > Hi Travers, > > Just an additional remarks to Michael's answer, if your S4 class inherits > from R's basic types, say integer, the resulting object will be an INTSXP. > If your S4 class does not inherit from any class, it will be an S4SXP. You > can think about this question from the object-oriented framework: If one > class inherits the integer class, what should R do to make all the integer > related functions compatible with the new class at C level? > > Best, > Jiefei > > On Tue, Oct 22, 2019 at 4:28 AM Travers Ching wrote: > >> I'm trying to understand the R internals a bit better and reading over the >> documentation. >> >> I see that there is a bit related to whether an object is S4 >> (S4_OBJECT_MASK), and also the object type S4SXP (25). The documentation >> makes clear that these two things aren't the same. >> >> But in practice, will the S4-bit and object type ever disagree for S4 >> objects? I know that one can set the bit manually in C; are there any >> practical applications for doing so? >> >> Thank you >> Travers >> >> [[alternative HTML version deleted]] >> >> __ >> R-devel@r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-devel >> > [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] Unexpected behavior when using macro to loop over vector
Hi all, I found an unexpected behavior when I was trying to use the macro defined in "R_ext/Itermacros.h" to loop over an atomic vector. Here is a minimum example: C++ code ``` #include "R_ext/Itermacros.h" #define GET_REGION_BUFSIZE 2 //Redefine the macro since C++ is not happy with the implicit type conversion #define ITERATE_BY_REGION_PARTIAL(sx, px, idx, nb, etype, vtype, \ strt, nfull, expr) do { \ const etype *px = (etype*)DATAPTR_OR_NULL(sx); \ if (px != NULL) { \ R_xlen_t __ibr_n__ = strt + nfull; \ R_xlen_t nb = __ibr_n__; \ for (R_xlen_t idx = strt; idx < __ibr_n__; idx += nb) { \ expr \ } \ } \ else ITERATE_BY_REGION_PARTIAL0(sx, px, idx, nb, etype, vtype, \ strt, nfull, expr); \ } while (0) // [[Rcpp::export]] void C_testPrint(SEXP x) { ITERATE_BY_REGION_PARTIAL(x, ptr, idx, nbatch, double, REAL, 1, 4, { for (R_xlen_t i = 0; i < nbatch; i++) Rprintf("idx: %lld, i: %lld, ptr:%f\n", idx, i, ptr[i]); }); } ``` The function C_testPrint loops over its argument x and prints out one value of x at each loop. The loop starts from the second element and ends in the fifth element of x. I also redefine the buffer size to see the effect of it. Here is my R code: R code ``` > C_testPrint(as.numeric(1:10)) idx: 1, i: 0, ptr:2.00 idx: 1, i: 1, ptr:3.00 idx: 3, i: 0, ptr:4.00 idx: 3, i: 1, ptr:5.00 > C_testPrint(c(1,2,3,4,5,6,7,8,9,10)) idx: 1, i: 0, ptr:1.00 idx: 1, i: 1, ptr:2.00 idx: 1, i: 2, ptr:3.00 idx: 1, i: 3, ptr:4.00 idx: 1, i: 4, ptr:5.00 ``` There are two problems in the outputs: 1. The numbers of lines are different 2. The starting indices are not the same. >From my understanding, the first output seems correct to me. The second is not unexpected. I believe the differences are due to the accessibility of the data pointer. Did I misunderstand and misuse the macro? Or is it a bug in R? Here is my session info. My R is a bit outdated but the macro seems unchanged in R 4.0. Thanks ``` > sessionInfo() R Under development (unstable) (2019-08-22 r77060) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows >= 8 x64 (build 9200) ``` [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel