Skip to content
JanineEgert edited this page Jun 11, 2021 · 13 revisions

Rcall: Calling R functions from Matlab

Description

Rcall provides a function interface for direct access to R packages within Matlab. R commands are collected in a temporary R script which is then executed via command line. Rcall works on all standard operating systems such as Windows, Linux and Mac and enables automated execution of R commands in a Matlab environment.

Usage

Rinit([Rpackages, Rpath, Rlibpath])

Rpush(varname,var)

Rrun(Rcommand)

Rpull(varstr)

Rclear

Arguments

Rpackages – cell strings of the R package names which to attach (alternatively the R packages can be defined in Rrun(‘library(Rpackage)’))

Rpath – string describing the location of the R.exe installation

Rlibpath – string describing the location of R library trees to search through

varname - string describing the variable name which is declared as following argument

var – variable which is passed to R

Rcommand – string of the R command which is word by word written into the executed R script

varstring – string of the variable name to be loaded

Implementation

Once the Rcall folder is added to the Matlab path, Rcall can be used in any Matlab script. The usual application of Rcall is to prefer performing the analysis in R or to compare the results of both programming languages. Using Rcall involves passing all necessary data to R, performing computations according to the user input, and sending the results back to Matlab for further use. In total, the Rcall application consists of five main steps, which are shown in the following Figure.

Limitations

Rcall does not support R functions which require user input. Graphical visualizations can be forwarded to a file, however support of graphics dependens on the user's system.

Examples

To illustrate the use of Rcall, a differential expression and clustering analysis is performed on an example data set. Log2-normally distributed data is simulated with 10,000 genes in 2 groups with 3 replicates in which 10% are differentially expressed:

1 rng(0)
2 grp = [0; 0; 0; 1; 1; 1];
3 dat = 2.^(randn(10000,6));
4 dat(1:1000,4:6) = dat(1:1000,4:6)+2;

First, R is initialized and the R packages are set to 'R.matlab' (Bengtsson and Jacobson. 2018) and 'limma' (Ritchie et al. 2015).

5 Rinit('limma')

Then, the variables are passed to Matlab with the Rpush command in line 6 and the R commands are defined (line 6-12). The input strings have the same effect as typing the R commands directly in R. In this example, a linear model is fitted. The results of the linear fit and the empirical Bayes statistics are stored in the MArrayLM object 'fitBay'. However, just the p-value 'p' of the differential expression analysis is passed to Matlab.

6 Rpush('dat',dat,'grp',grp)
7 Rrun('fit <- lmFit(dat,design=model.matrix(~1+grp))')       		
8 Rrun('fitBay <- eBayes(fit)')
9 Rrun('p <- fitBay$p.value')
10 Rrun('tiff("Volcano.tiff")')
11 Rrun('volcanoplot(fitBay,coef=2,highlight=3)')							
12 Rrun('dev.off()')
13 p = Rpull('p');

With the Rpull function in line 13 the R script is evaluated and the results are stored and transferred back to Matlab.

As a second example, a clustering analysis is performed on the same data:

14 Rrun('tiff("ClusterArrhythmia.tiff")')
15 Rrun('h <- heatmap(dat,row=NA)')
16 Rrun('cl <- h$colInd')
17 Rrun('dev.off()')
18 cl = Rpull('cl');

The index permutations and dendograms of the gene clustering are stored in the list 'h'. But just the column indices 'cl' of the clustering analysis are passed back to Matlab.

In the end, all temporary variables and R scripts are cleared.

19 Rclear

References

Ritchie, M. E., Phipson, B., Wu, D., Hu, Y., Law, C. W., Shi, W., and Smyth, G. K. (2015). limma powers differential expression analyses for RNA-sequencing and microarray studies. Nucleic Acids Research, 43(7), e47. DOI 10.1093/nar/gkv007.

Bengtsson, H. and Jacobson, A. (2018). R.matlab: Read and Write MAT Files and Call MATLAB from Within R. URL [https://CRAN.R-project.org/package=R.matlab] (https://CRAN.R-project.org/package=R.matlab).

Clone this wiki locally