Re: [Rd] Extract/format/show for S4 objects
The tis package has the ti (TimeIndex) class that does what you want, and I modestly think it's nicer than the zoo stuff. As for S4 classes, there's a good reason many of us don't use them: they're too inflexible and hard to program with. I wouldn't go there unless I really had to. Johann Hibschman writes: > Hi all, > > I'm trying to make an integer-backed quarter (as in fraction of year) > class, but I can't quite it to work. I want integer-backed so I don't > have to worry about floating-point effects when doing math, and so that > I can use it as in data.table. > > First of all, is there a good reference for this anywhere? All of the > S4 tutorials that I've found have been too high-level, and I can't find > any examples of implementing extract. In S3, I can use [.Date as my > example, but I can't find the equivalent for S4. > > Second, is this misguided? > > Now for the details. Given this start: > > library(zoo) # borrowing yearqtr for output. > setClass("iqtr", contains="integer") > ## Create an iqtr object from a numeric date (e.g. 2000.5). > iqtr <- function (x) { > iq <- as.integer(floor((as.numeric(x) - 2000) * 4 + 0.0001)) > new("iqtr", iq) > } > setMethod("show", "iqtr4", > function (object) { > cat(format(yearqtr(as.numeric(x) * 0.25 + 2000))) > }) > > > I have two issues: > > 1. Vectors of iqtrs do not display properly. > They print, but they don't look like vectors. >> iq <- iqtr(seq(2000, 2002, 0.25)) >> iq >2000 Q2 2000 Q2 2000 Q3 2000 Q4 2001 Q2 2002 Q1> > > 2. Subsets do not stay in the class. >> iq[1:2] >> [1] 0 1 > > Clearly, I'm just not understanding what's going on. Is there a guide > for how to make printing, format, subsetting, etc., all work for S4 > classes? > > Thanks, > Johann > -- Jeff __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Extract/format/show for S4 objects
Gabor Grothendieck writes: > The yearqtr class already rounds off automatically to avoid floating > point effects and handles #1 and #2. The main use for all this was so I could use quarters as index columns with data.table, which demands integer storage mode. (This is part of my ongoing attempt to reinvent q/kdb+ in R.) I've had some problems with yearmon's conversions in the past, so I'm (probably unfairly) suspicious of the approach of storing dates as floats. To be fair, once I reported the bug, it was fixed almost immediately; I was very impressed. > On the other hand if you are just interested in playing around with S4 > for its own sake review the source code of the mondate package as an > example of an S4 based date package. Thanks, that's a very useful example. I don't understand all of what's going on in there, but I find it easier to understand the documentation with an example in hand. Cheers, Johann __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Extract/format/show for S4 objects
Note that zoo's merge can handle that: >> library(zoo) > z <- zooreg(1:6, as.yearqtr("2000 Q1")) > merge(z, zlag = lag(z1, -1)) z zlag 2000 Q1 1 NA 2000 Q2 21 2000 Q3 32 2000 Q4 43 2001 Q1 54 2001 Q2 65 2001 Q3 NA6 On Wed, Jun 9, 2010 at 10:09 AM, Johann Hibschman wrote: > Gabor Grothendieck writes: > >> The yearqtr class already rounds off automatically to avoid floating >> point effects and handles #1 and #2. > > The main use for all this was so I could use quarters as index columns > with data.table, which demands integer storage mode. (This is part of > my ongoing attempt to reinvent q/kdb+ in R.) > > I've had some problems with yearmon's conversions in the past, so I'm > (probably unfairly) suspicious of the approach of storing dates as > floats. To be fair, once I reported the bug, it was fixed almost > immediately; I was very impressed. > >> On the other hand if you are just interested in playing around with S4 >> for its own sake review the source code of the mondate package as an >> example of an S4 based date package. > > Thanks, that's a very useful example. I don't understand all of what's > going on in there, but I find it easier to understand the documentation > with an example in hand. > > Cheers, > Johann > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Extract/format/show for S4 objects (Johann Hibschman)
Johann, Following up on Gabor's reply, the mondate package (new on CRAN last week) will accomplish your needs. >I'm trying to make an integer-backed quarter (as in fraction of year) >class, but I can't quite it to work. I want integer-backed so I don't >have to worry about floating-point effects when doing math, and so that >I can use it as in data.table. mondate represents a date internally as a numeric, not an integer, but the integer part corresponds to months, not years, so a zero fractional part of a mondate corresponds to a "complete month." Every third whole number, therefore, would correspond to a complete quarter. Arithmetic in months (or quarters by dividing by 3) returns a numeric with a "timeunits" attribute. mondate's can be stored in a data.frame. > (A<-mondate.ymd(2010,3*(1:4))) # display format is "U.S.", my location mondate: timeunits="months" [1] 03/31/2010 06/30/2010 09/30/2010 12/31/2010 > diff(A) # 3 months apart [1] 3 3 3 > A-mondate("12/31/2009") # months since the end of last year; date input format also works for my location [1] 3 6 9 12 attr(,"timeunits") [1] "months" > c((A-mondate("12/31/2009"))/3) # divide by 3 and remove the attribute and you have quarters [1] 1 2 3 4 > data.frame(QtrEnd=A, Amount=rep(1000,4)) QtrEnd Amount 1 03/31/2010 1000 2 06/30/2010 1000 3 09/30/2010 1000 4 12/31/2010 1000 If the date you are subtracting off is somewhere in the fourth quarter of 2009 but you still want to treat it as being representative of the fourth quarter, the floor function will remove the fractional part of the difference in quarters (I will enter November 1st in non-US format): > floor(c((A-mondate("2009-11-1"))/3)) [1] 1 2 3 4 >First of all, is there a good reference for this anywhere? All of the >S4 tutorials that I've found have been too high-level, and I can't find >any examples of implementing extract. In S3, I can use [.Date as my >example, but I can't find the equivalent for S4. See the source code for an S4 "[" method. I was unable to convert all S3 methods to S4, so you will see that some S3 methods remain (e.g., as.data.frame.mondate); S3methods=TRUE in the class definition as a result. I wrote this package to solve essentially your problem. If it falls short for you somehow, please me know. Thanks, - Dan Murphy [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] Documenting generic S4 replacement method for package building
Dear List Members, I'm struggling with the documentation of a generic S4 replacement method. I've created a S4 method "lows" via setGeneric("lows", function(object) standardGeneric("lows")) setGeneric("lows<-", function(object, value) standardGeneric("lows<-")) setMethod("lows", "myClass", function(object) { listOut = vector(mode = "list") for(i in names(factors(object))) { listOut[i] = low(obj...@factors[[i]]) } return(listOut) } ) setReplaceMethod("lows", "myclass", function(object,value) { for(i in seq(along = obj...@factors)) { low(obj...@factors[[i]]) = value[i] } return(object) } ) As far as I understand "2.1.3 Documenting S4 classes and methods (Writing R-exts)" http://cran.r-project.org/doc/manuals/R-exts.html#Documenting-S4-classes-and -methods I need to call promptMethods(lows) After that an S4 replacement method is documented in the same way as an S3 one: see the description of \method in Documenting functions. That's where I get lost. The example gives me \usage{ \method{print}{ts}(x, calendar, \dots) } If I do (see below for the whole file) \usage{ \method{lows}{myClass}(object) } R CMD check myPackage gives me Undocumented code objects ... lows lows<- And Objects in \usage without \alias in documentation object "lows-methods": lows.myClass which is S3 syntax? The problem is probably trivial to somebody who has written a documentation file for an S4 replacement method but I'm stuck and tried many things, none of them working. Does somebody what needs to be added to the file? Thank you in advance Thomas Roth - \name{lows-methods} \docType{methods} \alias{lows-methods} \alias{lows,facDesign-method} \title{Methods for Function lows in Package `myPackage'} \description{ Methods for function \code{lows} in Package `myPackage' } \section{Methods}{ \describe{ \item{\code{signature(object = "facDesign")}}{ %% ~~describe this method here~~ } }} \keyword{methods} \keyword{ ~~ other possible keyword(s) ~~ } __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] Solaris / CC builds
Hello, I'm trying to figure our why my package (cxxPack) fails to build under Solaris using CC on CRAN, and I wonder if somebody can comment? If I try to build R on a Solaris SPARC box that has CC the R configure script uses gcc/g++ instead of CC, yet CC seems to be used for the CRAN builds. How does one tall configure to setup for using CC? Thanks, Dominick [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Solaris / CC builds
On Jun 9, 2010, at 12:47 PM, Dominick Samperi wrote: > Hello, > > I'm trying to figure our why my package (cxxPack) fails to build under > Solaris using CC on CRAN, and I wonder if somebody can comment? > > If I try to build R on a Solaris SPARC box that has CC the R configure script > uses gcc/g++ instead of CC, yet CC seems to be used for the CRAN builds. > > How does one tall configure to setup for using CC? > See R-admin C.5: http://r.research.att.com/man/R-admin.html#Solaris Cheers, Simon __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] Argument mismatches in S3 generic and method
The way R treats the first argument to an S3 method which uses a different name for the argument than the generic depends on whether there is a ... in the argument list. If there is is no ellipsis then the call cannot tag the argument with either name but an untagged first argument works: > zNoDots <- function(x) UseMethod("zNoDots") > zNoDots.foo <- function(fooObject) { cat("zNoDots.foo: fooObject=") ; dput(fooObject) } > foo <- structure(1:4, class="foo") > x <- "x in .GlobalEnv" > zNoDots(foo) zNoDots.foo: fooObject=structure(1:4, class = "foo") > zNoDots(x=foo) zNoDots.foo: fooObject=Error in .Call("R_isS4Object", object, PACKAGE = "base") : 'object' is missing > zNoDots(fooObject=foo) Error in zNoDots(fooObject = foo) : unused argument(s) (fooObject = foo) If there is an ellipsis in the function definition the call can tag the argument by the name given in the method, but not by the generic's name for the argument (and an untagged argument works here also): > zDots <- function(x,...) UseMethod("zDots") > zDots.foo <- function(fooObject,...) { cat("zDots.foo: fooObject=") ; dput(fooObject) } > zDots(foo) zDots.foo: fooObject=structure(1:4, class = "foo") > zDots(x=foo) zDots.foo: fooObject=Error in .Call("R_isS4Object", object, PACKAGE = "base") : 'object' is missing > zDots(fooObject=foo) zDots.foo: fooObject=structure(1:4, class = "foo") Is there a deep reason for this difference or is it just an accident of implementation? The question came up when someone asked me why var.test(x=y~group, data=data.frame(y=1:5,group=letters[c(1,1,1,2,2)])) failed but leaving off the x= allowed it to work. var.test's first argument is called "x" but var.test.formula's is "formula". Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Question on trying to build R 2.11.1 on Tru64(aka OSF1)
First I tried 'setenv R_SHELL /usr/local/bin/bash', as bash is the weapon of choice for the faculty wishing to use R, then ran ./configure as before. The ./configure output line using as R_SHELL for scripts ... /usr/local/bin/bash would seem to indicate that the R_SHELL environment variable was recognized and acknowledged. However, I got the same build error: gnumake[2]: Entering directory `/usrX/JunqueYard/R-2.11.1/src/library/Recommended' begin installing recommended package MASS /usrX/JunqueYard/R-2.11.1/bin/Rcmd: @: not found gnumake[2]: *** [MASS.ts] Error 1 gnumake[2]: Leaving directory `/usrX/JunqueYard/R-2.11.1/src/library/Recommended' Rcmd for this build: #!/usr/local/bin/bash # # ${R_HOME}/bin/Rcmd ## Shell script wrapper for all R CMD commands. ## For internal use only. R_CMD="${R_HOME}/bin/Rcmd" export R_CMD R_VERSION=2.11.1 export R_VERSION R_OSTYPE="unix" export R_OSTYPE ## Add 'share/perl' to the perl library path. if test -n "${PERL5LIB}"; then PERL5LIB="${R_SHARE_DIR}/perl:${PERL5LIB}" export PERL5LIB else PERLLIB="${R_SHARE_DIR}/perl:${PERLLIB}" export PERLLIB fi ## Append 'share/texmf' to TeX's input search path. if test -z "$TEXINPUTS}"; then TEXINPUTS=".:${R_SHARE_DIR}/texmf:" else TEXINPUTS=".:${TEXINPUTS}:${R_SHARE_DIR}/texmf:" fi export TEXINPUTS . "${R_HOME}/etc${R_ARCH}/Renviron" export `sed 's/^ *#.*//; s/^\(.*\)=.*/\1/' "${R_HOME}/etc${R_ARCH}/Renviron"` extra= case "${1}" in perl) cmd="${PERL}" ;; awk) cmd="${AWK}" ;; ## this was a separate command prior to 2.10.0 Rd2txt) cmd="${R_HOME}/bin/Rdconv" extra="-t txt" ;; Rd2pdf) cmd="${R_HOME}/bin/Rd2dvi" extra="--pdf" ;; *) if test -x "${R_HOME}/bin/${1}"; then cmd="${R_HOME}/bin/${1}" else cmd="${1}" fi ;; esac shift exec "${cmd}" ${extra} "$...@}" ### Local Variables: *** ### mode: sh *** ### sh-indentation: 2 *** ### End: *** I tried removing the R_SHELL environment variable and editing the ./src/scripts/Rcmd.in, changing the {1}'s to {...@}'s. ./configure produced the following line showing that R_SHELL was back to ksh: using as R_SHELL for scripts ... /bin/ksh But that also produced the same build error gnumake[2]: Entering directory `/usrX/JunqueYard/R-2.11.1/src/library/Recommended' begin installing recommended package MASS /usrX/JunqueYard/R-2.11.1/bin/Rcmd: @: not found gnumake[2]: *** [MASS.ts] Error 1 gnumake[2]: Leaving directory `/usrX/JunqueYard/R-2.11.1/src/library/Recommended' Rcmd for this build: Rcmd for this build: #!/bin/ksh # # ${R_HOME}/bin/Rcmd ## Shell script wrapper for all R CMD commands. ## For internal use only. R_CMD="${R_HOME}/bin/Rcmd" export R_CMD R_VERSION=2.11.1 export R_VERSION R_OSTYPE="unix" export R_OSTYPE ## Add 'share/perl' to the perl library path. if test -n "${PERL5LIB}"; then PERL5LIB="${R_SHARE_DIR}/perl:${PERL5LIB}" export PERL5LIB else PERLLIB="${R_SHARE_DIR}/perl:${PERLLIB}" export PERLLIB fi ## Append 'share/texmf' to TeX's input search path. if test -z "$TEXINPUTS}"; then TEXINPUTS=".:${R_SHARE_DIR}/texmf:" else TEXINPUTS=".:${TEXINPUTS}:${R_SHARE_DIR}/texmf:" fi export TEXINPUTS . "${R_HOME}/etc${R_ARCH}/Renviron" export `sed 's/^ *#.*//; s/^\(.*\)=.*/\1/' "${R_HOME}/etc${R_ARCH}/Renviron"` extra= case "$...@}" in perl) cmd="${PERL}" ;; awk) cmd="${AWK}" ;; ## this was a separate command prior to 2.10.0 Rd2txt) cmd="${R_HOME}/bin/Rdconv" extra="-t txt" ;; Rd2pdf) cmd="${R_HOME}/bin/Rd2dvi" extra="--pdf" ;; *) if test -x "${R_HOME}/bin/$...@}"; then cmd="${R_HOME}/bin/$...@}" else cmd="$...@}" fi ;; esac shift exec "${cmd}" ${extra} "$...@}" ### Local Variables: *** ### mode: sh *** ### sh-indentation: 2 *** ### End: *** I also tried 'setenv R_SHELL /bin/sh' with the same results. Bill >Return-path: >Date: Tue, 08 Jun 2010 22:02:13 +0100 (BST) >From: Prof Brian Ripley >Subject: Re: [Rd] Question on trying to build R 2.11.1 on Tru64(aka OSF1) >system >To: bill.gless...@cwu.edu >Cc: r-devel@r-project.org > >Ah, so configure has chosen /bin/ksh as the shell, and that does not >like this sh syntax on your system. Try setting R_SHELL=/bin/sh when >configuring (or the path to bash, if you have that). > >Alternatively, use $...@} in Rcmd.in (we have that as a workaround in >R.sh.in). > >On Tue, 8 Jun 2010, bill.gless...@cwu.edu wrote: > >> >> Here is the Rcmd from the ./bin subdirectory of the build tree >> /usrX/JunqueYard/R-2.11.1: >> >> ...snip... R-devel@r-project.org mailing list >>> https://stat.ethz.ch/mailman/listinfo/r-devel >>> >>> -- >>> Brian D. Ripley, rip...@stats.ox.ac.uk >>> 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 >> >>
[Rd] Broken link in HTML version of "R Installation and Administration" manual
Hi, I just updated my R-devel today using the tarball from 2010-06-07 (r52225). The link to "3.1.10 64-bit Windows builds" in the table of content at the top of /doc/manual/R-admin.html is broken. The table of content has: 3.1.10 64-bit Windows builds but latter in the file, the referred section is tagged with: Thanks, H. -- Hervé Pagès Program in Computational Biology Division of Public Health Sciences Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N, M2-B876 P.O. Box 19024 Seattle, WA 98109-1024 E-mail: hpa...@fhcrc.org Phone: (206) 667-5791 Fax:(206) 667-1319 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Question on trying to build R 2.11.1 on Tru64(aka OSF1)
bill.gless...@cwu.edu wrote: > First I tried 'setenv R_SHELL /usr/local/bin/bash', as bash is the weapon > of choice for the faculty wishing to use R, then ran ./configure as before. > The ./configure output line > > using as R_SHELL for scripts ... /usr/local/bin/bash > > would seem to indicate that the R_SHELL environment variable was recognized > and acknowledged. However, I got the same build error: Two ideas (longshots...) (a) You're building in the source directory. Try not doing that an start with clean sources. (b) insert "set -v" at the top of Rcmd so that you see the script as it is executed. -- Peter Dalgaard Center for Statistics, Copenhagen Business School Phone: (+45)38153501 Email: pd@cbs.dk Priv: pda...@gmail.com __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel