Skip to content

Interfacing with R

Hyatt Moore IV edited this page Aug 14, 2018 · 19 revisions
  1. Background
  2. Setup
  3. Testing
  4. Troubleshooting

Background

Padaco provides a means of calling R scripts through a background command line or system interface calls. The MATLAB file which wraps the calls to R scripts and returns command line output is rcall.m which is located in Padaco's tools directory.

Syntax

output=rcall(filename);

  • filename

    Absolute or relative filename of the R script to be called. If left blank, the demonstration file test1.R will be used.

  • output

    Optional output, which will contain any command line output resulting from calling the R script filename. R scripts produce command line output with function calls like cat() and print.

The semicolon (;) suppresses MATLAB from displaying the result of rcall on the command window. Remove the semicolon to see the result of output immediately.

output=rcall(filename)

  • filename

    Absolute or relative filename of the R script to be called.

  • output

    Optional output, which will contain any command line output resulting from calling the R script filename. R scripts produce command line output with function calls like cat() and print.

Setup

R and RScript must be installed on the same platform as Padaco. For instructions on installing R, please visit the R home page or go directly to one of their installation mirrors. For information on RScript and its installation for various operating systems, please see the R installation page.

Mac OSX
  • /usr/local/bin/
  • Alternatively: /Library/Frameworks/R.framework/Resources/
Windows
  • R_HOME/bin/Rscript.exe
  • R_HOME/bin/i386 or R_HOME/bin/x64
Linux
/usr/lib/RScript

Once R is installed, it's installation location must be identified with Padaco. IF Padaco is unable to find R's executable at the default location it will then prompt the user to locate it through a file explorer window. R's executable location is saved in the file tools/rscript.path and does not need to be identified again. In the event that a new version of R is installed at a different location, you may either edit the rscript.path file and replace the current location with the new absolute file location or delete the rscript.path file and Padaco will lead you through the process of identifying R's executable location as before.

Testing

MATLAB command line

  1. Open MATLAB and navigate to Padaco's home directory.
  2. Change to the tools path: cd tools;
>> cd tools;
  1. Run the interface script without any arguments: rcall
>> rcall

ans =

    'Test 1 complete'

>>

Troubleshooting

  • Error message: File or directory not found

  • Possible reason: R is not in your environment path.

  • Solution: To verify R is setup correctly for command line use, open a terminal window or command prompt (Windows) and type R and return on the keyboard. If the R program launches, then this is not the problem you are facing. If instead you get the message that the R program cannot be found then it is either not installed, or has not been added to your environment path. See the following Stack Overflow post for further direction in this case.

  • Rscript execution error: No such file or directory

  • Possible reason: R and Rscript are for different versions, and Rscript is not able to find the version of R that it is expecting.

  • Verification steps:

    • Run Rscript from the command line with the --verbose option to determine which R version is being called.
    • Verify that the R version is indeed at the location specified.
    • If it is not you can either fix the issue by trying the steps above: a clean install of R. Or you can remove the Rscript alias and create a new alias (symbolic link) in its place which identifies the correct Rscript file.

Clone this wiki locally