从目录创建zip文件时出现问题

时间:2018-08-24 17:03:57

标签: r zip

我无法从R创建zip文件。相同的代码可以完美地与32位R版本3.4.2一起使用。

现在,我尝试在R版本3.5.1(64位计算机)上运行相同的命令,并且zip()命令似乎不起作用。发生了什么事?

zip(zipfile = "test.zip",files=list.files(getwd())) 
#create zip from whole directory, on 1st machine it works, now nothing happens

我检查了zip()的源代码,并且在调试它时,发现system2命令没有任何作用。

  zip <- function (zipfile, files, flags = "-r9X", extras = "", zip = Sys.getenv("R_ZIPCMD", 
                                                                                  "zip")) 
  {
    if (missing(flags) && (!is.character(files) || !length(files))) 
      stop("'files' must a character vector specifying one or more filepaths")
    args <- c(flags, shQuote(path.expand(zipfile)), shQuote(files), 
              extras)
    if (.Platform$OS.type == "windows") 
      invisible(system2(zip, args))
    else invisible(system2(zip, args))
  }

# I run this manually when trying to debug, nothing happens;
system2(zip, args) ## zip is a parameter here, not a function
####
Browse[2]> zip
[1] "zip"
Browse[2]> args
[1] "-r9X"                    "\"bla.zip\""            
[3] "\"[Content_Types].xml\"" "\"_rels\""              
[5] "\"docProps\""            "\"xl\""                 
[7] "" 

例如,荒谬的通话不会给出错误。

system2("blablađ",2) ## does nothing but no error or warning either

我一直在努力了解system2()函数的工作原理以及创建压缩文件夹需要更改的内容。

谢谢

编辑:从评论中获得帮助后,出现以下错误:

Browse[2]> system2(zip, args,stderr = T)
Error in system2(zip, args, stderr = T) : '"zip"' not found

已解决:在安装适用于3.5版的Rtools后,它可以正常工作。

1 个答案:

答案 0 :(得分:2)

zip帮助中:

  

zip(zipfile,files,flags =“ -r9X”,extras =“”,       zip = Sys.getenv(“ R_ZIPCMD”,“ zip”))

     

zip一个字符串,指定要使用的外部命令。

如您所见,zip函数具有参数zip,用于指定要使用的外部命令。在我的机器上是:

λ where zip
C:\Oracle\Ora11\BIN\zip.exe
C:\Program Files\Rtools\bin\zip.exe

zip程序在Rtools中可用,但通常在任何(Windows?)计算机上也可用。

要检查R是否找到zip,请输入:

> Sys.which("zip")
                              zip 
"C:\\Oracle\\Ora11\\bin\\zip.exe" 

如果得到"",则意味着zip不在路径中,如果环境变量R_ZIPCMD中也没有zip,则必须在{{ 1}}参数。

相关问题