Empty R environment becomes large file when saved

后端 未结 2 1339
遥遥无期
遥遥无期 2020-12-24 08:47

I\'m getting behaviour I don\'t understand when saving environments. The code below demonstrates the problem. I would have expected the two files (far-too-big.RData

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-24 09:37

    This is not my area of expertise but I belive environments work like this.

    • Any environment inherits everything in its parent environment.
    • All function calls create their own environment.

    The result of the above in your case is:

    1. When you run fn() it creates its own local environment (green), whose parent by default is globalenv() (grey).
    2. When you create the environment test (red) inside fn() its parent defaults to fn()'s environment (green). test will therefore include the object a.
    3. When you create the environment test1 (blue) and explicitly states that its parent is globalenv() it is separated from fn()'s environment and does not inherit the object a.

    So when saving test you also save a (somewhat hidden) copy of the object a. This does not happen when you save test1 as it does not include the object a.

    enter image description here

    Update

    Apparently this is a more complicated topic than I used to believe. Although I might just be quoting @joris-mays answer now I'd like to take a final go at it.

    To me the most intuitive visualization of environments would be a tree structure, see below, where each node is an environment and the arrows point to its respective enclosing environment (which I would like to believe is the same as its parent, but that has to do with frames and is beyond my corner of the world). A given environment encloses all objects you can reach by moving down the tree and it can access all objects you can reach by moving up the tree. When you save an environment it appears you save all objects and environments that are both enclosed by it and accessible from it (with the exception of globalenv()).

    However, the take home message is as Joris already stated: save your objects as lists and you don't need to worry.

    enter image description here

    If you want to know more I can recommend Norman Matloff's excellent book the art of R programming. It is aimed at software development in R rather than primary data analysis and assumes you have a fair bit of programming experience. I must admit I haven't fully digested the environment part yet, but as the rest of the book is very well written and pedagogical I assume this one is too.

提交回复
热议问题