2014-04-07 122 views
1

是否有任何函數可以將.numbers(Apple的電子表格程序)文件導入R?我可以從gdata包中使用read.xls導入.xlsx文件。但使用read.xls不適用於.numbers文件。將.numbers電子表格導入到R

+0

根據快速網頁搜索,這個軟件應該能夠創建xls文件。它可能也可以將數據導出爲文本文件? – Roland

+0

這就是我一直在做的,但寧願保留所有。數字文件 – luciano

回答

1

沒有正式的包和@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 

那不是壽可擴展性。

另外,數字已經恢復AppleScript的支持,它有一個export調用,因此,從理論上講,它是污垢簡單的創建一個文件夾動作或命令行腳本採取一個或多個.numbers文件,並把他們的CSV。這也可能是一種在R中編寫一個薄的「填充」模塊或函數的方法,它只是在幕後執行(ee read.numbers("myfile.numbers", table=1)然後將該數字表格導出並將第一個表格導出爲CSV文件,然後使用read.csv進行讀取它在)。

UPDATE

爲此,這裏是由(@plang)https://stackoverflow.com/users/1982991/plang]another SO post與最新版本的數字作品的腳本的修改。將它放到read.numbers()函數/包中並不困難,但不是我想要構建的東西,因爲它需要處理Numbers數據如何從具有多個表格/工作表的文檔中保存CSV文檔的太多邊緣條件。

用於Numbers的當前AppleScript字典將使它非常可能直接從腳本執行打開和讀取表(這仍然需要使用pbpaste黑客,因爲我不認爲存在R < - > AppleScript橋)。這可以通過Java < - > AppleScript橋來完成,但由於需要直接使用Numbers文檔的人數很少,這看起來似乎是過度殺傷性的。

我仍然建議將腳本(如下)轉換爲文件夾操作,並將需要在R中使用的Numbers文件拖放到批量轉換中。

# - input: Numbers input file 
# - output: CSV output file 
# 
# Attik System, Philippe Lang 
# 
# Orig from: https://stackoverflow.com/a/10845335/1457051 
# 
# 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