2010-08-12 7 views
7

एक व्यक्ति dput() एक एस 4 ऑब्जेक्ट कैसे होगा? मैंने कोशिश की इसएक एस 4 ऑब्जेक्ट dputting

require(sp) 
require(splancs) 
plot(0, 0, xlim = c(-100, 100), ylim = c(-100, 100)) 
poly.d <- getpoly() #draw a pretty polygon - PRETTY! 
poly.d <- rbind(poly.d, poly.d[1,]) # close the polygon because of Polygons() and its kin 
poly.d <- SpatialPolygons(list(Polygons(list(Polygon(poly.d)), ID = 1))) 
poly.d 
dput(poly.d) 

सूचना है कि अगर मैं dput() एक एस 4 वस्तु, मैं इसे फिर से फिर से संगठित नहीं कर सकते। तुम्हारे विचार?

+1

क्यों आप वस्तुओं इस तरह का निर्माण करना चाहते हैं? ऐसा लगता है कि यह एक ऐसा फ़ंक्शन लिखने से बहुत कम पठनीय होगा जो एक टेम्पलेट ऑब्जेक्ट बनाता है और देता है जिसे आप समायोजित कर सकते हैं। – Vince

+0

यह केवल थोड़ी सी बात है जब मैंने परीक्षण उद्देश्यों के लिए एक छोटे बहुभुज को बचाने की कोशिश की। मैं मानता हूं कि एन * 2 मैट्रिक्स और एक ऐसा फ़ंक्शन होना आसान है जो उस पर थोड़ा उलझन डालता है। –

उत्तर

9

जैसा कि वर्तमान में यह खड़ा है, आप इस ऑब्जेक्ट को 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 
+0

मेरे लिए बहुत उपयोगी है! धन्यवाद। एक फिक्स की आवश्यकता थी: अंतिम रिकर्सिव कॉल से पहले इस लाइन को dput2 में जोड़ा गया: 'if (i> 1) cat (",", file = file) ' – Roger

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^