Command works in powershell but not via system()/system2()/shell()? - r

I'm looking to run a powershell command from R. it works in powershell, but I can't get it working in R.
This works in powershell
[guid]::NewGuid()
But none of these work from R
system("[guid]::NewGuid()", intern=TRUE)
system2("[guid]::NewGuid()")
shell("[guid]::NewGuid()")
Any ideas?

I think you need to tell system() that you want to use powershell and not cmd as executable.
Assuming powershell is in you PATH variable try
system('powershell -command "[guid]::NewGuid()"', intern=TRUE)
You can also try leaving intern=TRUE away depending what kind of output you expect.

Related

Run windows CMD in R

I am a beginner at R. If this question sounds childish to you then pardon me.
I need to run the following command in R studio by using Windows CMD.
copy *.txt combinedfiles.txt
This command actually combines all the files into one file in Windows CMD.
I tried using system() and system2(). But I could not get the results.
e.g.:
system(cmd.exe copy *.txt combinedfiles.txt)
Kindly help me out with this problem.
copy is not a standalone program, but a builtin command of the Windows commandline interpreter. To run this, use shell in R.
shell("copy *.txt combinedfiles.txt")

Space in argument causing errors in Windows Command Prompt

I'm trying to run an R script through the command prompt. My operating system is Windows 10. I'm having trouble running the code because there is a space in the file path of my argument. This is what I paste into the command prompt.
"C:\Program Files\R\R-3.4.3\bin\Rscript.exe" "C:\Users\Scott\Google Drive\RScriptsB\Bundle_Runner.R"
I get this error:
The filename, directory name, or volume label syntax is incorrect.
However, when I run it using a file path with no spaces, it runs fine.
"C:\Program Files\R\R-3.4.3\bin\Rscript.exe" "C:\Users\Scott\Desktop\Bundle_Runner.R"
The same behavior happens when I schedule the task through Task Scheduler: it doesn't work with the space, and it works when I remove the space from the file path.
I'm using Google Drive to sync work from multiple computers, so I'd like to be able to run my scripts using the file path with a space.
Any solutions?
FOR %%a IN ("C:\Users\Scott\Google Drive\RScriptsB\Bundle_Runner.R") DO "C:\Program Files\R\R-3.4.3\bin\Rscript.exe" %%~sa
would be my approach - the problem appears to be with R, not cmd.
Try this:
C:/PROGRA~1/R/R-3.4.3/bin/Rscript.exe "C:\Users\Scott\Desktop\Bundle_Runner.R"
Or
C:/PROGRA~1/R/R-3.4.3/bin/Rscript.exe C:/Users/Scott/Google~1/RScriptsB/Bundle_Runner.R
This is possibly related to an error reported at r-devel ("[Rd] Bug in RScript.exe for 3.5.0", https://stat.ethz.ch/pipermail/r-devel/2018-April/075869.html) that has been fixed the next day.
Perhaps the problem was already present in R 3.4.3 (you are using in your question).
Proposed workaround:
...add an extra first argument that has no space in it, e.g. Rscript --vanilla "foo bar.R"
To minimize the impact caused by --vanilla you could use
Rscript --no-save "foo bar.R"
instead (which just does not save the workspace at the end of the session).

System(Rscript ) not returning and error but not running either

I want to source an asynchronous background processes using System() and Rscript but it doesn't seem to run the script. The line I'm using is below:
system("Rscript -e 'source(\"/Users/Federico/Documents/R/win-library/3.4/taskscheduleR/extdata/PriceTesting.R\")'", wait=FALSE)
In the sourced script I have it write a simple csv and it does not write which leads me to believe its not running the script at all.
Am I doing something wrong?
Rscript already runs the script, so you can simply pass the path to the script as argument:
system("Rscript '/Users/colin/R/dslinr/plop.R'")
That being said, I have no pblm here running :
system("Rscript -e 'source(\"/Users/colin/R/dslinr/plop.R\")'", wait=FALSE)
Does the script works if you launch it from the shell? Are you sure your path is correct ? It seems to be mixing unix path (with /User), and win-library (windows library for R).
I changed it to this and it runs. All I can think of is that maybe Rscript isn't a recognized command.
system("C:/PROGRA~1/R/R-34~1.0/bin/Rscript.exe
C:/Users/Federico/Documents/R/win-
library/3.4/taskscheduleR/extdata/PriceTesting.R", wait=FALSE)

Cannot run R.exe in Powershell

I often find it more useful to run R on the command line (windows). However when I try it in Powershell I tend to run into problems, but this is easily overcome by first running cmd and then it works.
This is the error I get when I do R CMD BATCH
Invoke-History: A positional parameter cannot be found that accepts the argument 'BATCH'
I later realised that r is an alias that returns the immediate past command, hence my inability to run R.
Subsequently, I found that using the full filename for the executable (i.e. R.exe) or using Rcmd.exe (i.e. Rcmd BATCH ...) worked.
However, I'm just curious, is there a work around, in case one runs into similar conflicts?
To start R in powershell:
R.exe
The workaround would be fully defining your calls.
& "path\to the\r.exe" arg1 arg2 etc
Alternatively,
$P = #{
FilePath="path\to the\r.exe"
ArgumentList=#('arg1','arg2')
}
Start-Process #P
I ran into this problem on linux, where there is no file extension for executables, so that didn't help, but still there are two options:
/usr/bin/R if it is installed in the usual location
Start-Process R (the start-process cmdlet does not need to be capitalized)

Run two command line instructions from matlab

I want to run a R script from matlab.
I can run the R code perfectly from cmd using:
cd "C:\Program Files\R\R-3.1.3\bin\x64"
R CMD BATCH "C:\Users\name\Desktop\Code.R"
However in Matlab I am not sure how to run this two instructions.
First, I noticed that I could use:
system('cd "C:\Program Files\R\R-3.1.3\bin\x64"')
to run a commnand line command. However I need to run two. And making:
system('cd "C:\Program Files\R\R-3.1.3\bin\x64"')
system('R CMD BATCH "C:\Users\name\Desktop\Code.R"')
does not work.
I also saw this post about running multiple command line instructions in a single line, but also that did not work.
Anyone knows how to do it?
Your script should generally not care where it’s executed. So you don’t need the cd statement at all:
system('"C:\Program Files\R\R-3.1.3\bin\x64\R.exe" CMD BATCH "C:\Users\name\Desktop\Code.R"')
Be careful thought that the R path might not always be the same … it’s probably safer to find R’s location programmatically. Though how to do that in Matlab on Windows, I don’t know.
Furthermore, I honestly don’t really know why R CMD BATCH even exists but I strongly recommend using RScript instead. It works much nicer for a number of reasons.
The code then becomes:
system('"C:\Program Files\R\R-3.1.3\bin\x64\Rscript.exe" "C:\Users\name\Desktop\Code.R"')
Try to use dos command instead of system.

Resources