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

前端 未结 4 1837
囚心锁ツ
囚心锁ツ 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:23

    The way I understand your question, you'd like to get a text representation of the functions and save that to a file. To do that, you can divert the output of the R console using the sink function.

    sink(file="MyBestFunction.R")
    bf_str
    sink()
    

    Then you can source the file or open it using R or another program via your OS.

    EDIT:

    To append a comment to the end of the file, you could do the following:

    theScore <- .876
    
    sink(file = "MyBestFunction.R", append = TRUE)
    cat("# This was a great function with a score of", theScore, "\r\n")
    sink()
    

    Depending on your setup, you might need to change \r\n to reflect the appropriate end of line characters. This should work on DOS/Windows at least.

提交回复
热议问题