1

Is there a way in R to check if a command exists in the operating system?

I know that file.exists(file) can be used to check if a file exists. But what about a command on the system PATH?

(This is with Ubuntu 20.04, R 4.1).

Related:

How to check if object (variable) is defined in R?

How can I check if a directory exists in a Bash shell script?

thor
  • 20,736
  • 28
  • 83
  • 160

1 Answers1

1

If it is a command in PATH, then it is actually a file somewhere in the search path.

An easy way is

system("which <command>")  # for unix
system("where <command>")  # for windows

If the command exists, then this should show the full path. Otherwise nothing.

Kota Mori
  • 5,663
  • 17
  • 23
  • Are you sure this correct? On Windows, If I do `system("where dir")` I get "INFO: Could not find files for the given pattern(s)" but `system("which dir")` returns 127. – Maurits Evers Apr 14 '22 at 05:28
  • Oh, sorry. I will change the example. But `where` would work for commands in the PATH though? – Kota Mori Apr 14 '22 at 06:38