R - 使用系统命令参数化解压缩文件
R - Parameterized unzipping of file using system command
我对使用 R 解压大文件的速度慢感到失望:
unzip("C:/My File.zip", exdir="C:/")
所以我写了一个系统命令:
system('powershell -command "Expand-Archive -Path "C:/My File.zip" -DestinationPath "C:/" -Force"')
这个命令工作得很好,但是现在我想参数化输入文件,我 运行 在构造命令时遇到反斜杠转义字符的问题:
in_file <- "C:/My File.zip"
command <- paste0("\'powershell -command \"Expand-Archive -Path \"", in_file, "\" -DestinationPath \"C:/\" -Force\"'")
虽然 cat(command)
看起来像我想要的字符串:
cat(command)
> 'powershell -command "Expand-Archive -Path "C:/My File.zip" -DestinationPath "C:/" -Force"'
当我 运行 system(command)
我得到一个错误:
Warning message:
running command ''powershell -command "Expand-Archive -Path "C:/My File.zip" -DestinationPath "C:/" -Force"'' had status 127
我把这归结为命令实际上没有像我想要的那样运行,因为转义字符仍然存在:
print(command)
> "'powershell -command \"Expand-Archive -Path \"C:/My File.zip\" -DestinationPath \"C:/\" -Force\"'"
我做了各种尝试来删除反斜杠,例如gsub("\","",str, fixed=TRUE)
但我就是做不到。
我怎样才能使我的解压缩命令起作用?
mt1022 让我走上正轨。对于这个post的补全,答案是使用:
paste0('powershell -command "Expand-Archive -Path """', in_file, '""" -DestinationPath """', out_file, '""" -Force"')
我对使用 R 解压大文件的速度慢感到失望:
unzip("C:/My File.zip", exdir="C:/")
所以我写了一个系统命令:
system('powershell -command "Expand-Archive -Path "C:/My File.zip" -DestinationPath "C:/" -Force"')
这个命令工作得很好,但是现在我想参数化输入文件,我 运行 在构造命令时遇到反斜杠转义字符的问题:
in_file <- "C:/My File.zip"
command <- paste0("\'powershell -command \"Expand-Archive -Path \"", in_file, "\" -DestinationPath \"C:/\" -Force\"'")
虽然 cat(command)
看起来像我想要的字符串:
cat(command)
> 'powershell -command "Expand-Archive -Path "C:/My File.zip" -DestinationPath "C:/" -Force"'
当我 运行 system(command)
我得到一个错误:
Warning message:
running command ''powershell -command "Expand-Archive -Path "C:/My File.zip" -DestinationPath "C:/" -Force"'' had status 127
我把这归结为命令实际上没有像我想要的那样运行,因为转义字符仍然存在:
print(command)
> "'powershell -command \"Expand-Archive -Path \"C:/My File.zip\" -DestinationPath \"C:/\" -Force\"'"
我做了各种尝试来删除反斜杠,例如gsub("\","",str, fixed=TRUE)
但我就是做不到。
我怎样才能使我的解压缩命令起作用?
mt1022 让我走上正轨。对于这个post的补全,答案是使用:
paste0('powershell -command "Expand-Archive -Path """', in_file, '""" -DestinationPath """', out_file, '""" -Force"')