[R] Question regarding S4 objects and reading in excel data with RODBC

Duncan Murdoch murdoch.duncan at gmail.com
Wed Aug 4 13:09:25 CEST 2010


On 04/08/2010 5:38 AM, Sander wrote:
> L.S.
> 
> I am trying to get data from an excel sheet using the RODBC library, quite
> like the example on page 34 of the 'Data manipulation with R' book written
> by Phil Spector. It seems very straightforward, yet I get the same error
> everytime I try it, with different excel sheets and trying to tweek the code
> a bit. This is what I do:
> 
> library(RODBC)
> 
> sheet='X:\\example.xls'
> con=odbcConnectExcel(sheet)
> 
> tbls=sqlTables(con)
> tbls$TABLE_NAME
> qry=paste("SELECT * FROM '",tbls at TABLE_NAME[1],"'", sep=" ")
> 
> 
> The error I get is:
> Error in paste("SELECT * FROM '", tbls at TABLE_NAME[1], "'", sep = " ") : 
>   trying to get slot "TABLE_NAME" from an object (class "data.frame") that
> is not an S4 object

tbls is not an S4 object, so you can't use the @ notation with it.  In 
some older versions of R @ would extract an attribute (because that's 
where S4 slots were stored), but current versions don't allow this. 
Without testing, I would guess you can replace

tbls at TABLE_NAME[1]

with

attr(tbls, "TABLE_NAME")[1]

but it's possible the name is wrong.

Duncan Murdoch

> 
> Does anyone know what I'm doing wrong here? I have to admit that I don't
> know much about sql.
> 
> 
> Best regards,
> 
> Sander van Kuijk



More information about the R-help mailing list