Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WISH: Simple class for files / pathnames #9

Open
HenrikBengtsson opened this issue Jan 14, 2016 · 4 comments
Open

WISH: Simple class for files / pathnames #9

HenrikBengtsson opened this issue Jan 14, 2016 · 4 comments

Comments

@HenrikBengtsson
Copy link
Owner

It would be useful to have a class for files (e.g. File) and basic functions for creating objects of such classes. I can imagine that this file class is a simple extension of character, because that is currently how files/pathnames are currently represented, e.g.

File <- function(...) { x <- c(...); structure(x, class=c("File", class(x))) }

Then existing functions returning files/pathnames, e.g.

dir <- function(...) File(base::dir(...))

and instead of c() users can have, say, p() for very brief syntax, e.g. p <- File.

Examples:

> pathname <- p("R/zzz.R")
> pathnames <- p("R/000.R", "R/zzz.R")
> pathnames <- dir("R/")
@mtmorgan
Copy link

  • 'isa' is not such a good model here, e.g., R's connections are not character vectors.
  • presumably one would like to transparently manage a collection of files, so File() has length(), [ and names() methods
  • It would be helpful to define the API, e.g., open(), close(), isOpen() and it's relation to the existing connection API.
  • Finalizers seem important
  • a 'registry' of file types seems useful, to map extensions (e.g., '.csv') to appropriate subclasses.

@lawremi
Copy link

lawremi commented Jan 14, 2016

Need to keep the notions of path (File) and stream (connection) distinct. We open() a path to get a stream. There would only be close() and isOpen() methods for the stream.

Also, the path represents a low-level format, but the stream might have richer semantics. For example, an HDF5 file might be represented by an HDF5File object. Lets say someone stored genotypes in the HDF5 file. The open() function might return an object that implements the same API as the VCF class. We could have a dual-dispatch openWithScheme generic that dispatches on the path and some object representing the VCF scheme.

@mtmorgan
Copy link

@lawremi Path versus stream is a useful distinction; sometimes its useful to create a stream without opening it, or to have a stream that exists in a state where it can be opened after being closed. Hence open() on the stream.

@leeper
Copy link

leeper commented Jan 15, 2016

And what potentially intrigues me would be filetype-specific classes that would allow method dispatch. I'm mainly think about this for data import/export (in the context of rio), but I can imagine there might be other contexts, such as calling graphics devices based on file type without the need for jpeg(), png(), etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants