在R中使用shell()运行.bat文件 [英] Run .bat file using shell() in R
问题描述
我使用PDFSam的控制台来分割和合并一些PDF文件。我可以半自动使用.bat文件,但我想做的整个事情在R.
I'm using the console of PDFSam to split and merge some PDF files. I can do this semi-automatically using .bat files, but I would like to do the whole thing in R.
这个代码在我的.bat文件工作原理: p>
This code in my .bat file works:
C:
cd "/Program Files/pdfsam/bin/"
run-console.bat -f "d:/delete/A_9.pdf" -o d:/delete -s BURST -overwrite split
但是这个等效代码在我的R shell命令不返回任何错误,但似乎不工作。
But this "equivalent" code in my R shell command returns no errors, but doesn't seem to work.
shell('C:
cd "/Program Files/pdfsam/bin/"
run-console.bat -f "d:/delete/A_9.pdf" -o d:/delete -s BURST -overwrite split')
在shell命令中缺少一个选项?我尝试了一些在shell中列出的选项无效。
Is there an option I'm missing in my shell command? I've tried a few of the options listed in ?shell to no avail.
我使用Windows XP和
R版本2.13.1 -07-08)
平台:i386-pc-mingw32 / i386(32位)
I'm using windows XP and R version 2.13.1 (2011-07-08) Platform: i386-pc-mingw32/i386 (32-bit)
感谢,
Tom
Thanks, Tom
推荐答案
您可以将多个命令连接到&
工作:
You could pass multiple commands to shell by concatenating them by &
, so below should work:
shell('C: & cd C:/Program Files/pdfsam/bin & run-console.bat -f "d:/delete/A_9.pdf" -o d:/delete -s BURST -overwrite split')
$ b b
但是,作为解决方法,您可以临时更改R工作目录:
But as a workaround you could temporary change R working directory:
current.wd <- getwd()
setwd("C:/Program Files/pdfsam/bin")
shell('run-console.bat -f "d:/delete/A_9.pdf" -o d:/delete -s BURST -overwrite split')
setwd(current.wd)
函数:
shell.in.dir <- function(dir, ...) {
current.wd <- getwd()
on.exit(setwd(current.wd))
setwd(dir)
shell(...)
}
shell.in.dir("C:/Program Files/pdfsam/bin",
'run-console.bat -f "d:/delete/A_9.pdf" -o d:/delete -s BURST -overwrite split')
这篇关于在R中使用shell()运行.bat文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!