我在从 R 创建 zip 文件时遇到问题。相同的代码在 R 版本 3.4.2、32 位计算机上完美运行。
\n\n现在我尝试在 R 版本 3.5.1、64 位计算机上运行相同的东西,并且 zip() 命令似乎不起作用。到底是怎么回事?
\n\nzip(zipfile = "test.zip",files=list.files(getwd())) \n#create zip from whole directory, on 1st machine it works, now nothing happens\nRun Code Online (Sandbox Code Playgroud)\n\n我检查了 zip() 的源代码,当我调试它时,我发现 system2 命令什么也没做。
\n\n zip <- function (zipfile, files, flags = "-r9X", extras = "", zip = Sys.getenv("R_ZIPCMD", \n "zip")) \n {\n if (missing(flags) && (!is.character(files) || !length(files))) \n stop("\'files\' must a character vector specifying one or more filepaths")\n args <- c(flags, shQuote(path.expand(zipfile)), shQuote(files), \n extras)\n if (.Platform$OS.type == "windows") \n invisible(system2(zip, args))\n else invisible(system2(zip, args))\n }\n\n# I run this manually when trying to debug, nothing happens;\nsystem2(zip, args) ## zip is a parameter here, not a function\n####\nBrowse[2]> zip\n[1] "zip"\nBrowse[2]> args\n[1] "-r9X" "\\"bla.zip\\"" \n[3] "\\"[Content_Types].xml\\"" "\\"_rels\\"" \n[5] "\\"docProps\\"" "\\"xl\\"" \n[7] "" \nRun Code Online (Sandbox Code Playgroud)\n\n例如,荒谬的调用不会给出错误。
\n\nsystem2("blabla\xc4\x91",2) ## does nothing but no error or warning either\nRun Code Online (Sandbox Code Playgroud)\n\n我一直试图了解 system2() 函数如何工作以及我需要更改什么才能创建压缩文件夹。
\n\n谢谢
\n\n编辑:在使用评论的帮助后,我收到以下错误:
\n\nBrowse[2]> system2(zip, args,stderr = T)\nError in system2(zip, args, stderr = T) : \'"zip"\' not found\nRun Code Online (Sandbox Code Playgroud)\n\n已解决:安装 Rtools 3.5 版后,它可以正常工作。
\n来自zip帮助:
\n\n\nzip(zipfile, 文件, 标志 = "-r9X", extras = "",\n zip = Sys.getenv("R_ZIPCMD", "zip"))
\n\nzip 指定要使用的外部命令的字符串。
\n
正如您所看到的,该zip函数有一个参数zip来指定要使用的外部命令。在我的机器上是:
\xce\xbb where zip\nC:\\Oracle\\Ora11\\BIN\\zip.exe\nC:\\Program Files\\Rtools\\bin\\zip.exe\nRun Code Online (Sandbox Code Playgroud)\n\n该zip程序可在 Rtools 中使用,但通常也可在任何(Windows?)机器上使用。
要检查zipR 是否找到,请输入:
> Sys.which("zip")\n zip \n"C:\\\\Oracle\\\\Ora11\\\\bin\\\\zip.exe" \nRun Code Online (Sandbox Code Playgroud)\n\n如果得到"",则意味着zip不在路径中,如果它不在环境变量中R_ZIPCMD,则必须在zip参数中指定其路径。