[rkward-devel] [rkward-cvs] SF.net SVN: rkward:[4063] trunk/rkward/rkward/plugins/00saveload/import

Milan Bouchet-Valat nalimilan at club.fr
Sun Dec 4 19:44:42 UTC 2011


Le dimanche 04 décembre 2011 à 20:24 +0100, Thomas Friedrichsmeier a
écrit :
> well, for all I can see, all available solutions have some real drawbacks. On 
> my system, perl is no problem, while my rJava installation tends to be broken, 
> frequently. But certainly, for many users it will be the other way around.
FWIW, the RODBC package can be used to read .xls and .xlsx files on
Windows, since a driver is installed by default on that platform (as a
bonus, Access .mdb and .mdbx files are also read). As it's written by
Microsoft, we can expect it to support correctly these formats. ;-)

The code is relatively simple, and well documented (see [1], p. 16). For
illustration purposes, here's what I'm using in a Rcmdr plugin I wrote
(doItAndPrint() is Rcmdr way of running a command):

# 'file' contains the path to the file
ext <- tolower(substring(file, nchar(file) - sop + 2, nchar(file)))
channelStr <- switch(EXPR = ext,
	             xls = "odbcConnectExcel",
	             xlsx = "odbcConnectExcel2007",
	             mdb = "odbcConnectAccess",
	             accdb = "odbcConnectAccess2007")
doItAndPrint(paste("channel <- ", channelStr, "(\"", file, "\")",
                   sep=""))

# For Excel and Access, need to select a particular sheet or table
tabdat <- sqlTables(channel)
names(tabdat) <- tolower(names(tabdat))

if(ext == "mdb" || ext == "accdb")
    tabdat <- tabdat[tabdat$table_type == "TABLE", 3]

if(ext == "xls" || ext == "xlsx") {
    tabname <- tabdat$table_name
    tabdat <- ifelse(tabdat$table_type == "TABLE",
                     substring(tabname, 2, nchar(tabname) - 2),
                     substring(tabname, 1, nchar(tabname) - 1))
}

# If there are several tables
if(length(tabdat) > 1)
    fil <- tk_select.list(sort(tabdat),
                          title=gettext_Rcmdr("Select one table"))
else
    fil <- tabdat

if(fil == "") {
    Message(gettext_Rcmdr("No table selected"), type="error")
    return()
}

if(ext == "xls" || ext == "xlsx")
    fil <- paste("[", fil, "$]", sep = "")

# Retrieve the data
command <- paste("sqlQuery(channel=channel, select * from ", fil,")",
	         sep = "")
doItAndPrint(paste("corpusDataset <- ", command, sep = ""))
doItAndPrint("odbcCloseAll()")


Just my two cents


1: http://cran.r-project.org/web/packages/RODBC/vignettes/RODBC.pdf





More information about the Rkward-devel mailing list