[R] Error: unexpected '<' in "<" when modifying existing functions
Rui Esteves
ruimaximo at gmail.com
Sat Jan 14 09:58:22 CET 2012
Thank you both.
1) As Duncan said, if I leave <environment: namespace:stats> out, it
will not work since it is using .C and .Fortran functions that kmeans
calls.
I
2) don`t know how to use the as.environment() (I did not understood by
reading the help).
3) Setting environment(kmeansnew) <- environment(stats::kmeans) does
not work as well.
4) Using fix() works, but then I don`t know how to store just the
function in an external file. To use it in another computer, for
example. If I use save(myfunc,"myFile.R", ASCII=TRUE) it doesn't work
when I try to load it again using myfunc=load("myFile.R")
Rui
On Sat, Jan 14, 2012 at 3:22 AM, Duncan Murdoch
<murdoch.duncan at gmail.com> wrote:
> On 12-01-13 8:05 PM, Peter Langfelder wrote:
>>
>> On Fri, Jan 13, 2012 at 4:57 PM, Rui Esteves<ruimaximo at gmail.com> wrote:
>>>
>>> Hi.
>>> I am trying to modify kmeans function.
>>> It seems that is failing something obvious with the workspace.
>>> I am a newbie and here is my code:
>>
>>
>>>> <environment: namespace:stats>
>>>
>>> Error: unexpected '<' in "<"
>>
>>
>> Do not include the last line
>>
>> <environment: namespace:stats>
>>
>> it is not part of the function definition. Simply leave it out and
>> your function will be defined in the user workspace (a.k.a. global
>> environment).
>
>
> That's only partly right. Leaving it off will define the function in the
> global environment, but the definition might not work, because that's where
> it will look up variables, and the original function would look them up in
> the stats namespace. I don't know if that will matter, but it might lead to
> tricky bugs.
>
> What you should do when modifying a function from a package is set the
> environment to the same environment a function in the package would normally
> get, i.e. to the stats namespace. I think the as.environment() function can
> do this, but I always forget the syntax; an easier way is the following:
>
> Create the new function:
>
> kmeansnew <- function (...) ...
>
> Set its environment the same as the old one:
>
> environment(kmeansnew) <- environment(stats::kmeans)
>
> BTW, if you use the fix() function to get a copy for editing, it will do
> this for you automatically.
>
> Duncan Murdoch
>
>
More information about the R-help
mailing list