将.numbers电子表格导入R

3

有没有任何函数可以将.numbers(苹果的电子表格程序)文件导入R?我可以使用gdata包中的read.xls导入.xlsx文件。但是,使用read.xls无法处理.numbers文件。


根据快速的网络搜索,这个软件应该能够创建 xls 文件。可能它也可以将数据导出为文本文件? - Roland
这就是我一直在做的,但我更喜欢将所有内容保留为.numbers文件。 - luciano
1个回答

2

没有正式的包,@Roland 是正确的,你可能最好通过 File->Export->CSV 将数据导出。如果你只需要一个表格 - 例如:

enter image description here

那么你可以选择单元格并使用 pbpaste :

dat <- read.csv(pipe('pbpaste'), sep='\t')
dat
##       A   B
## 1     4 456
## 2     5 346
## 3     5 345
## 4    34 345
## 5     4 345
## 6    45 345
## 7    46 345
## 8  3456 345
## 9   678  34
## 10  568  34

但这不具有可扩展性。

或者,Numbers已经恢复了AppleScript支持,并且它有一个export调用,因此理论上创建一个文件夹操作或命令行脚本以将一个或多个.numbers文件转换为CSV文件将非常简单。这也可以是在R中编写一个薄的“shim”模块或函数的方法,该函数只会在幕后执行此操作(例如,read.numbers("myfile.numbers", table=1),然后将该Numbers表导出到CSV文件的第一个表,然后使用read.csv来读取)。

更新

为此,这里是(@plang)[https://stackoverflow.com/users/1982991/plang]另一个SO帖子修改的脚本,适用于最新版本的Numbers。将其放入read.numbers()函数/包中并不太困难,但我没有构建它的计划,因为它需要处理太多从具有多个表/工作表的文档保存CSV文档的边缘情况。

当前的Numbers AppleScript字典使得直接从脚本打开和读取表非常可能(在我看来仍然需要使用pbpaste hack,因为我认为没有R<->AppleScript桥)。它可以通过Java<->AppleScript桥完成,但考虑到需要直接使用Numbers文档的人口较少,这似乎有些过度。

我仍然建议将脚本(如下所示)转换为文件夹操作,并将您需要在R中使用的Numbers文件拖放到其中以进行批量转换。

# - input: Numbers input file
# - output: CSV output file
#
# Attik System, Philippe Lang
#
# Orig from: https://dev59.com/yGTWa4cB1Zd3GeqPDXFt#10845335
#
# Creation date: 31 mai 2012
# Modification date: 07-Apr-2014 by @hrbrmstr
# -------------------------------------------------------------------------------
on run argv
    # We retreive the path of the script
    set myPath to (path to me)
    tell application "Finder" to set myFolder to folder of myPath

    # We get the command line parameters
    set input_file to item 1 of argv
    set output_file to item 2 of argv

    # We retreive the extension of the file
    set theInfo to (info for (input_file))
    set extname to name extension of (theInfo)

    # Paths
    set input_file_path to (myFolder as text) & input_file
    set output_file_path to (myFolder as text) & output_file

    log input_file_path
    log output_file_path

    if extname is equal to "numbers" then
        tell application "Numbers"
            open input_file_path
            export document 1 as CSV to output_file_path
            close every window saving no
        end tell
    end if
end run

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接