2

I want to invoke some R-Code by a certain Rscript.exe via a Start-Process-cmdlet. To do this, a powershell variable is given as -e argument to Rscript in order to evaluate the code.

Steps to reproduce

Assuming the Powershell is opened in the bin-Folder of a R-Installation, I can pass code as argument:

$code = "print('Nospaces');Sys.sleep(12)"
Start-Process .\Rscript.exe -ArgumentList "-e", $code

But if the code contains any spaces whatsoever, the Rscript will fire an unexpected end of input-Error:

$code = "print('With spaces');Sys.sleep(12)"
Start-Process .\Rscript.exe -ArgumentList "-e", $code #it does not matter if code is in parenthesis or not
Start-Process .\Rscript.exe -ArgumentList "-e", "$code"

In this example, the space is located within a print statement, but hte space can be set anywhere within the code.

It does however work, if I do not invoke Rscript.exe via Start-Process:

.\Rscript.exe -e $code

Question

Is there a way to invoke Rscript by starting the process and still being able to use code containing spaces within the $code-Variable?

I want to invoke it via Start-Process in order not to block the Powershell script while executing the RScript.

Sandwichnick
  • 1,379
  • 6
  • 13
  • You may try it the other way around ... `$code = 'print("With spaces");Sys.sleep(12)'` – Olaf Sep 08 '21 at 12:03
  • Presumably Powershell is quoting the command line in a way that interferes with R's interpretation of it. I'd check the docs for Powershell quoting. – user2554330 Sep 08 '21 at 15:43

1 Answers1

3

RScript.exe is a console application, and the best way to invoke such applications - as in any shell - is by direct invocation - no need for Start-Process (see this answer for more information):

$code = "print('With spaces');Sys.sleep(12)"
.\Rscript.exe -e $code
  • PowerShell will automatically handle double-quoting of the $code value on demand, based on whether the value contains spaces or note.

  • As an aside (not required here): situationally, &, the call operator is required for direct invocation, namely if the executable name or path is quoted and/or contains variable references (e.g. & $rscriptPath ... or & 'C:\path to\RScript.exe' ...)

Caveat: While there's no problem in this particular case, even with such direct invocation, up to at least PowerShell 7.1, problems can arise with arguments that have embedded " characters and with empty-string arguments - see this answer for more information.


If you were to use Start-Process (not advisable here), you'd additionally have to:

  • pass -Wait to ensure synchronous execution - but note that you wouldn't be able to (directly) capture output from the invocation in PowerShell.

  • pass all arguments as a single string to -ArgumentList using embedded (escaped) double-quoting around your $code variable.

# Not advisable, but works in principle.
Start-Process -Wait .\Rscript.exe -ArgumentList "-e `"$code`""

While in an ideal world passing the arguments individually to -AgumentList - as you've tried - is undoubtedly preferable (or should at least work too), a long-standing bug unfortunately makes it better to encode all arguments in a single string, as shown - see this answer for more information.

mklement0
  • 382,024
  • 64
  • 607
  • 775