Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1000373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:30:09+00:00 2026-05-16T07:30:09+00:00

How would a person dput() an S4 object? I tried this require(sp) require(splancs) plot(0,

  • 0

How would a person dput() an S4 object? I tried this

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)

Notice that if I dput() an S4 object, I cannot reconstruct it again. Your thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:30:10+00:00Added an answer on May 16, 2026 at 7:30 am

    As it currently stands, you cannot dput this object. The code of dput 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()
    }
    

    This handles S4 objects recursively, but it relies on the assumption an S3 object will not contain an S4 object, which in your example does not hold:

    > isS4(slot(poly.d,'polygons'))
    [1] FALSE
    > isS4(slot(poly.d,'polygons')[[1]])
    [1] TRUE
    

    Edit: Here is a work-around the limitations of dput. It works for the example you provided, but I don’t think that it will work in general (e.g. 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 it is in action:

    > dput2(poly.d,file=(tempFile <- tempfile()))
    > poly.d2 <- dget(tempFile)
    > all.equal(poly.d,poly.d2)
    [1] TRUE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would a person use the Lua scripting language to do things such as
I'm a bit rubbish with Regex and was wondering if some kind person would
At run time I would like to find whether an EntityObject also has a
I have a list of Person s each which have multiple fields that I
Picture this: you are creating a little module that people can incorporate into their
I want to create a simple form with only an input textbox and search
Is there a guaranteed sequence of execution of the following java code: int i
I am half way reading the OWL2 primer and is having problem understanding the
Two temp tables are created and then loaded...Here's the schema. Create table #SH ([date]
In Linq Get items higher then lastname I asked how to get people higher

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.