Not only is it fun to think about a generic load function, I created one and
it is a pleasure to use--avoids the inconsistent function names that
prompted your post. What's more important, language purity (S3 methods) or
making life simpler for the users (consistency)?
This "import" is not a true S3 generic, but handles: clipboard, csv, rda,
rdata, txt, xls, xlsx. (You have to load one of the packages that has a
read.xls function--there are several different ones). A corresponding
"export" function can similarly be defined. Loosely inspired by importData
from S-Plus.
Kevin
import <- function(file, sheet=1, header=TRUE, ...){
if(file=="clipboard") {
dat <- read.table(file="clipboard", header=header, sep='\t',
na.strings=c('NA','*'),
as.is=TRUE)
return(dat)
}
# Otherwise 'file' is on a disk
stopifnot(file.exists(file))
getFileExtension <- function(file){
dotpos <- regexpr("\\.+[[:alnum:]]+$", file)
substring(file, ifelse(dotpos<1, 1000, dotpos+1))
}
ext <- casefold(getFileExtension(file))
if(ext=="csv")
dat <- read.csv(file, header, ...)
else if (ext=="rdata" | ext=="rda") {
## # Note, 'load' loads the data inside the frame of the 'import'
function
## # and returns the name of the object. We have to do some trickery
## # to move the data into 'our' object named 'dat'.
dat <- local({objname <- load(file); get(objname)})
} else if (ext=="txt")
dat <- read.table(file, sep="\t", header, ...) # header=FALSE
else if (ext=="xls") {
dat <- read.xls(file, sheet, ...)
} else if (ext=="xlsx") {
require("xlsx")
dat <- read.xlsx(file, sheet, header, ...)
} else {
stop("Unknown file extension")
}
return(dat)
}
On Tue, Oct 18, 2011 at 3:36 PM, Hadley Wickham <[email protected]> wrote:
> >> Ending names in .foo is a bad idea because of the S3 naming conventions,
> so
> >> I think this is unlikely. But you can always create an alias
> yourself...
> >
> > I always thought that S3 was part of the reason for read.ext write.ext.
> In:
> >
> > "/path/file.ext"
> >
> > the "class" of the file is "ext". I kind of like the idea of taking
> > this farther, generic functions read/write dispatch to the appropriate
> > method depending on the class of the file. Generally, only read/write
> > would be used, specifying the specific method as needed. read.rda and
> > write.rda could replace load/save where: dat <- read.rda() would
> > create an environment, dat rather than simply loading them into the
> > global environment.
> >
> > Though this is more of a hypothetical situation than a suggestion for
> change.
>
> That makes me think of a generic read that would dispatch not on the
> class of the first argument (which would always be a string), but
> would instead dispatch on the file extension. So read("x.csv") would
> call read.csv, read("cache.rds") would use readRDS etc. Of course it
> doesn't work in complete generality, but it's a fun idea to think
> about.
>
> Hadley
>
> --
> Assistant Professor / Dobelman Family Junior Chair
> Department of Statistics / Rice University
> http://had.co.nz/
>
> ______________________________________________
> [email protected] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
[[alternative HTML version deleted]]
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel