2016-03-02 246 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