Performs basic syntax conversion from a Matlab function file to R
Usage
matlab2r(
filename,
output = "diff",
improve_formatting = TRUE,
change_assignment = TRUE,
append = FALSE,
restyle = !improve_formatting,
skip_lines = NULL
)
Arguments
- filename
name of the file
- output
can be "asis", "clean", "save" or "diff"
- improve_formatting
if
TRUE
(default), makes minor changes to conform to best-practice formatting conventions- change_assignment
if
TRUE
(default), uses<-
as the assignment operator- append
if
FALSE
(default), overwrites file; otherwise, append output to input- restyle
if
TRUE
, will restyle the output with styler (only foroutput = "save"
)- skip_lines
vector of lines to be skipped. These will be commented out and tagged as TODO, instead.
Note
This function is intended to expedite the process of converting a Matlab function to R by making common replacements. It does not have the immediate goal of outputting a ready-to-use function. In other words, after using this function you should go back to it and make minor changes.
It is also advised to do a dry-run with output = "clean"
and only switching
to output = "save"
when you are confident that no important code will be
lost (for shorter functions, a careful visual inspection should suffice).
Examples
matlab_script <- system.file("extdata", "matlabDemo.m", package = "matlab2r")
matlab2r(matlab_script)
#> Warning: Please pay special attention to parentheses. MATLAB uses them for both argument-passing and object-subsetting. The latter cases should be replaced by squared brackets.
#> Displaying line number, original content and modified content
#> ----------------------- line 1 -----------------------
#> function y = f(x)
#> f <- function(x) {
#> ----------------------- line 2 -----------------------
#> % This is a quick demonstration of MATLAB syntax. Its output is uninteresting.
#> # This is a quick demonstration of MATLAB syntax. Its output is uninteresting.
#> ----------------------- line 3 -----------------------
#> z = pi;
#> z <- pi
#> ----------------------- line 4 -----------------------
#> if (z == 0)
#> if ((z == 0)) {
#> ----------------------- line 5 -----------------------
#> z = 10;
#> z <- 10
#> ----------------------- line 6 -----------------------
#> else
#> } else {
#> ----------------------- line 7 -----------------------
#> disp('OK');
#> disp('OK')
#> ----------------------- line 8 -----------------------
#> end
#> }
#> ----------------------- line 9 -----------------------
#> for i = 1:10
#> for (i in 1:10) {
#> ----------------------- line 10 -----------------------
#> z2 = i - z;
#> z2 <- i - z
#> ----------------------- line 11 -----------------------
#> end
#> }
#> ----------------------- line 12 -----------------------
#> y = z + x - (1 * 3 / 9) ^ 2 + z2;
#> y <- z + x - (1 * 3 / 9) ^ 2 + z2
#> ----------------------- line 13 -----------------------
#> NA
#> return(y)
#> }
matlab2r(matlab_script, output = "clean")
#> Warning: Please pay special attention to parentheses. MATLAB uses them for both argument-passing and object-subsetting. The latter cases should be replaced by squared brackets.
#> f <- function(x) {
#> # This is a quick demonstration of MATLAB syntax. Its output is uninteresting.
#> z <- pi
#> if ((z == 0)) {
#> z <- 10
#> } else {
#> disp('OK')
#> }
#> for (i in 1:10) {
#> z2 <- i - z
#> }
#> y <- z + x - (1 * 3 / 9) ^ 2 + z2
#> return(y)
#> }