From pdalgd at gmail.com  Thu Oct 29 11:44:24 2015
From: pdalgd at gmail.com (peter dalgaard)
Date: Thu, 29 Oct 2015 11:44:24 +0100
Subject: [Rd] R CMD BATCH vs R CMD batch
In-Reply-To: <m2oafiau08.fsf@krugs.de>
References: <CAM3-Kjagdzcya2ecCm=1Q7roFnNsKH+RR0Ryg3JEWPGU1b=4VA@mail.gmail.com>
	<22065.32480.203040.942634@max.nulle.part>
	<m2y4emax15.fsf@krugs.de>
	<CADfFDC4wMVsEeZHQd0QoHvQDWT1JOizL0p+DQM=7JgBy40ZS_Q@mail.gmail.com>
	<m2oafiau08.fsf@krugs.de>
Message-ID: <7AF88AA2-B815-4CB3-825A-5D7AF9D8CFC6@gmail.com>


On 29 Oct 2015, at 10:44 , Rainer M Krug <Rainer at krugs.de> wrote:

> Out of interest: What is the magic ~R CMD~ is doing? Is it documented
> anywhere?


R is open source.... (and shell scripts are considered self-documenting by some)

On Unix-alikes, R is a shell script which, if called with 1st argument CMD, calls ${R_HOME}/bin/Rcmd, which is another shell script that ends with 

case "${1}" in
## this was a separate command prior to 2.10.0
  Rd2txt)
    cmd="${R_HOME}/bin/Rdconv"
    extra="-t txt"
    ;;
## removed in 2.15.0
  Rd2dvi)
    echo "R CMD Rd2dvi is defunct: use Rd2pdf instead"
    exit 1
    ;;
  *)
    if test -x "${R_HOME}/bin/${1}"; then
      cmd="${R_HOME}/bin/${1}"
    else
      cmd="${1}"
    fi
    ;;
esac
shift

exec "${cmd}" ${extra} "${@}"

I.e., except for setting up variables and such, R CMD ${foo} essentially looks for ${R_HOME}/bin/${foo} and executes it if it exists and is executable, otherwise it just executes ${foo}.

Notice that, somewhat contrary to what Dirk said, this logic works at the mercy of the file system when it comes to case sensitivity. On OSX with the default case-insensitive setup we get

Peters-iMac:IBP pd$ R CMD install
Error: ERROR: no packages specified

which comes from R's package installer, whereas a case-sensitive FS would pick up /usr/bin/install instead. That is just how the "test" shell built-in works: 

Peters-iMac:~ pd$ test -x `R RHOME`/bin/INSTALL ; echo $?
0
Peters-iMac:~ pd$ test -x `R RHOME`/bin/UNSTALL ; echo $?
1
Peters-iMac:~ pd$ test -x `R RHOME`/bin/install ; echo $?
0


-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



[Rd] R CMD BATCH vs R CMD batch
peter dalgaard