2
process=StartProcess[$SystemShell];
outputFile=FileNameJoin[{NotebookDirectory[],"output.txt"}];
ClearAll[run];
run[commands_,seconds_:2]:=Module[{},
    commandSent=commands<>" > \""<>outputFile<>"\"";
    WriteLine[process,commandSent];
    Pause[seconds];
    Import[outputFile]
];

$rDirectory="C:\\Program Files\\R\\R-3.3.0\\bin\\x64";

now runnning

run["cd \""<>$rDirectory<>"\""]

and

run["dir"]

produces the same result as running on cmd directly but running

run["R.exe"]

doesn't seem to start the R process. But if i do these directly on cmd it works.

I have used this method several times to connect to external processes but it is not working for R. Someone please help!!!

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
user13892
  • 9,375
  • 1
  • 13
  • 41
  • Run is for single execution while i want to keep sending a sequence of commands programmatically and retrieve results. Can you please check whether i am the only one having this problem or there is some issue? – user13892 Sep 15 '18 at 17:49
  • Oh sorry. Forget about what I said. I thought it was about a typo... – Henrik Schumacher Sep 15 '18 at 17:51
  • 1
    But you might be interested in RLink... – Henrik Schumacher Sep 15 '18 at 17:53
  • Stupid RLink crashes for large data. And has other bugs. I want the program to run in R but queries generated programmatically since there are alot of combinations. – user13892 Sep 15 '18 at 17:55
  • Do i need to add it to "PATH" since i am cd-ing to the directory which has the executable? – user13892 Sep 15 '18 at 17:56
  • process=StartProcess[$SystemShell]; is starting the cmd not as an admin. Do you know how to start the process as an admin? – user13892 Sep 15 '18 at 20:31
  • If you don't mind the notebook being your terminal, you may try Import[commandstring,"Text"], e.g., cmdstr = "!R.exe something", then Import[cmdstr,"Text"] – xieyn Sep 16 '18 at 01:44
  • @xieyn does your command work for windows? It is not working for me. – user13892 Sep 16 '18 at 08:53
  • Does anyone know how to get the process object of a running process like a running cmd. Which will allow me to run R explicitly there and send commands via the process object. – user13892 Sep 16 '18 at 08:55
  • Perhaps make a post for RLink crashing with large data. That should return some useful results for half of this question. – Edmund Sep 16 '18 at 12:22
  • @user13892 Ah, it's weird. Import is not working with R. I had used Import to call another external program and it worked perfectly. RunProcess works for me, for example, RunProcess[{"c:\\Program Files\\R\\R-3.5.1\\bin\\Rscript.exe", "test.r"}, "StandardOutput"], but this may be not what you want... – xieyn Sep 16 '18 at 12:58
  • "RLink crashes for large data. And has other bugs." - Can you please report those? Either through the standard mechanism (tech.support), or even just via separate questions here. There are some bugs for the installation procedure, that are being dealt with, but I have not seen reports for crashing with big data transfers yet. Thanks. – Leonid Shifrin Sep 16 '18 at 20:56
  • @LeonidShifrin I will create a concrete example for RLink failing with a dataset and add as a separate question. But can you please help me why this simple approach of calling R from cmd running as a subprocess of the wolfram kernel is not working here. – user13892 Sep 18 '18 at 11:27
  • Thanks. Re: command shell - off the top of my head, you need an extra "!" sign in front of your command if you use Import, see e.g. here for more details. If that doesn't help, I can try looking into this later. – Leonid Shifrin Sep 18 '18 at 12:36

0 Answers0