-
Notifications
You must be signed in to change notification settings - Fork 29
rcall_check
Let's assume you are programming a package for Stata and you wish to make use of an existing R package in your Stata package. There are several points that you need to pay an especial attention to:
- What is the minimum R version that you expect the users to connect to their
rcall
? - What packages the users must install in R to be able to use your program?
- What is the minimum required version for these R packages?
There are certainly more points that you need to consider when programming defensively, but for the very basic problems mentioned above, rcall
has a solution; Namely, a program called rcall_check
which checks for the version of the R, required packages, and their version.
The syntax of the rcall_check
program is as follows:
rcall_check [pkgname>=ver] [pkgname>=ver] [...] , rversion(ver)
Let's assume a user has R version 2.8.0 installed to his rcall
. But your Stata package requires R version 3.3.0 or newer. You can place the following code in your ado program to return a proper error:
. rcall_check , r(3.3)
R version 3.3 or higher is required. rcall is using R 2.8.0
invalid syntax
r(198);
rcall_check
returns the error automatically, which is convenient. Now let's assume that you need a particular R package.
Let's say, readstata13
, which is an R package required by rcall
for communicating data sets between Stata and R. Let's assume the user does not have this package installed. Or he has the package, but the package version is older than what you have assumed. You can return proper error to let the user know what R package he has to install and which of his R packages he must update to be able to use your package properly. In the following example, let's assume you want the user to know he should install readstata13
package:
. rcall_check readstata13
R package readstata13 is required
invalid syntax
r(198);
You can also specify the minimum required version of the package. Assuming he has version 0.7 installed:
. rcall_check readstata13>=0.8.0
R package readstata13 0.8.0 or newer is required
invalid syntax
r(198);
You can check for several R packages and the required R version at the same time. In the next example, I also check for the installation of the foreign
package:
. rcall_check readstata13>=0.8.0 foreign>=0.8 , rversion(3.3.0)