Especificación de PATH cuando se usa el paquete R gdata

Problem: helpers fail when reading an .xls into R using gdata

I have some .xls that i'd like to read into R. I am able to do so using read.xls en la gdata package, however the helper functions sheetNames y sheetCount fail - i'd like to understand what i'm doing wrong so that i can use them as they would be very useful.

require(gdata)
fp <- file.path('~/data/first.xls')

When i know the structure of the sheets, and am able to point it at my intended data sheet, the perl script runs just fine:

firstdata <- read.xls(fp, sheet=2)

and i have my data... in firstdata.

However, in the very same sheets, the helpers fail.

I find myself opening the .xls in excel, figuring them out, and then loading into R using read.xls(fp, sheet=N) -- not a disaster, but neither is it ideal.

In particular, when the sheets are not my own, and I need information about them before i can set sheet=N in read.xls(), the helper functions sheetNames y sheetCount would be very useful, however they fail -- why?

sheetCount(fp)

> sheetNames(fp)
Error in read.table(tc, as.is = TRUE, header = FALSE) : 
  no lines available in input
In addition: Warning message:
running command ''/usr/bin/perl'     '~/R/wd/raRpackages/gdata/perl/sheetNames.pl' '~/data/first.xls'' had status 2 
Unable to open file '~/data/first.xls'.

y:

> sheetCount(fp)
Error in read.table(tc, as.is = TRUE, header = FALSE) :  
  no lines available in input
In addition: Warning message:
running command ''/usr/bin/perl'     '~/R/wd/raRpackages/gdata/perl/sheetCount.pl' '~/data/first.xls'' had status 2 
Unable to open file '~/data/first.xls'.

After a bit of fiddling, i (quite by accident) found that using the full path solves this problem:

fp2 <- file.path("/Users/ricardo/data/first.xls")
sheetcount(fp2)
[1] 13

preguntado el 31 de julio de 12 a las 11:07

1 Respuestas

Parece que ~ isn't being expanded out to your home directory. This expansion is usually done by the shell so R probably won't do it and perl definitely won't.

Instead, use the explicit path or $HOME or $ENV{HOME} from within the Perl program.

Respondido 31 Jul 12, 12:07

+1 thanks. that does fix the problem. as a general rule should one prefer the explicit paths to avoid these traps? - Ricardo

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.