R system or system2 command using paste or sprintf to run bat file?

孤街浪徒 提交于 2019-12-25 01:17:08

问题


I have a bat file that I can run from R using the following system command:

system('"C:\\PROGRA~1\\THERMO~1\\AFFYME~1\\APT-12~1.5\\bin\\apt-vars.bat" && cd "C:\\Users\\phyca\\Desktop\\R7\\R7_10-~1" && apt-cel-convert.exe --f text --i --cel-files CEL_FILE.txt')

But when I try to automate my whole process, I can't get it to trigger. The line returns without error, but it doesn't trigger the script.

#set apt location
apt_cnvrt <- "C:/Program Files/Thermo Fisher Scientific/Affymetrix Power Tools/APT-1.20.5/bin/apt-vars.bat"
#convert path format
apt_cnvrt <- gsub("/", "\\\\", apt_cnvrt)
#get short path
apt_cnvrt <- shortPathName(apt_cnvrt)
#store wd
outdir <- getwd()
#create file with list of files in dir
cel_list <- list.files(path = outdir, full.names = F, pattern = ".CEL")
fileConn<-file(paste0(outdir,"/CEL_FILE.txt"))
writeLines(c("cel_files", cel_list), fileConn)
close(fileConn)
#convert path format
outdir <- gsub("/", "\\\\", outdir)
#get short path
outdir <- shortPathName(outdir)
#set apt command
command <- "apt-cel-convert.exe --f text --i --cel-files CEL_FILE.txt"
#paste variables together to make: system('"C:\\Progra~1\\Thermo~1\\Affyme~1\\APT-1.20.5\\bin\\apt-vars.bat" && cd "C:\\Users\\me\\Desktop\\test" && apt-cel-convert.exe --format text --in-place --cel-files CEL_FILE.txt')
path <- noquote(paste0("'\"",apt_cnvrt,"\"", " && cd \"",outdir,"\" && ", command,"'"))
system(path)

Path looks like this when all is said and done, and it matches the command that works exactly....but it won't trigger the script:

'"C:\\PROGRA~1\\THERMO~1\\AFFYME~1\\APT-12~1.5\\bin\\apt-vars.bat" && cd "C:\\Users\\me\\Desktop\\test" && apt-cel-convert.exe --f text --i --cel-files CEL_FILE.txt'

I've tried various versions of paste, paste0, noquote, sprintf, system, system2, shell....etc, but can't get any of them to accept the variable to trigger the script.


回答1:


I was able to get system to accept the following:

system(sprintf("\"%s\" && cd \"%s\" && %s", apt_cnvrt, outdir, command))

The code runs to completion now as long as I am running it inside R studio.



来源:https://stackoverflow.com/questions/58616143/r-system-or-system2-command-using-paste-or-sprintf-to-run-bat-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!