[R] Reading data

Henrik Bengtsson hb at stat.berkeley.edu
Fri Sep 25 19:18:45 CEST 2009


You can use R.utils (on CRAN) to help you figure out why the file is
not found or not readable.

library("R.utils");
pathname <- C:/Documents and Settings/ashta/My Documents/R_data/rel.dat";
pathname <- Arguments$getReadablePathname(pathname);
rel <- read.table(pathname, quote="", header=FALSE, sep="",
col.names=c("id","orel","nrel"));

If the file is not found it gives an error an tries to tell you why, e.g.

> Arguments$getReadablePathname("C:/Windows/system32/cmd.exe")
[1] "C:/Windows/system32/cmd.exe"

> Arguments$getReadablePathname("C:/Windows/system323/cmd.exe")
Error in list(`Arguments$getReadablePathname("C:/Windows/system323/cmd.exe")` =
<environment>,  :

[2009-09-25 10:11:57] Exception: Pathname not found:
C:/Windows/system323/cmd.exe (C:/Windows/ exists, but nothing beyond)
  at throw(Exception(...))
  at throw.default("Pathname not found: ", pathname, reason)
  at throw("Pathname not found: ", pathname, reason)
  at method(static, ...)
  at Arguments$getReadablePathname("C:/Windows/system323/cmd.exe")

It will also tell you if the file exists, but you don't have the
permission to read it.


Second, your error message reports on a pathname that starts with
'file=', which I've never seen;

  cannot open file 'file=C:/Documents and
Settings/sewalem/MyDocuments/R_data/rel.dat': Invalid argument

what version of R are you use, i.e. what does sessionInfo() give?


Third, it is true that backslashes need to be escaped.  However,
*forward-slashes* work with *any
platform*.  I stick with the latter so I don't have to think about it.
 It should make no difference in your case.


My $.02

/Henrik

On Fri, Sep 25, 2009 at 7:32 AM, Michael A. Miller <mmiller3 at iupui.edu> wrote:
> Sometimes it is easiest to open a file using a file selection
> widget.  I keep this in my .Rprofile:
>
> getOpenFile <- function(...){
>  require(tcltk)
>  return(tclvalue(tkgetOpenFile()))
> }
>
> With this you can find your file and open it with
>
>  rel <- read.table(getOpenFile(), quote="", header=FALSE, sep="", col.names=c("id","orel","nrel"))
>
> or
>
>  filename <- getOpenFile()
>  rel <- read.table(filename, quote="", header=FALSE, sep="", col.names=c("id","orel","nrel"))
>
> Mike
>
>
> P.S. I keep a couple functions on hand for choosing writable files
> and directories too...
>
> getSaveFile <- function(...){
>  require(tcltk)
>  return(tclvalue(tkgetSaveFile()))
> }
>
> chooseDir <- function(...){
>  require(tcltk)
>  return(tclvalue(tkchooseDirectory()))
> }
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list