Re: [Rd] Puzzled about a new method for "[".

2019-11-05 Thread Iñaki Ucar
You can try for testing with a column of class errors, from the package
'errors'. The attributes depend on the content in the way Hadley pointed
out.

Iñaki

El lun., 4 nov. 2019 23:19, Rolf Turner  escribió:

> On 5/11/19 10:54 AM, Duncan Murdoch wrote:
> > On 04/11/2019 4:40 p.m., Pages, Herve wrote:
> >> Hi Rolf,
> >>
> >> On 11/4/19 12:28, Rolf Turner wrote:
> >>>
> >>> On 5/11/19 3:41 AM, Hadley Wickham wrote:
> >>>
>  For what it's worth, I don't think this strategy can work in general,
>  because a class might have attributes that depend on its data/contents
>  (e.g.
> 
> https://urldefense.proofpoint.com/v2/url?u=https-3A__vctrs.r-2Dlib.org_articles_s3-2Dvector.html-23cached-2Dsum&d=DwICAg&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=pqLHzHYLUeyQnxA1K_XhSbKJql6r9wK1RXcDG2tuZ6s&s=kPUlNqBPr6j4lPvqkIj8w2Gl5JYGLqJ7ws6wH5tpGcw&e=
> 
>  ). I
>  don't think these are particularly common in practice, but it's
>  dangerous to assume that you can restore a class simply by restoring
>  its attributes after subsetting.
> >>>
> >>>
> >>> You're probably right that there are lurking perils in general, but I
> am
> >>> not trying to "restore a class".  I simply want to *retain* attributes
> >>> of columns in a data frame.
> >>>
> >>> * I have a data frame X
> >>> * I attach attributes to certain of its columns;
> >>>attr(X$melvin,"clyde") <- 42
> >>> (I *don't* change the class of X$melvin.)
> >>> * I form a subset of X:
> >>>   Y <- X[1:100,3:10]
> >>> * given that "melvin" is amongst columns 3 through 10 of X,
> >>>   I want Y$melvin to retain the attribute "clyde", i.e. I
> >>>   want attr(Y$melvin,"clyde") to return 42
> >>>
> >>> There is almost surely a better approach than the one that I've chosen
> >>> (isn't there always?) but it seems to work, and the perils certainly
> are
> >>> not immediately apparent to me.
> >>
> >> Maybe you've solved the problem for the columns that contain your
> >> objects but now you've introduced a potential problem for columns that
> >> contain objects with attributes whose value depend on content.
> >>
> >> Hadley it right that restoring the original attributes of a vector (list
> >> or atomic) after subsetting is unsafe.
> >
> > Right, so Rolf should only restore attributes that are ones he added in
> > the first place.  Unknown attributes should be left alone.
>
> Fair point.  And that gets fiddly.  I guess I'm going to have to rethink
> my strategy.
>
> cheers,
>
> Rolf
>
> --
> Honorary Research Fellow
> Department of Statistics
> University of Auckland
> Phone: +64-9-373-7599 ext. 88276
>
> __
> 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] Questions on the R C API

2019-11-05 Thread Gabriel Becker
Hi Martin and Jiefei,

A quick note, I'll try to respond more completely later.


> It is a primitive but I don't understand how it manage to do that.
>

Primitives and Internals are allowed to be, but usually aren't, SPECIALSXPs
rather than BUILTINSXPs. SPECIALSXPs receive their arguments unevaluated
when called from R. See
https://cran.r-project.org/doc/manuals/r-release/R-ints.html#g_t_002eInternal-vs-_002ePrimitive.
This is not possible for functions that are not part of the R sources as
far as I know.

Also the difference between Primitives and internals isn't evaluation of
arguments (thats special vs builtin) but rather, whether they behave like a
closure (internal) or not (primitive).

Evaluation behavior for internal and primitive functions is defined within
the function table in names.c, the comment there explains the format.

Best,
~G

>
> Best,
> Morgan
>
>
>
> > Best,
> > Jiefei
> >
> >
> > On Mon, Nov 4, 2019 at 2:41 PM Morgan Morgan 
> > wrote:
> >
> >> Hi All,
> >>
> >> I have some questions regarding the R C API.
> >>
> >> Let's assume I have a function which is defined as follows:
> >>
> >> R file:
> >>
> >> myfunc <- function(a, b, ...) .External(Cfun, a, b, ...)
> >>
> >> C file:
> >>
> >> SEXP Cfun(SEXP args) {
> >>   args = CDR(args);
> >>   SEXP a = CAR(args); args = CDR(args);
> >>   SEXP b = CAR(args); args = CDR(args);
> >>   /* continue to do something with remaining arguments in "..." using
> the
> >> same logic as above*/
> >>
> >>   return R_NilValue;
> >> }
> >>
> >> 1/ Let's suppose that in my c function I change the value of a inside
> the
> >> function but I want to reset it to what it was when I did SEXP a =
> >> CAR(args); . How can I do that?
> >>
> >> 2/Is there a method to set "args" at a specific position so I can
> access a
> >> specific value of my choice? If yes, do you have an simple example?
> >>
> >> 3/ Let's suppose now, I call the function in R. Is there a way to avoid
> >> the
> >> function to evaluate its arguments before going to the C call? Do I have
> >> to
> >> do it at the R level or can it be done at the C level?
> >>
> >> Thank you very much in advance.
> >> Best regards
> >> Morgan
> >>
> >> [[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
>

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Puzzled about a new method for "[".

2019-11-05 Thread Rolf Turner

On 5/11/19 9:37 PM, Iñaki Ucar wrote:
You can try for testing with a column of class errors, from the package 
'errors'. The attributes depend on the content in the way Hadley pointed 
out.


Thanks, but it turns out to be much simpler than that.  There is a very 
easy way to accomplish what I want --- simply give an attribute to the 
data frame, rather than to a certain column of that data frame.  I don't 
know why the hell I didn't do that in the first place!  Duh!!!


Sorry for all the noise that this issue has generated.

cheers,

Rolf

--
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel