QUESTION: is there a way to make objects (e.g. data frames) read-only? BACKGROUND: I am writing some functions that use a data frame (frequencies of tidal constituents) that I want to be read-only. I can see how to accomplish this within a single function (just define the data in the function), but I'm not sure how to share read-only values between (un-nested) functions. Is there a more elegant method than duplicating the creation of the data frame in each function? -- View this message in context: http://www.nabble.com/how-to-make-read-only-data-frames--tp14756183p14756183.html Sent from the R help mailing list archive at Nabble.com.
Dan, You could write a function that just creates the data. And call this function each time you need the data. HTH, Thierry ------------------------------------------------------------------------ ---- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx at inbo.be www.inbo.be Do not put your faith in what statistics say until you have carefully considered what they do not say. ~William W. Watt A statistical analysis, properly conducted, is a delicate dissection of uncertainties, a surgery of suppositions. ~M.J.Moroney -----Oorspronkelijk bericht----- Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Namens Dan Kelley Verzonden: vrijdag 11 januari 2008 15:13 Aan: r-help at r-project.org Onderwerp: [R] how to make read-only data frames? QUESTION: is there a way to make objects (e.g. data frames) read-only? BACKGROUND: I am writing some functions that use a data frame (frequencies of tidal constituents) that I want to be read-only. I can see how to accomplish this within a single function (just define the data in the function), but I'm not sure how to share read-only values between (un-nested) functions. Is there a more elegant method than duplicating the creation of the data frame in each function? -- View this message in context: http://www.nabble.com/how-to-make-read-only-data-frames--tp14756183p1475 6183.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help at r-project.org mailing list 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.
I don't know of one. But you could use save() to save the dataframe to a different file, then remove the dataframe from .GlobalEnv, then attach the different file. While not strictly speaking read-only, that should provide good protection against accidental changes to the dataframe in the normal course of things. -Don At 6:13 AM -0800 1/11/08, Dan Kelley wrote:>QUESTION: is there a way to make objects (e.g. data frames) read-only? > >BACKGROUND: I am writing some functions that use a data frame (frequencies >of tidal constituents) that I want to be read-only. I can see how to >accomplish this within a single function (just define the data in the >function), but I'm not sure how to share read-only values between >(un-nested) functions. Is there a more elegant method than duplicating the >creation of the data frame in each function? > > >-- >View this message in context: >http://www.nabble.com/how-to-make-read-only-data-frames--tp14756183p14756183.html >Sent from the R help mailing list archive at Nabble.com. > >______________________________________________ >R-help at r-project.org mailing list >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.-- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062
I think what you want to do is define your data frame(s) in an environment that is not on the standard search path and then create your functions so that they use the same environment or their environments inherit from the one with the data frames. For large projects the best way to do this is probably create a package and use a namespace so that the data frame is available to your exported functions but not exported itself. you could also use functions like new.environment and environment to explicitly set the environment (this does not make the data frame strictly read only, just harder to change or overwrite by accident). Another approach for small quick cases is to use the local function, it will create a local environment then you can the data frame and a list of functions that use it. Hope this helps, ________________________________ From: r-help-bounces@r-project.org on behalf of Dan Kelley Sent: Fri 1/11/2008 7:13 AM To: r-help@r-project.org Subject: [R] how to make read-only data frames? QUESTION: is there a way to make objects (e.g. data frames) read-only? BACKGROUND: I am writing some functions that use a data frame (frequencies of tidal constituents) that I want to be read-only. I can see how to accomplish this within a single function (just define the data in the function), but I'm not sure how to share read-only values between (un-nested) functions. Is there a more elegant method than duplicating the creation of the data frame in each function? -- View this message in context: http://www.nabble.com/how-to-make-read-only-data-frames--tp14756183p14756183.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@r-project.org mailing list 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. [[alternative HTML version deleted]]