Re: [Rd] Why is strptime always returning a vector of length 9 ?

2009-08-09 Thread Martin Maechler
> "l" == laurent  
> on Sun, 09 Aug 2009 21:45:07 +0200 writes:

l> Thanks.  It seems that the source of my confusion comes
l> from using first using str() (and then once on the wrong
l> track, it is easier to miss the information a man page
l> that also describes POSIXct that is itself a vector of
l> length equal to the number of entries it contains).

l> With the current example:
>> str(xd)
l>  POSIXlt[1:9], format: "2007-03-09" "2007-05-31"
l> "2008-11-12" "2008-11-12" ...

l> A quick inspection of the output does indicate a
l> something with nine elements, but the elements appear to
l> be "2007-03-09", "2007-05-31", etc... possibly creating
l> confusion.

l> To make it even more confusing I have:
>> x[1]
l> [1] "March 09, 2007"
>> str(x[1])
l>  chr "March 09, 2007"

l> For what it is worth, I think that the behavior of the
l> extract operator "[" (defined as a S3 method
l> "[.POSIXlt()") is inconsistent with the output of
l> length() (default method for lists).

Yes, exactly;  these two have beeb defined inconsistently,
exactly in the respect you mention.

Many months ago, when I came to the same conclusion, 
I vaguely remember that I had wanted / proposed to change this
(namely, change [.POSIXlt, the  length() method for  "POSIXlt") ,
but IIRC had been vetoed by others.
... another frustrating experience for me ...

Martin Maechler, ETH Zurich



l> On Sun, 2009-08-09 at 11:45 -0500, Jeff Ryan wrote:
>> The reason is in the ?strptime under value:
>> 
>> 'strptime' turns character representations into an object of class
>> '"POSIXlt"'.  The timezone is used to set the 'isdst' component
>> and to set the '"tzone"' attribute if 'tz != ""'.
>> 
>> And POSIXlt is a list of length 9.
>> 
>> 
>> HTH
>> Jeff
>> 
>> On Sun, Aug 9, 2009 at 10:35 AM, Gabor
>> Grothendieck wrote:
>> > Try this to see its components:
>> >
>> >> str(unclass(xd))
>> > List of 9
>> >  $ sec  : num [1:6] 0 0 0 0 0 0
>> >  $ min  : int [1:6] 0 0 0 0 0 0
>> >  $ hour : int [1:6] 0 0 0 0 0 0
>> >  $ mday : int [1:6] 9 31 12 12 30 30
>> >  $ mon  : int [1:6] 2 4 10 10 6 6
>> >  $ year : int [1:6] 107 107 108 108 109 109
>> >  $ wday : int [1:6] 5 4 3 3 4 4
>> >  $ yday : int [1:6] 67 150 316 316 210 210
>> >  $ isdst: int [1:6] 0 1 0 0 1 1
>> >
>> > and read R News 4/1 for more.
>> >
>> > On Sun, Aug 9, 2009 at 10:20 AM, laurent wrote:
>> >> Dear List,
>> >>
>> >>
>> >> I am having an issue with strptime (see below).
>> >> I can reproduce it on R-2.8, R-2.9, and R-2.10-dev, I tempted to see
>> >> either a bug or my misunderstanding (and then I just don't currently 
see
>> >> where).
>> >>
>> >> # setup:
>> >> x <- c("March 09, 2007", "May 31, 2007", "November 12, 2008", 
"November
>> >> 12, 2008", "July 30, 2009", "July 30, 2009" )
>> >>
>> >> # showing the problem
>> >>> length(x)
>> >> 6
>> >>> xd <- strptime(x, format = "%B %d, %Y")
>> >>> length(xd)
>> >> 9
>> >>> xd[1:9]
>> >> [1] "2007-03-09" "2007-05-31" "2008-11-12" "2008-11-12" "2009-07-30"
>> >> [6] "2009-07-30" NA   NA   NA
>> >>> length(strptime(rep(x, 2), format="%B %d, %Y"))
>> >> [1] 9
>> >>> strptime(rep(x, 2), format="%B %d, %Y")[1:12]
>> >>  [1] "2007-03-09" "2007-05-31" "2008-11-12" "2008-11-12" "2009-07-30"
>> >>  [6] "2009-07-30" "2007-03-09" "2007-05-31" "2008-11-12" "2008-11-12"
>> >> [11] "2009-07-30" "2009-07-30
>> >>
>> >> Any pointer would be appreciated.
>> >>
>> >> L.

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] model.matrix evaluation challenges

2009-08-09 Thread Ben Bolker
  I am having difficulty with evaluation/environment construction
for a formula to be evaluated by model.matrix().  Basically, I
want to construct a model matrix that first looks in "newdata"
for the values of the model parameters, then in "obj...@data".
Here's what I've tried:

  1. model.matrix(~f,data=c(newdata,obj...@data)) -- fails because
something (terms()?) within model.matrix() tries to coerce "data"
to a data frame -- but newdata and obj...@data have different
numbers of rows.

  2. with(c(newdata,obj...@data),model.matrix(~f))   works --
BUT not when ~f is assigned to another object (as it will be
when it is being dug out of a previously fit model).

  3. If "f" exists as a column within newdata and/or obj...@data,
but the formula is assigned to another object (see below), then
with(c(newdata,obj...@data),model.matrix(z))  fails -- because
model.matrix() is explicitly evaluating the variables in the formula
in the environment of z (i.e., ignoring the first argument of "with" ...)


  Any advice on how to solve this without making a bigger mess?

  sincerely
Ben Bolker


## set up a data frame for prediction

set.seed(1001)
f = factor(rep(letters[1:4],each=20))
x = runif(80)
u = rnorm(4)
y = rnorm(80,mean=2+x*(3+u[f]),sd=0.1)
dat = data.frame(f,x,y)

## fit a model ... could easily do by lm() but want to
##   demonstrate the problem

library(bbmle)
m1 = mle2(y~dnorm(a+b*x,sd=exp(logs)),parameters=list(b~f),data=dat,
  start=list(a=0,b=2,logs=-3))

## data frame for prediction
pp0 = expand.grid(x=seq(0,1,length=11),
  f=levels(dat$f))

## combine frame and model data: have to keep the model data
##  around, because it contain other information needed for
##  prediction.

cc = c(pp0,m...@data)
try(mm <- model.matrix(~f,cc))  ## fails -- different number of rows
  ## (at some point R tries to coerce cc to a data frame)
nrow(with(cc,model.matrix(~f))) ## works ... but ...

## here's what happens within predict.mle2()
## extract information about parameter models
params <- eval(m...@call$parameters)
params <- params[[1]]
params[[2]] <- NULL

nrow(with(cc,model.matrix(params)))
rm(f)
## object "f" not found -- because model.matrix
##  evaluates in the parent environment of params ...
try(nrow(with(cc,model.matrix(params



-- 
Ben Bolker
Associate professor, Biology Dep't, Univ. of Florida
bol...@ufl.edu / www.zoology.ufl.edu/bolker
GPG key: www.zoology.ufl.edu/bolker/benbolker-publickey.asc



signature.asc
Description: OpenPGP digital signature
__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Problem using model.frame with argument subset in own function

2009-08-09 Thread Gavin Simpson
On Sun, 2009-08-09 at 11:32 -0500, Douglas Bates wrote:
> On Sat, Aug 8, 2009 at 1:31 PM, Gavin Simpson wrote:
> > Dear List,
> 
> > I am writing a formula method for a function in a package I maintain. I
> > want the method to return a data.frame that potentially only contains
> > some of the variables in 'data', as specified by the formula.
> 
> The usual way to call model.frame (the method that Thomas Lumley has
> called "the standard, non-standard evaluation) is to match the call to
> foo, replace the name of the function being called with
> as.name("model.frame") and force an evaluation in the parent frame.
> it looks like
> 

Thanks Doug. I also received an off-list reply from Brian Ripley
suggesting two alternative approaches.

The bit I was missing was how to manipulate other aspects of the call -
it hadn't clicked that the arguments of the function can be manipulated
by altering the components of the matched call.

In the end I came up with something like:

mf <- match.call()
mf[[1]] <- as.name("model.frame")
mt <- terms(formula, data = data, simplify = TRUE)
mf[[2]] <- formula(mt, data = data)
mf$na.action <- substitute(na.action)
dots <- list(...)
mf[[names(dots)]] <- NULL
mf <- eval(mf,parent.frame())
tran.default(mf, ...)

which seems to be working in the tests I have been running, allowing me
to pass along some components of the call to model.frame, whilst
reserving ... for the default methods arguments, and also get the
simplified formula.

All the best,

G

> mf <- match.call()
> if (missing(data)) data <- environment(formula)
> ## evaluate and install the model frame
> m <- match(c("formula", "data", "subset", "weights", "na.action", 
> "offset"),
>names(mf), 0)
> mf <- mf[c(1, m)]
> mf$drop.unused.levels <- TRUE
> mf[[1]] <- as.name("model.frame")
> fr <- eval(mf, parent.frame())
> 
> The point of all of this manipulation is to achieve the kind of result
> you need where the subset argument is evaluated in the correct
> environmnent.
> 
> > The problem I am having is in writing the function and wrapping it
> > around model.frame. Consider the following data frame:
> >
> > dat <- data.frame(A = runif(10), B = runif(10), C = runif(10))
> >
> > And the wrapper function:
> >
> > foo <- function(formula, data = NULL, ..., subset = NULL,
> >na.action = na.pass) {
> >mt <- terms(formula, data = data, simplify = TRUE)
> >mf <- model.frame(formula(mt), data = data, subset = subset,
> >  na.action = na.action)
> >## real function would do more stuff here and pass mf on to
> >## other functions
> >mf
> > }
> >
> > This is how I envisage the function being called. The real world use
> > would have a data.frame with tens or hundreds of components where only a
> > few need to be excluded. Hence wanting formulas of the form below to
> > work.
> >
> > foo(~ . - B, data = dat)
> >
> > The aim is to return only columns A and C in an object returned by
> > model.frame. However, when I run the above, I get the following error:
> >
> >> foo(~ A + B, data = dat)
> > Error in xj[i] : invalid subscript type 'closure'
> >
> > I've tracked this down to the line in model.frame.default
> >
> >subset <- eval(substitute(subset), data, env)
> >
> > After evaluating this line, subset contains:
> >
> > Browse[1]> subset
> > function (x, ...)
> > UseMethod("subset")
> > 
> >
> > Not NULL, and hence the error later on when calling the internal
> > model.frame code.
> >
> > So the question is, what am I doing wrong?
> >
> > If I leave the subset argument out of the definition of foo and rely
> > upon the default in model.frame.default, the function works as
> > expected.
> >
> > Perhaps the question should be, how do I modify foo() to allow it to
> > have a formal subset argument, passed to model.frame?
> >
> > Any other suggestions gratefully accepted.
> >
> > Thanks in advance,
> >
> > G
> > --
> > %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
> >  Dr. Gavin Simpson [t] +44 (0)20 7679 0522
> >  ECRC, UCL Geography,  [f] +44 (0)20 7679 0565
> >  Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk
> >  Gower Street, London  [w] http://www.ucl.ac.uk/~ucfagls/
> >  UK. WC1E 6BT. [w] http://www.freshwaters.org.uk
> > %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
> >
> > __
> > R-devel@r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
> >
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,  [f] +44 (0)20 7679 0565
 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London  [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT. [w] http://www.freshwaters.org.uk
%

Re: [Rd] Why is strptime always returning a vector of length 9 ?

2009-08-09 Thread laurent
Thanks.

It seems that the source of my confusion comes from using first using
str() (and then once on the wrong track, it is easier to miss the
information a man page that also describes POSIXct that is itself a
vector of length equal to the number of entries it contains).

With the current example:
> str(xd)
 POSIXlt[1:9], format: "2007-03-09" "2007-05-31" "2008-11-12"
"2008-11-12" ...

A quick inspection of the output does indicate a something with nine
elements, but the elements appear to be "2007-03-09", "2007-05-31",
etc... possibly creating confusion.

To make it even more confusing I have:
> x[1]
[1] "March 09, 2007"
> str(x[1])
 chr "March 09, 2007"

For what it is worth, I think that the behavior of the extract operator
"[" (defined as a S3 method "[.POSIXlt()") is inconsistent with the
output of length() (default method for lists).


L.




On Sun, 2009-08-09 at 11:45 -0500, Jeff Ryan wrote:
> The reason is in the ?strptime under value:
> 
> 'strptime' turns character representations into an object of class
>  '"POSIXlt"'.  The timezone is used to set the 'isdst' component
>  and to set the '"tzone"' attribute if 'tz != ""'.
> 
> And POSIXlt is a list of length 9.
> 
> 
> HTH
> Jeff
> 
> On Sun, Aug 9, 2009 at 10:35 AM, Gabor
> Grothendieck wrote:
> > Try this to see its components:
> >
> >> str(unclass(xd))
> > List of 9
> >  $ sec  : num [1:6] 0 0 0 0 0 0
> >  $ min  : int [1:6] 0 0 0 0 0 0
> >  $ hour : int [1:6] 0 0 0 0 0 0
> >  $ mday : int [1:6] 9 31 12 12 30 30
> >  $ mon  : int [1:6] 2 4 10 10 6 6
> >  $ year : int [1:6] 107 107 108 108 109 109
> >  $ wday : int [1:6] 5 4 3 3 4 4
> >  $ yday : int [1:6] 67 150 316 316 210 210
> >  $ isdst: int [1:6] 0 1 0 0 1 1
> >
> > and read R News 4/1 for more.
> >
> > On Sun, Aug 9, 2009 at 10:20 AM, laurent wrote:
> >> Dear List,
> >>
> >>
> >> I am having an issue with strptime (see below).
> >> I can reproduce it on R-2.8, R-2.9, and R-2.10-dev, I tempted to see
> >> either a bug or my misunderstanding (and then I just don't currently see
> >> where).
> >>
> >> # setup:
> >> x <- c("March 09, 2007", "May 31, 2007", "November 12, 2008", "November
> >> 12, 2008", "July 30, 2009", "July 30, 2009" )
> >>
> >> # showing the problem
> >>> length(x)
> >> 6
> >>> xd <- strptime(x, format = "%B %d, %Y")
> >>> length(xd)
> >> 9
> >>> xd[1:9]
> >> [1] "2007-03-09" "2007-05-31" "2008-11-12" "2008-11-12" "2009-07-30"
> >> [6] "2009-07-30" NA   NA   NA
> >>> length(strptime(rep(x, 2), format="%B %d, %Y"))
> >> [1] 9
> >>> strptime(rep(x, 2), format="%B %d, %Y")[1:12]
> >>  [1] "2007-03-09" "2007-05-31" "2008-11-12" "2008-11-12" "2009-07-30"
> >>  [6] "2009-07-30" "2007-03-09" "2007-05-31" "2008-11-12" "2008-11-12"
> >> [11] "2009-07-30" "2009-07-30
> >>
> >> Any pointer would be appreciated.
> >>
> >>
> >>
> >> L.
> >>
> >> __
> >> 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
> >
> 
> 
>

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] odbcConnectExcel on non-Windows?

2009-08-09 Thread Marc Schwartz

Hi Spencer,

Unfortunately, the two Perl modules in question, Text::CSV_XS and  
Encode, are not pure Perl modules as they contain C code.


This means that the modules need to be compiled either beforehand as a  
pre-packaged binary or during installation, both for the Perl version  
that is installed on the user's system and for the operating system in  
use. The modules are binary specific to the Perl minor version being  
used (eg. 5.6 is not compatible with 5.8, which is not compatible with  
5.10), which further complicates matters.


As a consequence, I cannot include them in the package and reasonably  
expect them to work for a given Perl version/OS combination.


For folks on Windows, there are essentially two options:

1. Install the ActiveState Perl distribution, which includes all  
required modules, without having to install anything extra. That is  
the easiest method today.


2. Install Duncan's Perl distribution as included in RTools, which at  
present does not include at least Text::CSV_XS. If one could convince  
Duncan to add that module to his offering, it would make that option  
easier, so as to not require users to separately install the module  
from CPAN or to install AS Perl. I have cc'd Duncan here to solicit  
his comments on that. It might also reduce the potential for confusion  
relative to having more than one Perl installation and dealing with  
setting $PATH, but that is not an overly complicated issue and why I  
have the 'perl' argument in the functions.



For folks on OSX, there seems to be the potential for Text::CSV_XS to  
be or not to be installed, possibly depending upon when they got their  
Mac and what updates have been installed. It was installed on my  
spring 2009 unibody MBP, but not on Prof. Ripley's MB Air, for  
example. If it is not, one can install XCode Tools to install a C  
compiler and then install/compile the module from CPAN or from  
MacPorts. One can also use the AS Perl installation to achieve the  
same simple approach as on Windows.


On most recent Linux distributions, the modules are either already  
present with the default Perl install or can be easily installed via  
the distribution's package management system (eg. yum or apt-get). Of  
course, AS Perl is also available here as yet another option if for  
some reason, folks don't want to use their Linux distribution's  
existing tools.


The above information is contained in the package INSTALL file. If  
things there are unclear, I welcome comments and content that might  
help to clarify things.


Regards,

Marc


On Aug 9, 2009, at 1:09 PM, spencerg wrote:


Dear Marc:

In spite of your efforts to help me, I still get an "F" in your  
"WriteXLS" class.  Consider the following:


> library(WriteXLS)
> help(pac=WriteXLS)
> testPerl()
Perl found.

The following Perl modules were not found on this system:

Text::CSV_XS

If you have more than one Perl installation, be sure the correct one  
was used here.


Otherwise, please install the missing modules. See the package  
INSTALL file for more information.


##

Might it be feasible to include the required Perl modules with  
the "WriteXLS" package?  Then you would not have to rely on fools  
like me not knowing how to get the preferred copy of any required  
Perl modules.


On my Windows platform (Vista), I got RODBC to work for  
"writeFindFn2xls" in the "sos" package.  Unfortunately, the "daily  
checks" on R-Forge failed on non-Windows platforms.  When I saw  
that, I asked R-Devel for help.  Dirk suggested I try "WriteXLS";   
see below.


What do you suggest?

If I don't hear an answer, I propose to NOT add "WriteXLS" to  
the "DESCRIPTION" file but rather to modify the code for  
"writeFindFn2xls" so that on non-Windows platforms, it tests to see  
if "WriteXLS" is installed.  If no, it will issue an appropriate  
error message.  If it finds "WriteXLS", it will try to use it.


However, if you have other suggestions, I'd be pleased to hear  
them.


Best Wishes,
Spencer Graves


> sessionInfo()
R version 2.9.1 (2009-06-26)
i386-pc-mingw32

locale:
LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States. 
1252;LC_MONETARY=English_United States. 
1252;LC_NUMERIC=C;LC_TIME=English_United States.1252


attached base packages:
[1] stats graphics  grDevices utils datasets  methods   base
other attached packages:
[1] sos_1.0-5  brew_1.0-3 WriteXLS_1.8.1
###

Dirk Eddelbuettel wrote:

On 9 August 2009 at 12:04, spencerg wrote:
|   What should I do regarding code to write an Excel file in a  
| non-Windows platform? [...]
|   What would you suggest we do about this? [...] |   If  
there is a better way to handle this, I would like to know.

http://cran.r-project.org/web/packages/WriteXLS/index.html

Portable (in the sense of used by Perl, Python, ... or other  
scripting
engines) solutions have existed for 

Re: [Rd] odbcConnectExcel on non-Windows?

2009-08-09 Thread spencerg
Dear Gabor: 

 Good suggestion.  I will probably do that if "WriteXLS" is not 
installed or if it is but "testPerl()" is FALSE. 


 Thanks,
 Spencer

Gabor Grothendieck wrote:

Instead of writing out an xls file you could write out a file
in any format that Excel can read, e.g. csv, with a suitable
renaming of your function.

On Sun, Aug 9, 2009 at 1:04 PM, spencerg wrote:
  

Hello:

What should I do regarding code to write an Excel file in a non-Windows
platform?

The "sos" package [new version of "RSiteSearch"] on R-Forge includes
"writeFindFn2xls", which starts with "require(RODBC)".  The next line calls
"odbcConnectExcel".  This works under Windows but fails under Linux and
MacOS.

What would you suggest we do about this?

It currently tests (.Platform$OS.type == "windows");  if(FALSE), it
issues an error, saying that it only works under Windows.  Also, the help
page skips those tests if the the platform is not Windows.

If there is a better way to handle this, I would like to know.

Thanks,
Spencer

__
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] odbcConnectExcel on non-Windows?

2009-08-09 Thread spencerg
Dear Marc: 



 In spite of your efforts to help me, I still get an "F" in your 
"WriteXLS" class.  Consider the following: 



> library(WriteXLS)
> help(pac=WriteXLS)
> testPerl()
Perl found.

The following Perl modules were not found on this system:

Text::CSV_XS

If you have more than one Perl installation, be sure the correct one was 
used here.


Otherwise, please install the missing modules. See the package INSTALL 
file for more information.


##

 Might it be feasible to include the required Perl modules with the 
"WriteXLS" package?  Then you would not have to rely on fools like me 
not knowing how to get the preferred copy of any required Perl modules. 



 On my Windows platform (Vista), I got RODBC to work for 
"writeFindFn2xls" in the "sos" package.  Unfortunately, the "daily 
checks" on R-Forge failed on non-Windows platforms.  When I saw that, I 
asked R-Devel for help.  Dirk suggested I try "WriteXLS";  see below. 



 What do you suggest? 



 If I don't hear an answer, I propose to NOT add "WriteXLS" to the 
"DESCRIPTION" file but rather to modify the code for "writeFindFn2xls" 
so that on non-Windows platforms, it tests to see if "WriteXLS" is 
installed.  If no, it will issue an appropriate error message.  If it 
finds "WriteXLS", it will try to use it. 



 However, if you have other suggestions, I'd be pleased to hear them. 



 Best Wishes,
 Spencer Graves


> sessionInfo()
R version 2.9.1 (2009-06-26)
i386-pc-mingw32

locale:
LC_COLLATE=English_United States.1252;LC_CTYPE=English_United 
States.1252;LC_MONETARY=English_United 
States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252


attached base packages:
[1] stats graphics  grDevices utils datasets  methods   base


other attached packages:
[1] sos_1.0-5  brew_1.0-3 WriteXLS_1.8.1
###

Dirk Eddelbuettel wrote:

On 9 August 2009 at 12:04, spencerg wrote:
|   What should I do regarding code to write an Excel file in a 
| non-Windows platform? 
[...]
|   What would you suggest we do about this? 
[...] 
|   If there is a better way to handle this, I would like to know. 


http://cran.r-project.org/web/packages/WriteXLS/index.html

Portable (in the sense of used by Perl, Python, ... or other scripting
engines) solutions have existed for a dozen years.  We have a wrapper to Perl
code for a long time too (having been of the initial discussions that lead to
Greg's initial read.xls from the gdata package).

Dirk




__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] odbcConnectExcel on non-Windows?

2009-08-09 Thread Gabor Grothendieck
Instead of writing out an xls file you could write out a file
in any format that Excel can read, e.g. csv, with a suitable
renaming of your function.

On Sun, Aug 9, 2009 at 1:04 PM, spencerg wrote:
> Hello:
>
>     What should I do regarding code to write an Excel file in a non-Windows
> platform?
>
>     The "sos" package [new version of "RSiteSearch"] on R-Forge includes
> "writeFindFn2xls", which starts with "require(RODBC)".  The next line calls
> "odbcConnectExcel".  This works under Windows but fails under Linux and
> MacOS.
>
>     What would you suggest we do about this?
>
>     It currently tests (.Platform$OS.type == "windows");  if(FALSE), it
> issues an error, saying that it only works under Windows.  Also, the help
> page skips those tests if the the platform is not Windows.
>
>     If there is a better way to handle this, I would like to know.
>
>     Thanks,
>     Spencer
>
> __
> 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] odbcConnectExcel on non-Windows?

2009-08-09 Thread Dirk Eddelbuettel

On 9 August 2009 at 12:04, spencerg wrote:
|   What should I do regarding code to write an Excel file in a 
| non-Windows platform? 
[...]
|   What would you suggest we do about this? 
[...] 
|   If there is a better way to handle this, I would like to know. 

http://cran.r-project.org/web/packages/WriteXLS/index.html

Portable (in the sense of used by Perl, Python, ... or other scripting
engines) solutions have existed for a dozen years.  We have a wrapper to Perl
code for a long time too (having been of the initial discussions that lead to
Greg's initial read.xls from the gdata package).

Dirk

-- 
Three out of two people have difficulties with fractions.

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] odbcConnectExcel on non-Windows?

2009-08-09 Thread spencerg
Hello: 



 What should I do regarding code to write an Excel file in a 
non-Windows platform? 



 The "sos" package [new version of "RSiteSearch"] on R-Forge 
includes "writeFindFn2xls", which starts with "require(RODBC)".  The 
next line calls "odbcConnectExcel".  This works under Windows but fails 
under Linux and MacOS. 



 What would you suggest we do about this? 



 It currently tests (.Platform$OS.type == "windows");  if(FALSE), 
it issues an error, saying that it only works under Windows.  Also, the 
help page skips those tests if the the platform is not Windows. 



 If there is a better way to handle this, I would like to know. 



 Thanks,
 Spencer

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Why is strptime always returning a vector of length 9 ?

2009-08-09 Thread Jeff Ryan
The reason is in the ?strptime under value:

'strptime' turns character representations into an object of class
 '"POSIXlt"'.  The timezone is used to set the 'isdst' component
 and to set the '"tzone"' attribute if 'tz != ""'.

And POSIXlt is a list of length 9.


HTH
Jeff

On Sun, Aug 9, 2009 at 10:35 AM, Gabor
Grothendieck wrote:
> Try this to see its components:
>
>> str(unclass(xd))
> List of 9
>  $ sec  : num [1:6] 0 0 0 0 0 0
>  $ min  : int [1:6] 0 0 0 0 0 0
>  $ hour : int [1:6] 0 0 0 0 0 0
>  $ mday : int [1:6] 9 31 12 12 30 30
>  $ mon  : int [1:6] 2 4 10 10 6 6
>  $ year : int [1:6] 107 107 108 108 109 109
>  $ wday : int [1:6] 5 4 3 3 4 4
>  $ yday : int [1:6] 67 150 316 316 210 210
>  $ isdst: int [1:6] 0 1 0 0 1 1
>
> and read R News 4/1 for more.
>
> On Sun, Aug 9, 2009 at 10:20 AM, laurent wrote:
>> Dear List,
>>
>>
>> I am having an issue with strptime (see below).
>> I can reproduce it on R-2.8, R-2.9, and R-2.10-dev, I tempted to see
>> either a bug or my misunderstanding (and then I just don't currently see
>> where).
>>
>> # setup:
>> x <- c("March 09, 2007", "May 31, 2007", "November 12, 2008", "November
>> 12, 2008", "July 30, 2009", "July 30, 2009" )
>>
>> # showing the problem
>>> length(x)
>> 6
>>> xd <- strptime(x, format = "%B %d, %Y")
>>> length(xd)
>> 9
>>> xd[1:9]
>> [1] "2007-03-09" "2007-05-31" "2008-11-12" "2008-11-12" "2009-07-30"
>> [6] "2009-07-30" NA           NA           NA
>>> length(strptime(rep(x, 2), format="%B %d, %Y"))
>> [1] 9
>>> strptime(rep(x, 2), format="%B %d, %Y")[1:12]
>>  [1] "2007-03-09" "2007-05-31" "2008-11-12" "2008-11-12" "2009-07-30"
>>  [6] "2009-07-30" "2007-03-09" "2007-05-31" "2008-11-12" "2008-11-12"
>> [11] "2009-07-30" "2009-07-30
>>
>> Any pointer would be appreciated.
>>
>>
>>
>> L.
>>
>> __
>> 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
>



-- 
Jeffrey Ryan
jeffrey.r...@insightalgo.com

ia: insight algorithmics
www.insightalgo.com

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Problem using model.frame with argument subset in own function

2009-08-09 Thread Douglas Bates
On Sat, Aug 8, 2009 at 1:31 PM, Gavin Simpson wrote:
> Dear List,

> I am writing a formula method for a function in a package I maintain. I
> want the method to return a data.frame that potentially only contains
> some of the variables in 'data', as specified by the formula.

The usual way to call model.frame (the method that Thomas Lumley has
called "the standard, non-standard evaluation) is to match the call to
foo, replace the name of the function being called with
as.name("model.frame") and force an evaluation in the parent frame.
it looks like

mf <- match.call()
if (missing(data)) data <- environment(formula)
## evaluate and install the model frame
m <- match(c("formula", "data", "subset", "weights", "na.action", "offset"),
   names(mf), 0)
mf <- mf[c(1, m)]
mf$drop.unused.levels <- TRUE
mf[[1]] <- as.name("model.frame")
fr <- eval(mf, parent.frame())

The point of all of this manipulation is to achieve the kind of result
you need where the subset argument is evaluated in the correct
environmnent.

> The problem I am having is in writing the function and wrapping it
> around model.frame. Consider the following data frame:
>
> dat <- data.frame(A = runif(10), B = runif(10), C = runif(10))
>
> And the wrapper function:
>
> foo <- function(formula, data = NULL, ..., subset = NULL,
>                na.action = na.pass) {
>    mt <- terms(formula, data = data, simplify = TRUE)
>    mf <- model.frame(formula(mt), data = data, subset = subset,
>                      na.action = na.action)
>    ## real function would do more stuff here and pass mf on to
>    ## other functions
>    mf
> }
>
> This is how I envisage the function being called. The real world use
> would have a data.frame with tens or hundreds of components where only a
> few need to be excluded. Hence wanting formulas of the form below to
> work.
>
> foo(~ . - B, data = dat)
>
> The aim is to return only columns A and C in an object returned by
> model.frame. However, when I run the above, I get the following error:
>
>> foo(~ A + B, data = dat)
> Error in xj[i] : invalid subscript type 'closure'
>
> I've tracked this down to the line in model.frame.default
>
>    subset <- eval(substitute(subset), data, env)
>
> After evaluating this line, subset contains:
>
> Browse[1]> subset
> function (x, ...)
> UseMethod("subset")
> 
>
> Not NULL, and hence the error later on when calling the internal
> model.frame code.
>
> So the question is, what am I doing wrong?
>
> If I leave the subset argument out of the definition of foo and rely
> upon the default in model.frame.default, the function works as
> expected.
>
> Perhaps the question should be, how do I modify foo() to allow it to
> have a formal subset argument, passed to model.frame?
>
> Any other suggestions gratefully accepted.
>
> Thanks in advance,
>
> G
> --
> %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
>  Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
>  ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
>  Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
>  Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
>  UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
> %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
>
> __
> 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] Why is strptime always returning a vector of length 9 ?

2009-08-09 Thread Gabor Grothendieck
Try this to see its components:

> str(unclass(xd))
List of 9
 $ sec  : num [1:6] 0 0 0 0 0 0
 $ min  : int [1:6] 0 0 0 0 0 0
 $ hour : int [1:6] 0 0 0 0 0 0
 $ mday : int [1:6] 9 31 12 12 30 30
 $ mon  : int [1:6] 2 4 10 10 6 6
 $ year : int [1:6] 107 107 108 108 109 109
 $ wday : int [1:6] 5 4 3 3 4 4
 $ yday : int [1:6] 67 150 316 316 210 210
 $ isdst: int [1:6] 0 1 0 0 1 1

and read R News 4/1 for more.

On Sun, Aug 9, 2009 at 10:20 AM, laurent wrote:
> Dear List,
>
>
> I am having an issue with strptime (see below).
> I can reproduce it on R-2.8, R-2.9, and R-2.10-dev, I tempted to see
> either a bug or my misunderstanding (and then I just don't currently see
> where).
>
> # setup:
> x <- c("March 09, 2007", "May 31, 2007", "November 12, 2008", "November
> 12, 2008", "July 30, 2009", "July 30, 2009" )
>
> # showing the problem
>> length(x)
> 6
>> xd <- strptime(x, format = "%B %d, %Y")
>> length(xd)
> 9
>> xd[1:9]
> [1] "2007-03-09" "2007-05-31" "2008-11-12" "2008-11-12" "2009-07-30"
> [6] "2009-07-30" NA           NA           NA
>> length(strptime(rep(x, 2), format="%B %d, %Y"))
> [1] 9
>> strptime(rep(x, 2), format="%B %d, %Y")[1:12]
>  [1] "2007-03-09" "2007-05-31" "2008-11-12" "2008-11-12" "2009-07-30"
>  [6] "2009-07-30" "2007-03-09" "2007-05-31" "2008-11-12" "2008-11-12"
> [11] "2009-07-30" "2009-07-30
>
> Any pointer would be appreciated.
>
>
>
> L.
>
> __
> 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


[Rd] Why is strptime always returning a vector of length 9 ?

2009-08-09 Thread laurent
Dear List,


I am having an issue with strptime (see below).
I can reproduce it on R-2.8, R-2.9, and R-2.10-dev, I tempted to see
either a bug or my misunderstanding (and then I just don't currently see
where).

# setup:
x <- c("March 09, 2007", "May 31, 2007", "November 12, 2008", "November
12, 2008", "July 30, 2009", "July 30, 2009" )

# showing the problem
> length(x)
6
> xd <- strptime(x, format = "%B %d, %Y")
> length(xd)
9
> xd[1:9]
[1] "2007-03-09" "2007-05-31" "2008-11-12" "2008-11-12" "2009-07-30"
[6] "2009-07-30" NA   NA   NA   
> length(strptime(rep(x, 2), format="%B %d, %Y"))
[1] 9
> strptime(rep(x, 2), format="%B %d, %Y")[1:12]
 [1] "2007-03-09" "2007-05-31" "2008-11-12" "2008-11-12" "2009-07-30"
 [6] "2009-07-30" "2007-03-09" "2007-05-31" "2008-11-12" "2008-11-12"
[11] "2009-07-30" "2009-07-30

Any pointer would be appreciated.



L.

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel