使用R包gdata时的路径规范

时间:2022-12-07 12:49:00

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

问题:当使用gdata将.xls读入R时,helper函数失败

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.

我有一些。xls,我想读到r,我可以使用read。在gdata包中的xls,但是helper函数sheetNames和sheetCount失败了——我想了解我做错了什么,以便我可以使用它们,因为它们非常有用。

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:

当我知道这些表的结构,并且能够将它指向我想要的数据表时,perl脚本运行得很好:

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

and i have my data... in firstdata.

我有我的数据…在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.

我在excel中打开。xls,算出它们,然后用read加载到R中。xls(fp, sheet=N)——不是灾难,但也不理想。

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?

特别是,当这些表不是我自己的时,我需要关于它们的信息,然后才能在read.xls()中设置sheet=N,那么helper函数sheetNames和sheetCount将非常有用,尽管它们失败了——为什么?

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

1 个解决方案

#1


3  

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.

看起来~并没有扩展到您的主目录。这种扩展通常由shell完成,所以R可能不会这样做,perl肯定不会。

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

相反,可以在Perl程序中使用显式路径或$HOME或$ENV{HOME}。

#1


3  

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.

看起来~并没有扩展到您的主目录。这种扩展通常由shell完成,所以R可能不会这样做,perl肯定不会。

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

相反,可以在Perl程序中使用显式路径或$HOME或$ENV{HOME}。