From pburns at pburns.seanet.com  Wed Apr 24 11:29:43 2013
From: pburns at pburns.seanet.com (Patrick Burns)
Date: Wed, 24 Apr 2013 10:29:43 +0100
Subject: [Rd] as.name and namespaces
In-Reply-To: <5176EC94.3060607@gmail.com>
References: <5176C393.9090802@pburns.seanet.com>
	<3F6D53EB-1C02-42B4-AD2D-085D9E9E94C3@gmail.com>
	<5176E65B.50500@pburns.seanet.com> <5176EC94.3060607@gmail.com>
Message-ID: <5177A607.10706@pburns.seanet.com>

Here is an example problem:

 > mycall <- expression(lm(Y ~ x))[[1]]
 > mycall
lm(Y ~ x)
 > newname <- "stats::lm"

 > desiredResult
stats::lm(Y ~ x)

I've solved the problem in the kludgy way of
deparsing, fixing the string and then parsing.

I like Duncan's third method, but it seems like
it assumes the solution.  Moving functions around
is unappetizing for my use -- this is for testing
and keeping things as faithful to real use is a
good thing.

Pat


On 23/04/2013 21:18, Duncan Murdoch wrote:
> On 13-04-23 3:51 PM, Patrick Burns wrote:
>> Okay, that's a good reason why it shouldn't.
>>
>> Why it should is that I want to substitute
>> the first element of a call to be a function
>> including the namespace.
>
> Three ways:
>
> 1.  Assign the function from the namespace locally, then call the local
> one.
> 2.  Import the function in your NAMESPACE (if you know the name in
> advance).
> 3.  Construct an expression involving ::, and substitute that in.
>
> For example:
>
> substitute(foo(x), list(foo=quote(baz::bar)))
>
> Duncan Murdoch
>
>>
>> Pat
>>
>>
>> On 23/04/2013 18:32, peter dalgaard wrote:
>>>
>>> On Apr 23, 2013, at 19:23 , Patrick Burns wrote:
>>>
>>>> 'as.name' doesn't recognize a name with
>>>> its namespace extension as a name:
>>>>
>>>>> as.name("lm")
>>>> lm
>>>>> as.name("stats::lm")
>>>> `stats::lm`
>>>>> as.name("stats:::lm")
>>>> `stats:::lm`
>>>>
>>>>
>>>> Is there a reason why it shouldn't?
>>>
>>> Any reason why it should? :: and ::: are operators. foo$bar is not
>>> the same as `foo$bar` either.
>>>
>>
>
>

-- 
Patrick Burns
pburns at pburns.seanet.com
twitter: @burnsstat @portfolioprobe
http://www.portfolioprobe.com/blog
http://www.burns-stat.com
(home of:
  'Impatient R'
  'The R Inferno'
  'Tao Te Programming')



[Rd] as.name and namespaces
Patrick Burns