जैसा कि वर्तमान में यह खड़ा है, आप इस ऑब्जेक्ट को dput
नहीं कर सकते हैं। dput
का कोड निम्नलिखित पाश में शामिल हैं:
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()
}
यह एस 4 रिकर्सिवली वस्तुओं संभालती है, लेकिन यह धारणा पर निर्भर करता है एक S3 वस्तु एक एस 4 वस्तु, शामिल नहीं होंगे जो अपने उदाहरण में नहीं रखता है:
> isS4(slot(poly.d,'polygons'))
[1] FALSE
> isS4(slot(poly.d,'polygons')[[1]])
[1] TRUE
संपादित करें: यहां dput
की सीमाओं के आसपास एक कार्य है। यह आपके द्वारा प्रदान किए गए उदाहरण के लिए काम करता है, लेकिन मुझे नहीं लगता कि यह सामान्य रूप से काम करेगा (उदा। यह गुणों को संभाल नहीं करता है)।
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))
}
और यहाँ यह कार्रवाई में है:
> dput2(poly.d,file=(tempFile <- tempfile()))
> poly.d2 <- dget(tempFile)
> all.equal(poly.d,poly.d2)
[1] TRUE
क्यों आप वस्तुओं इस तरह का निर्माण करना चाहते हैं? ऐसा लगता है कि यह एक ऐसा फ़ंक्शन लिखने से बहुत कम पठनीय होगा जो एक टेम्पलेट ऑब्जेक्ट बनाता है और देता है जिसे आप समायोजित कर सकते हैं। – Vince
यह केवल थोड़ी सी बात है जब मैंने परीक्षण उद्देश्यों के लिए एक छोटे बहुभुज को बचाने की कोशिश की। मैं मानता हूं कि एन * 2 मैट्रिक्स और एक ऐसा फ़ंक्शन होना आसान है जो उस पर थोड़ा उलझन डालता है। –