Saving R objects (code) in .R files (R genetic programming)

前端 未结 4 1841
囚心锁ツ
囚心锁ツ 2021-01-21 17:02

I\'m using R for genetic programming with the RGP package. The environment creates functions that solve problems. I want to save these functions in their own .R source files. I

4条回答
  •  一向
    一向 (楼主)
    2021-01-21 17:38

    While the question has already been answered, I should mention that dump has some pitfalls and IMO you would be better off saving your function in binary format via save.

    In particular, dump only saves the function code itself, not any associated environment. This may not be a problem here, but could bite you at some point. For example, suppose we have

    e <- new.env()
    e$x <- 10
    f <- function(y) y + x
    environment(f) <- e
    

    Then dump("f") will only save the function definition of f, and not its environment. If you then source the resulting file, f will no longer work correctly. This doesn't happen if you use save and load.

提交回复
热议问题