Can't call cygwin commands from R - r

I'm working in windows box, and using cygwin to imitate unix shell. Also I have R and want to run any system comand from it. I believe that system('command') would do this, but it didn't work for each 'command'. For example system('ls') returns /cygdrive/c/cygwin/bin/ls: write error: Bad file descriptor and I think that this is beacause R calls ls >& - which also returns same error. So how solve this problem?

I hope you are still interested in a solution. Here is what I have:
Start R from Windows and call cygwin executable by path: system('C:/cygwin/bin/ls.exe');
Start R from within cygwin:
Start cygwin
Set path: export PATH="$PATH:/cygdrive/c/Programme/R-2.9.2/bin"
Call R: Rgui.exe
Call shell: system('ls')
Regards,
Martin.

Related

Error that says Rscript is not recognized as an internal or external command, operable program or batch file [duplicate]

shell_exec("Rscript C:\R\R-3.2.2\bin\code.R ");
This is the call to script.On calling the above script, the error occurs.
I am trying to call my R script from the above path but no output is being shown. While checking the error logs of PHP, it says 'Rscript' is not recognized as an internal or external command, operable program or batch file.' The script is working fine on the Rstudio but not running on the command line.
Add the Rscript path to your environment variables in Windows:
Go to Control Panel\System and Security\System and click Advanced System Settings, then environment variables, click on path in the lower box, edit, add "C:\R\R-3.2.2\bin"
Restart everything. Should be good to go. Then you should be able to do
exec('Rscript PATH/TO/my_code.R')
instead of typing the full path to Rscript. Won't need the path to your my_code.R script if your php file is in the same directory.
You need to set the proper path where your RScript.exe program is located.
exec ("\"C:\\R\\R-3.2.2\\bin\\Rscript.exe\"
C:\\My_work\\R_scripts\\my_code.R my_args";
#my_args only needed if you script take `args`as input to run
other way is you declare header in your r script (my_code.r)
#!/usr/bin/Rscript
and call it from command line
./my_code.r
If you are running it in Git Bash terminal, you could follow a revised version of the idea suggested by #user5249203: in the first line of your file my_code.R, type the following
#!/c/R/R-3.2.2/bin/Rscript.exe
I assumed that your path to Rscript.exe is the one listed above C:\R\R-3.2.2\bin. For anyone having a different path to Rscript.exe in Windows, just modify the path-to-Rscript accordingly. After this modification of your R code, you could run it in the Git Bash terminal using path-to-the-code/mycode.R. I have tested it on my pc.
I faced the same problem while using r the first time in VS Code, just after installing the language package (CRAN).
I restart the application and everything worked perfectly. I think restarting would work for you as well.

Problem with mismatched perl binaries using system2 command

I am trying to run an executable script from within RStudio. The script runs perfectly from Terminal, however, when trying to run it form within R I get the following error message:
setwd("/users/admin")
command <- "/users/admin/Test_script.command"
system2(command)
> lib/Bio/DB/HTS/Faidx.c: loadable library and perl binaries are mismatched (got handshake key 0xdb00080, needed 0xc500080)
I am running MacOS X Big Sur and I use perlbrew with perl version 5.26.2, I guess the problem is that RStudio tries to use my system perl version - but I couldn't find a way to change that…
Any ideas? Thanks!
I found a way to get it running by looking into the actual script, which is perl based. Then I was able to solve the problem by following the answer to https://stackoverflow.com/a/65553736/4253357 as Michal suggested. Thanks again for your help!

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)

How does R system() recognize command path?

How does R's built-in system() function know where to look to invoke some arbitrary OS command specified by the command argument? For example, if I homebrew install some_command_line_program, how does R's system() function know where it is located when I type:
cmd <- "some_complicated_code_from_some_command_line_program"
system(cmd, wait=FALSE)
In other words, how is R smart enough to know where to look without any user input? If I compile from source via Github (instead of homebrew install), would system() also recognize the command?
What system does depends on your OS, you've not told us (although you've given us some clues...).
On unix-alike systems, it executes it as a command in a bash shell, which searches for the first match in the directories on the $PATH environment variable. You can see what that is in R:
> Sys.getenv("PATH")
[1] "/usr/local/heroku/bin:/usr/local/heroku/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/nobackup/rowlings/NEST4B"
In Windows, it does something else.
You can get a full path to whatever it might run with Sys.which, which uses the systems' which command on unixes and fakes it on Windows. Read the help for more.
If you compile something from source then it will be found if the file that runs the command (a shell script, an executable, a #!-script in any language) is placed in a folder in your $PATH. You can create a folder yourself, say /home/user/bin, put your executables in there, add that to your $PATH, and (possibly after logging out an in again, or restarting R, or just firing up a new shell...) then R will find it.

calling Qiime with system call from R

Hej,
When I try to call QIIME with a system call from R, i.e
system2("macqiime")
R stops responding. It's no problem with other command line programs though.
can certain programs not be called from R via system2() ?
MacQIIME version:
MacQIIME 1.8.0-20140103
Sourcing MacQIIME environment variables...
This is the same as a normal terminal shell, except your default
python is DIFFERENT (/macqiime/bin/python) and there are other new
QIIME-related things in your PATH.
(note that I am primarily interested to call QIIME from R Markdown with engine = "sh" which fails, too. But I strongly suspect the problems are related)
In my experience, when you call Qiime from unix command line, it usually creates a virtual shell of it`s own to run its commands which is different from regular system commands like ls or mv. I suspect you may not be able to run Qiime from within R unless you emulate that same shell or configuration Qiime requires. I tried to run it from a python script and was not successful.

Resources