2016-03-02 73 views
1

我希望使用R(版本3.2.2)來讀取壓縮爲.Z文件的多個文件。我瞭解uncompress()已在最近的版本中刪除。我非常感謝,如果任何人都可以讓我知道我可以如何使用R來解壓.Z文件的如何解壓縮.Z文件

+0

你使用的是什麼操作系統?你可能需要通過調用另一個工具來使用'system()'... https://kb.iu.edu/d/abck,http://superuser.com/questions/190053/universal-command-line -un-storage-tool-on-a-mac –

+0

嗨,本,我使用Windows 7 – mikeqfu

回答

1

我知道我遲到了這個問題,但我正在四處尋找,看看有什麼更好的建議比我們在發現您的問題時所做的更好。正如Ben所建議的那樣,調用另一個工具對於Windows來說可能是最好的選擇,Linux和OS X可以本地處理.Z文件。

下面是一個函數示例(不是我寫的,Ivan Hanigan寫的,但是我已經使用了它,所以我知道它的工作原理)在R包中檢查操作系統,然後查找7Zip如果操作系統是Windows。

https://github.com/swish-climate-impact-assessment/awaptools/blob/master/R/ZipFunctions.R

################################################################ 
# name:ZipFunctions.R 
uncompress_linux <- function(filename) 
    { 
    print(filename) 
    system(sprintf('uncompress %s',filename)) 
    } 

# tries to find 7 zip exe 
ExecutableFileName7Zip <- function() 
{ 
    executableName <- "C:\\Program Files\\7-Zip\\7z.exe" 

    if(file.exists(executableName)) 
    { 
    return (executableName) 
    } 

    #other executable file names and ideas go here ... 
    stop("failed to find 7zip") 
} 

# simple function to extract 7zip file 
# need to have 7zip installed 
Decompress7Zip <- function(zipFileName, outputDirectory, delete) 
{ 
    executableName <- ExecutableFileName7Zip() 

# fileName = GetFileName(zipFileName) 
# fileName = PathCombine(outputDirectory, fileName) 


# if(file.exists(fileName)) 
# { 
#  unlink(zipFileName); 
# } 

    arguments <- paste(sep="", 
        "e ", 
        "\"", zipFileName, "\" ", 
        "\"-o", outputDirectory, "\" ", 
    "") 

    print(arguments) 

    RunProcess(executableName, arguments) 

    if(delete) 
    { 
    unlink(zipFileName); 
    } 
} 
+0

非常感謝您的回答。這也是我當時的工作。我想,雖然可能不是唯一的,但可能是R在Windows上處理.Z文件的最有效方式。乾杯。 – mikeqfu

1

您可以嘗試安裝最後存檔的版本。 (請注意,uncompress是一個已歸檔的程序包,而不是用於基本R中已被棄用/刪除的函數...)。您需要在計算機上安裝合適的開發工具(C編譯器,make)[例如見#2 here]。

library("devtools") 
install_version("uncompress","1.34") 

這乾淨安裝了我R的開發版本,但我還沒有真正嘗試過任何.Z文件,因爲我沒有他們躺在周圍的人。

+0

謝謝本。這似乎不適用於我。我嘗試按照您的建議安裝開發工具,但收到消息說「錯誤:無法找到構建解壓縮所需的構建工具」。 – mikeqfu