Search code examples
rcommand-linefasttext

R system call failing due to arithmetic operator


I am trying to mimic the results of a fasttext command line operation in R using the system function. When I run the prompt in the command line I get the desired results as below:

F:\>fasttext print-sentence-vectors model.bin < sentences.txt
   0.072631 0.036104 0.0046829 0.057456 0.03449

When I try to mimic this in R I get the following:

> system("fasttext print-sentence-vectors model.bin < sentences.txt")
  usage: fasttext print-sentence-vectors <model>

  <model>      model filename

  [1] 1

I believe the issue is the requirement of "<" in the command line since when I remove it from the command line prompt I get

F:\>fasttext print-sentence-vectors model.bin sentences.txt
usage: fasttext print-sentence-vectors <model>

<model>      model filename

Same as what I get in R. So my question is, how do I go about resolving this in R ? I tried various versions with the paste function as well as wrapping the < in quotes but nothing seemed to work.

Tried shell() as per a comment below and got this error message

> shell("fasttext print-sentence-vectors model.bin < sentences.txt")
'\\mypathname\fasttextstuff'
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported.  Defaulting to Windows directory.
The system cannot find the file specified.
Warning message:
In shell("fasttext print-sentence-vectors model.bin < sentences.txt") :
  'fasttext print-sentence-vectors model.bin < sentences.txt' execution failed with error code 1

Turns out shell() worked as per the comment below with a directory change.


Solution

  • The redirection symbol < is handled by your command shell, not by fasttext. It looks as though you are on Windows, and on that OS R doesn't run a shell when running system(). Use shell() instead.

    After that failed, this fixes it: make sure that your current directory uses a drive letter (e.g. D:), not a UNC path, which CMD.EXE doesn't like.