[R] open connection to system

John McKown john.archie.mckown at gmail.com
Tue Jul 14 19:48:03 CEST 2015


On Tue, Jul 14, 2015 at 11:45 AM, Adrian Dușa <dusa.adrian at unibuc.ro> wrote:

> Dear list,
>
> Probably not the best subject line, but hopefully I can explain.
> I would like to use R and open a connection to a (system) command line base
> chess engine
> (for example, there is an open source one at stockfishchess.org)
>
> In the Terminal window (using MacOS), I can type two commands:
>
> $ ./stockfish-6-64     <-- this is the first command
> Stockfish 6 64 by Tord Romstad, Marco Costalba and Joona Kiiski
> go movetime 3000     <-- this is the second command
>
> (then lots of lines calculated by the engine, with a final answer after 3
> seconds)
>
> First command opens a connection to the chess engine, the seconds one tells
> it to search for a move.
> The question is, can I do this via R?
>
> I tried the system() command, which works with the first command:
> > system("./stockfish-6-64", intern=TRUE)
> [1] "Stockfish 6 64 by Tord Romstad, Marco Costalba and Joona Kiiski"
>
> but it closes the connection and returns an error if I attempt the second
> command:
>
> > system("./stockfish-6-64\ngo movetime 3000", intern=TRUE)
> Error in system("./stockfish-6-64\ngo movetime 3000", intern = TRUE) :
>   error in running command
> sh: line 1: go: command not found
>
>
> Any hint would be really appreciated, thanks in advance,
> Adrian
>
> --
> Adrian Dusa
> University of Bucharest
>

What system() does is run a command & wait for it to end. ​I take it you
are running on a Mac. Do you want to send multiple command to "stockfish",
or only one command? If the latter, you can do something like:

commands=c("go movetime 3000");
system("./stockfish-6-64",intern=TRUE,input=commands);

​If you want to send a number of commands, and not "interact" with the
"stockfish" command, you can:

commands=c("first command","second command"); # and so on
system("./stockfish-6-64",intern=TRUE,input=commands);

But if you want to "interact" with "stockfish", that's much more difficult
and I don't have any example available. I _think_ you'd need to look at
using mcparallel() and the "parallel" package. Or maybe the
socketConnection() function in some way.                                 ​



-- 

Schrodinger's backup: The condition of any backup is unknown until a
restore is attempted.

Yoda of Borg, we are. Futile, resistance is, yes. Assimilated, you will be.

He's about as useful as a wax frying pan.

10 to the 12th power microphones = 1 Megaphone

Maranatha! <><
John McKown

	[[alternative HTML version deleted]]



More information about the R-help mailing list