You have to force the copy yourself. You can take advantage of R's
reference counting to see if it is necessary at the C level as well. See
the R extensions manual, 5.9.10, for more details:
http://cran.r-project.org/doc/manuals/R-exts.html#Named-objects-and-copying;
in particular, you can make use of the NAMED macro to see if multiple
symbols are pointing at the same memory.

I would recommend forcing the copy at the C level so that users don't run
into such surprises; copying at the R level is unnecessary if that object
is not otherwise used.

-Kevin


On Fri, Nov 22, 2013 at 1:43 PM, Skye Bender-deMoll <skyebend@skyeome.net>wrote:

> Dear R-devs,
>
> I'm working on a package where we have a function that modifies an Object
> via .Call  to a C function. Unfortunately in some situations this
> counterintuitive modifies a previously made copy of the object passed to
> the function.  For example, if I first make an assignment to "copy" the
> object,
>
> b<-a
>
> and then modify 'a' , the value of 'b' will be modified as well.  I
> understand that this is probably due to lazy evaluation--the copy of 'a' is
> not made until the 'a' or 'b' instance is further modified. Peeking at 'a'
> and 'b' using .Internal(inspect(a)) seems to confirm this.  But for some
> reason our function is not triggering the copy.
>
> We have been able to kludge it to force a copy and behave as expected by
> making a slight modification to the object before passing it to .Call:
>
> modificationFun <- function(x){
>   x$ForceCopy<-NULL
>   x<-.Call("do_the_mod_in_c_R", package='mypackage')
>   invisible(x)
> }
>
>
> What is the correct way to force the copy to occur to avoid inadvertently
> modifying both instances of the object?  Neither force() or eval() seem to
> do this.
>
> Would it be faster to call duplicate(x) inside the C function instead of
> forcing the copy at the R level?
>
> thanks for your help,
>  -skye
>
> ______________________________________________
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

	[[alternative HTML version deleted]]

