Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kcrisman opened this issue May 14, 2012 · 3 comments
Open

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

kcrisman opened this issue May 14, 2012 · 3 comments

Comments

@kcrisman
Copy link
Member

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.

Component: interfaces

Keywords: r-project

Issue created by migration from https://trac.sagemath.org/ticket/12948

@mwhansen
Copy link
Contributor

comment:1

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.

@kcrisman
Copy link
Member Author

comment:2

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.

@kcrisman
Copy link
Member Author

comment:3

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.

@kcrisman kcrisman changed the title Get R interface to properly accept keywords Document really well how to get the R interface to accept keywords May 18, 2012
@jdemeyer jdemeyer modified the milestones: sage-5.11, sage-5.12 Aug 13, 2013
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.1, sage-6.2 Jan 30, 2014
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.2, sage-6.3 May 6, 2014
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.3, sage-6.4 Aug 10, 2014
@mkoeppe mkoeppe removed this from the sage-6.4 milestone Dec 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants