Opened 8 years ago

Last modified 5 years ago

#12948 new enhancement

Document really well how to get the R interface to accept keywords

Reported by: kcrisman Owned by: was
Priority: major Milestone: sage-6.4
Component: interfaces Keywords: r-project
Cc: Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Description

One of many example of how this doesn't work. In order to do

    subset(cars, select = speed)

you need to do the not very Sage-like

    sage: r.new("subset(cars,select=speed)")

because otherwise you have

sage: r.subset("cars",select='speed')
Error: object 'sage2' not found
sage: r.subset(r.cars,select='speed')
['sage2']

See this ask.sagemath question for this example, but this happens a lot with keywords, you almost have to just substitute them in by hand.

Diagnosis: in R interface we have

    def function_call(self, function, args=None, kwds=None):
        args, kwds = self._convert_args_kwds(args, kwds)
        self._check_valid_function_name(function)
        return self.new("%s(%s)"%(function, ",".join([s.name() for s in args] +
                                                     [self._sage_to_r_name(key)+'='+kwds[key].name() for key in kwds ] )))

and notice that interface._convert_arg_kwds "Converts all of the args and kwds to be elements of this interface.", which in this case is inappropriate.

I'm not sure what the best fix is, but at any rate changing to

#        args, kwds = self._convert_args_kwds(args, kwds)
        self._check_valid_function_name(function)
        return self.new("%s(%s)"%(function, ",".join([s for s in args] +
                                                     [self._sage_to_r_name(key)+'='+kwds[key] for key in kwds ] )))

allows

sage: r.subset("cars",select='speed')

to work, though it can't be a final solution since it breaks

sage: r.subset(r.cars,select='speed')

which should also work, in principle.

Change History (7)

comment:1 follow-up: Changed 8 years ago by mhansen

The current way to do this is

r('cars').subset(select='"speed"')

You need to use double quotes for strings when passing them into the interface. Otherwise, speed will be treated as an "identifier" rather than a string. There were some thought on changing this, but it is backwards incompatible and should be made consistent across interfaces.

Additionally,

r('cars')['"speed"']

also works.

Last edited 8 years ago by mhansen (previous) (diff)

comment:2 in reply to: ↑ 1 Changed 8 years ago by kcrisman

You need to use double quotes for strings when passing them into the interface. Otherwise, speed will be treated as an "identifier" rather than a string. There were some thought on changing this, but it is backwards incompatible and should be made consistent across interfaces.

Hmm, thanks. Somehow I figured this out a long time ago but completely forgot - at the very least we need better doc! So is this true for all the interfaces?

Also, you should post these answers on the ask.sagemath.org question. Since they're yours :-)

Additionally,

r('cars')['"speed"']

also works.

Oh my! That is somewhat surprising, but great.

comment:3 Changed 8 years ago by kcrisman

  • Summary changed from Get R interface to properly accept keywords to Document really well how to get the R interface to accept keywords
  • Type changed from defect to enhancement

I now realize from this ask.sagemath.org post that there is http://wiki.sagemath.org/R, which I never knew about, and which has this. In addition, it refers to #2963, which seems very close to a dup of this ticket.

So I am repurposing this ticket to much better documentation of this issue, including the examples from that wiki page, and then #2963 can stay for possible changing this situation.

comment:4 Changed 6 years ago by jdemeyer

  • Milestone changed from sage-5.11 to sage-5.12

comment:5 Changed 6 years ago by vbraun_spam

  • Milestone changed from sage-6.1 to sage-6.2

comment:6 Changed 6 years ago by vbraun_spam

  • Milestone changed from sage-6.2 to sage-6.3

comment:7 Changed 5 years ago by vbraun_spam

  • Milestone changed from sage-6.3 to sage-6.4
Note: See TracTickets for help on using tickets.