[R] help exporting to excel via xlsReadWritePro

Hans-Peter Suter gchappi at gmail.com
Thu Jul 2 22:36:06 CEST 2009


> I’m trying to export a file from R to excel using the xlsReadWritePro function and I keep getting the error below.

POSIXt is not currently supported (neither in data.frames nor vectors)
and must be converted to other types (see ?write.xls). I almost never
use POSIXlt/ct myself but it would probably be a good idea to provide
support (I've added it as a request). Code:

dates <- c( "2009-07-02 19:30:00", "2009-07-02 19:31:15", "2009-07-02
20:12:42" )
write.xls( data.frame( as.POSIXlt( dates ) ), "anothertestfile.xls" )
# gives error
write.xls( as.POSIXlt( dates ), "anothertestfile.xls" )
     # do.l

> [snip] Also is there anyway to export different datasets to  multiple sheets similar to the ods function in SAS?

Here is an example:

  # setup
library( "xlsReadWritePro" )
dates <- c( "2009-07-02 19:30:00", "2009-07-02 19:31:15", "2009-07-02
20:12:42" )
mydat <- data.frame( idt = as.character( as.POSIXlt( dates ) ),
                     odt1 = isoStrToDateTime( dates ),
                     odt2 = isoStrToDateTime( dates ),
                     val = c( 1, 2, 3 ),
                     stringsAsFactors = FALSE )
class( mydat[,1] ) <- "isodatetime"                     # required
('character' is not currently accepted as isodatetime column (will be
fixed))
class( mydat[,2] ) <- "oledatetime"                     # optional
('numeric' will be accepted as oledatetime column)

  # write data (note: FALSE is necessary to keep the data.frame)
xls <- xls.new ( "testfile.xls" )                       # create new
spreadsheet and keep in memory
write.xls( mydat[,1,FALSE], xls, sheet = 1, cols = 1 )  # write
write.xls( mydat[,2, FALSE], xls, sheet = 1, cols = 2 ) # do.
write.xls( mydat[,3, FALSE], xls, sheet = 1, cols = 3, colClasses =
"oledatetime" ) # do.
write.xls( mydat[,4], xls, sheet = 2 )                  # do. in sheet 2
xls.sheet( xls, 'select', 1 )                           # first sheet
shall be active when spreadsheet is opened
xls.close( xls )                                        # save and
close memory represention

Hope this helps/clarifies things. Otherwise feel free to ask again (PM
prefered as it's probably better not to use the r-help mailling list
for questions about a non-free library). Btw, there will be a forum
some time later.

Cheers,
Hans-Peter




More information about the R-help mailing list