Re: [R] dumping/loading objects with 'tsp' attribute

2006-11-23 Thread Prof Brian Ripley
On Thu, 23 Nov 2006, Antonio, Fabio Di Narzo wrote:

 Dear all,
 I'm indirectly faced with the fact that setting the 'tsp' attribute of
 an object modifies its class definition:
 class( structure(1:2, tsp=c(1,2,1), class=c(myts,ts)) )
 [1] ts   myts

 In general, this is of really little (ok, I admit: totally no)
 interest for me because 'myts' class is added just after assigning the
 'tsp' attribute (by calling ts).
 However, this behaviour gives me troubles when re-loading a previously
 deparsed object, so that:
 x - ts(1:2)
 class(x) - c(myts, class(x))
 dput( x , temp.dat)
 class(dget(temp.dat))
 [1] ts   myts
 unlink(temp.dat)

 In other words, my real problem should be restated as: how to safely
 dump (and then load) an object which has a (perhaps valid) tsp
 attribute?
 More generally: can someone suggest me a safer way to dump/restoring R 
 objects?

save/load.

?dput comes with copious warnings about the problems of doing what you are 
attempting, so I wonder why you are surprised.

-- 
Brian D. Ripley,  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] dumping/loading objects with 'tsp' attribute

2006-11-23 Thread Antonio, Fabio Di Narzo
2006/11/23, Prof Brian Ripley [EMAIL PROTECTED]:
 On Thu, 23 Nov 2006, Antonio, Fabio Di Narzo wrote:

  Dear all,
  I'm indirectly faced with the fact that setting the 'tsp' attribute of
  an object modifies its class definition:
  class( structure(1:2, tsp=c(1,2,1), class=c(myts,ts)) )
  [1] ts   myts
 
  In general, this is of really little (ok, I admit: totally no)
  interest for me because 'myts' class is added just after assigning the
  'tsp' attribute (by calling ts).
  However, this behaviour gives me troubles when re-loading a previously
  deparsed object, so that:
  x - ts(1:2)
  class(x) - c(myts, class(x))
  dput( x , temp.dat)
  class(dget(temp.dat))
  [1] ts   myts
  unlink(temp.dat)
 
  In other words, my real problem should be restated as: how to safely
  dump (and then load) an object which has a (perhaps valid) tsp
  attribute?
  More generally: can someone suggest me a safer way to dump/restoring R 
  objects?

 save/load.

Ok. That seems to be the final statement: to be sure about the result,
I have to use binary files for storing R objects (even pure data
objects with attached attributes) instead of dumped R code.
I will accept that (infact, as I can read in 'save' help page, those
binaries should be portable across R platforms) and avoid boring you
anymore :-)


 ?dput comes with copious warnings about the problems of doing what you are
 attempting, so I wonder why you are surprised.

I've seen. That's because I've asked for suggestions, and not called
this a 'bug' or a 'nonsense' behaviour.


 --
 Brian D. Ripley,  [EMAIL PROTECTED]
 Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
 University of Oxford, Tel:  +44 1865 272861 (self)
 1 South Parks Road, +44 1865 272866 (PA)
 Oxford OX1 3TG, UKFax:  +44 1865 272595



-- 
Antonio, Fabio Di Narzo
Ph.D. student at
Department of Statistical Sciences
University of Bologna, Italy

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] dumping/loading objects with 'tsp' attribute

2006-11-23 Thread Gabor Grothendieck
On 11/23/06, Antonio, Fabio Di Narzo [EMAIL PROTECTED] wrote:
 2006/11/23, Prof Brian Ripley [EMAIL PROTECTED]:
  On Thu, 23 Nov 2006, Antonio, Fabio Di Narzo wrote:
 
   Dear all,
   I'm indirectly faced with the fact that setting the 'tsp' attribute of
   an object modifies its class definition:
   class( structure(1:2, tsp=c(1,2,1), class=c(myts,ts)) )
   [1] ts   myts
  
   In general, this is of really little (ok, I admit: totally no)
   interest for me because 'myts' class is added just after assigning the
   'tsp' attribute (by calling ts).
   However, this behaviour gives me troubles when re-loading a previously
   deparsed object, so that:
   x - ts(1:2)
   class(x) - c(myts, class(x))
   dput( x , temp.dat)
   class(dget(temp.dat))
   [1] ts   myts
   unlink(temp.dat)
  
   In other words, my real problem should be restated as: how to safely
   dump (and then load) an object which has a (perhaps valid) tsp
   attribute?
   More generally: can someone suggest me a safer way to dump/restoring R 
   objects?
 
  save/load.

 Ok. That seems to be the final statement: to be sure about the result,
 I have to use binary files for storing R objects (even pure data
 objects with attached attributes) instead of dumped R code.

If its mainly ascii you are after but the source file part is not important
then note that save can save to ascii files:

y - x
save(x, file = temp.rda, ascii = TRUE)
rm(x)
load(temp.rda)
identical(x, y)
readLines(temp.rda)
unlink(temp.rda)



 I will accept that (infact, as I can read in 'save' help page, those
 binaries should be portable across R platforms) and avoid boring you
 anymore :-)

 
  ?dput comes with copious warnings about the problems of doing what you are
  attempting, so I wonder why you are surprised.

 I've seen. That's because I've asked for suggestions, and not called
 this a 'bug' or a 'nonsense' behaviour.

 
  --
  Brian D. Ripley,  [EMAIL PROTECTED]
  Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
  University of Oxford, Tel:  +44 1865 272861 (self)
  1 South Parks Road, +44 1865 272866 (PA)
  Oxford OX1 3TG, UKFax:  +44 1865 272595
 


 --
 Antonio, Fabio Di Narzo
 Ph.D. student at
 Department of Statistical Sciences
 University of Bologna, Italy

 __
 R-help@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.


