2

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 can't for the life of me figure out how. One way I've tried is this:

bf_str = print(bf)
save(bf_str,file="MyBestFunction.R"))

bf is simply the best fit function. I've tried it also like this:

save(bf,file="MyBestFunction.R"))

The outputted file is super weird. Its just a bunch of crazy characters

Zeke Nierenberg
  • 2,216
  • 1
  • 19
  • 30
  • Related to [R: Turning RData file into script files](http://stackoverflow.com/questions/3835714/r-turning-rdata-file-into-script-files) and [How to print the structure of an R object to the console](http://stackoverflow.com/questions/9991393/how-to-print-the-structure-of-an-r-object-to-the-console/9991460). – Joshua Ulrich Aug 16 '12 at 12:36

4 Answers4

7

You can use dump for this. It will save the assignment and the definition so you can source it later.

R> f <- function(x) x*2
R> dump("f")
R> rm(f)
R> source("dumpdata.R")
R> f
function(x) x*2

Update to respond to the OP's additional request in the comments of another answer:

You can add attributes to your function to store whatever you want. You could add a score attribute:

R> f <- function(x) x*2
R> attr(f, 'score') <- 0.876
R> dump("f")
R> rm(f)
R> source("dumpdata.R")
R> f
function(x) x*2
attr(,"score")
[1] 0.876
R> readLines("dumpdata.R")
[1] "f <-"                                     
[2] "structure(function(x) x*2, score = 0.876)"
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
6

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.

BenBarnes
  • 19,114
  • 6
  • 56
  • 74
  • Ah. That's not how I read the question, but I bet you're right. – GSee Aug 16 '12 at 12:16
  • @GSee, the "just a bunch of crazy characters" part suggested that maybe plain text is the goal. But we'll see... – BenBarnes Aug 16 '12 at 12:19
  • Option 1 works, but this also looks good. I'd love a way to output some more text in the file in a comment, specifically its score... Thanks – Zeke Nierenberg Aug 16 '12 at 18:03
  • @ZekeAlexandreNierenberg, There are a few ways to include comments in the file. One way to do it is added as an edit above. It might be helpful to include a small working example if this isn't what you're looking for. – BenBarnes Aug 17 '12 at 06:07
  • @ZekeAlexandreNierenberg: you should have added that extra requirement to your question. It would have given others a chance to respond, instead of only the one answer you commented on. I have added a solution to your extra requirement to my answer. – Joshua Ulrich Aug 17 '12 at 13:59
  • Sorry, still learning Stack Overflow etiquette. Didn't know that was a question when I posted. Your solution is very tidy. – Zeke Nierenberg Aug 17 '12 at 20:12
3

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.

Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
2

You can pass save the names of objects to be saved via the list argument

save(list="bf_str", file="MyBestFunction.R")

Alternatively, you could use dput

dput(bf_str, file='MyFun.R')

and dget to retrieve

bf_str <- dget("MyFun.R")
GSee
  • 48,880
  • 13
  • 125
  • 145