As it stands now, you cannot dput this object. The dput code contains the following loop:
if (isS4(x)) { cat("new(\"", class(x), "\"\n", file = file, sep = "") for (n in slotNames(x)) { cat(" ,", n, "= ", file = file) dput(slot(x, n), file = file, control = control) } cat(")\n", file = file) invisible() }
Recursively processes S4 objects, but relies on the assumption that the S3 object will not contain the S4 object, which in your example does not execute:
> isS4(slot(poly.d,'polygons')) [1] FALSE > isS4(slot(poly.d,'polygons')[[1]]) [1] TRUE
Edit: The following is a description of the limitations of dput . It works for the example you provided, but I don't think it will work at all (for example, it does not handle attributes).
dput2 <- function (x, file = "", control = c("keepNA", "keepInteger", "showAttributes")){ if (is.character(file)) if (nzchar(file)) { file <- file(file, "wt") on.exit(close(file)) } else file <- stdout() opts <- .deparseOpts(control) if (isS4(x)) { cat("new(\"", class(x), "\"\n", file = file, sep = "") for (n in slotNames(x)) { cat(" ,", n, "= ", file = file) dput2(slot(x, n), file = file, control = control) } cat(")\n", file = file) invisible() } else if(length(grep('@',capture.output(str(x)))) > 0){ if(is.list(x)){ cat("list(\n", file = file, sep = "") for (i in 1:length(x)) { if(!is.null(names(x))){ n <- names(x)[i] if(n != ''){ cat(" ,", n, "= ", file = file) } } dput2(x[[i]], file = file, control = control) } cat(")\n", file = file) invisible() } else { stop('S4 objects are only handled if they are contained within an S4 object or a list object') } } else .Internal(dput(x, file, opts)) }
And here he is in action:
> dput2(poly.d,file=(tempFile <- tempfile())) > poly.d2 <- dget(tempFile) > all.equal(poly.d,poly.d2) [1] TRUE