__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] dumping/loading objects with 'tsp' attribute

2006-11-23 Thread Gabor Grothendieck
On 11/23/06, Gabor Grothendieck [EMAIL PROTECTED] wrote:
 On 11/23/06, Antonio, Fabio Di Narzo [EMAIL PROTECTED] wrote:
  2006/11/23, Prof Brian Ripley [EMAIL PROTECTED]:
   On Thu, 23 Nov 2006, Antonio, Fabio Di Narzo wrote:
  
Dear all,
I'm indirectly faced with the fact that setting the 'tsp' attribute of
an object modifies its class definition:
class( structure(1:2, tsp=c(1,2,1), class=c(myts,ts)) )
[1] ts   myts
   
In general, this is of really little (ok, I admit: totally no)
interest for me because 'myts' class is added just after assigning the
'tsp' attribute (by calling ts).
However, this behaviour gives me troubles when re-loading a previously
deparsed object, so that:
x - ts(1:2)
class(x) - c(myts, class(x))
dput( x , temp.dat)
class(dget(temp.dat))
[1] ts   myts
unlink(temp.dat)
   
In other words, my real problem should be restated as: how to safely
dump (and then load) an object which has a (perhaps valid) tsp
attribute?
More generally: can someone suggest me a safer way to dump/restoring R 
objects?
  
   save/load.
 
  Ok. That seems to be the final statement: to be sure about the result,
  I have to use binary files for storing R objects (even pure data
  objects with attached attributes) instead of dumped R code.

 If its mainly ascii you are after but the source file part is not important
 then note that save can save to ascii files:

 y - x
 save(x, file = temp.rda, ascii = TRUE)
 rm(x)
 load(temp.rda)
 identical(x, y)
 readLines(temp.rda)
 unlink(temp.rda)

and here is a workaround if you are willing to do a bit of work when
outputting the objects:


# test data
x - ts(1:2)
class(x) - c(myts, ts)
y - x

# need 3 output statements
dump(x, temp.R)
write(attributes(x) -, temp.R, append = TRUE)
write(deparse(attributes(x)), temp.R, append = TRUE)

# source it back in and check
rm(x)
source(temp.R)
identical(x,y)
readLines(temp.R)

# clean up
unlink(temp.R)

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] dumping/loading objects with 'tsp' attribute

2006-11-23 Thread Antonio, Fabio Di Narzo
2006/11/23, Gabor Grothendieck [EMAIL PROTECTED]:
 On 11/23/06, Gabor Grothendieck [EMAIL PROTECTED] wrote:
  On 11/23/06, Antonio, Fabio Di Narzo [EMAIL PROTECTED] wrote:
   2006/11/23, Prof Brian Ripley [EMAIL PROTECTED]:
On Thu, 23 Nov 2006, Antonio, Fabio Di Narzo wrote:
   
 Dear all,
 I'm indirectly faced with the fact that setting the 'tsp' attribute of
 an object modifies its class definition:
 class( structure(1:2, tsp=c(1,2,1), class=c(myts,ts)) )
 [1] ts   myts

 In general, this is of really little (ok, I admit: totally no)
 interest for me because 'myts' class is added just after assigning the
 'tsp' attribute (by calling ts).
 However, this behaviour gives me troubles when re-loading a previously
 deparsed object, so that:
 x - ts(1:2)
 class(x) - c(myts, class(x))
 dput( x , temp.dat)
 class(dget(temp.dat))
 [1] ts   myts
 unlink(temp.dat)

 In other words, my real problem should be restated as: how to safely
 dump (and then load) an object which has a (perhaps valid) tsp
 attribute?
 More generally: can someone suggest me a safer way to dump/restoring 
 R objects?
   
save/load.
  
   Ok. That seems to be the final statement: to be sure about the result,
   I have to use binary files for storing R objects (even pure data
   objects with attached attributes) instead of dumped R code.
 
  If its mainly ascii you are after but the source file part is not important
  then note that save can save to ascii files:
 
  y - x
  save(x, file = temp.rda, ascii = TRUE)
  rm(x)
  load(temp.rda)
  identical(x, y)
  readLines(temp.rda)
  unlink(temp.rda)

 and here is a workaround if you are willing to do a bit of work when
 outputting the objects:


 # test data
 x - ts(1:2)
 class(x) - c(myts, ts)
 y - x

 # need 3 output statements
 dump(x, temp.R)
 write(attributes(x) -, temp.R, append = TRUE)
 write(deparse(attributes(x)), temp.R, append = TRUE)

 # source it back in and check
 rm(x)
 source(temp.R)
 identical(x,y)
 readLines(temp.R)

 # clean up
 unlink(temp.R)


Many thanks!
This works as I want, and can be eventually encapsulated in a sort of
safedump function for convenience.
More generally, interesting to learn that using
attributes-
has no side effects (i.e., attributes aren't set by using accessor
functions, isn't it?), as opposed to using 'structure' named
arguments, so can be more convenient in building R objects with
arbitrary attributes (at useR risk...).

-- 
Antonio, Fabio Di Narzo
Ph.D. student at
Department of Statistical Sciences
University of Bologna, Italy

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.