Running a Powershell script from R using system2 (), not system ()

I have a PowerShell script (say in C: \ directoryName \ coolScript.ps1). If I want to call this from R, I can run

system('powershell -file "C:\\directoryName\\coolScript.ps1"') 

If I try to do the same with system2() , it does not return an error, but the script is not executed. Since the documentation for the system() command states that system2() "recommended for new code," I would like to use system2() . Is there any way to do this?

+7
source share
1 answer

Unlike system() , the command argument to system2() always enclosed in shQuote quotes, so it must be a single command with no arguments, while arguments are passed to command with the args argument.

They both work:

 system("sed -i 's/oldword\\s/oldword/g' d:/junk/x/test.tex") system2("sed", args=c("-i", "s/oldword\\s/newword/g", "d:/junk/x/test.tex")) 

I would try:

 system2("powershell", args=c("-file", "C:\\directoryName\\coolScript.ps1")) 

Another thing you should be aware of is that there are two versions of the R executable: R-3.2.1 \ bin \ i386 (32-bit) and R-3.2.1 \ bin \ x64 (64-bit version). By default, only the first is installed on 32-bit versions of Windows, but both are on 64-bit operating systems. The 32-bit version of R will invoke the 32-bit version of PowerShell for the same version for the 64-bit version, so be careful to use Set-ExecutionPolicy for the correct version.

+13
source

All Articles