Home > OS >  How to check if an executable command exists in R
How to check if an executable command exists in R

Time:04-14

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?

CodePudding user response:

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.

  •  Tags:  
  • r
  • Related