Hello everybody, Does anybody know how to obtain a deep copy of a list? The context is the following. I am passing a list to a function which, in turn, passes it to compiled C code via .Call. The problem is that the C code modifies elements of the list so that, after the R function returns, I end up with a modified version of the list argument. I believe passing to the function a deep copy of the list argument would solve my problem. Thank you in advance for the help! Giovanni -- Giovanni Petris Professor and Director of Statistics Department of Mathematical Sciences University of Arkansas Fayetteville, AR 72701 USA [[alternative HTML version deleted]]
Or modifying the C code to conform with the usual functional copy-on-write paradigm used throughout most of R. On November 12, 2022 12:04:57 PM PST, Giovanni Petris <gpetris at uark.edu> wrote:>Hello everybody, > >Does anybody know how to obtain a deep copy of a list? > >The context is the following. I am passing a list to a function which, in turn, passes it to compiled C code via .Call. The problem is that the C code modifies elements of the list so that, after the R function returns, I end up with a modified version of the list argument. I believe passing to the function a deep copy of the list argument would solve my problem. > >Thank you in advance for the help! > >Giovanni > >-- >Giovanni Petris >Professor and Director of Statistics >Department of Mathematical Sciences >University of Arkansas >Fayetteville, AR 72701 >USA > > > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.-- Sent from my phone. Please excuse my brevity.
? Sat, 12 Nov 2022 20:04:57 +0000 Giovanni Petris <gpetris at uark.edu> ?????:> The problem is that the C code modifies elements of the list so that, > after the R function returns, I end up with a modified version of the > list argument. I believe passing to the function a deep copy of the > list argument would solve my problem.Is it an option to change the C code? I think that the fault lies with it, not with its caller. Except when using reference types, it should be safe to make the assumption that variables in R are passed as if by copy. In the interest of getting a quick workaround, does unserialize(serialize(L, NULL)) help? I would expect that to work unless your list contains something like environments. -- Best regards, Ivan