Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PATH specification when using the R package gdata

Tags:

r

perl

xls

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 in the gdata package, however the helper functions sheetNames and 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 and 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'.

and:

> 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
like image 944
ricardo Avatar asked Feb 01 '26 15:02

ricardo


1 Answers

It looks like ~ 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.

like image 143
jmcnamara Avatar answered Feb 04 '26 05:02

jmcnamara



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!