2020-11-09 14:30:09 +01:00
|
|
|
#' @title Convert Matlab function to R
|
2020-11-09 15:00:07 +01:00
|
|
|
#' @description Performs basic syntax conversion from Matlab to R
|
2020-11-09 14:30:09 +01:00
|
|
|
#' @param filename name of the file
|
2020-11-19 12:15:16 +01:00
|
|
|
#' @param output can be "asis", "clean" (default), "save" or "append"
|
2020-11-19 10:48:33 +01:00
|
|
|
#' @param improve_formatting if `TRUE` (default), makes minor changes
|
|
|
|
|
#' to conform to best-practice formatting conventions
|
2020-11-19 12:15:16 +01:00
|
|
|
#' @param change_assignment if `TRUE` (default), uses `<-` as the assignment operator
|
|
|
|
|
#' @param append if `FALSE` (default), overwrites file; otherwise, append
|
|
|
|
|
#' output to input
|
2020-11-09 15:00:07 +01:00
|
|
|
#' @return text converted to R, printed to screen or replacing input file
|
2020-11-09 14:30:09 +01:00
|
|
|
#' @author Waldir Leoncio
|
2020-11-19 07:49:30 +01:00
|
|
|
#' @importFrom utils write.table
|
2020-11-09 14:30:09 +01:00
|
|
|
#' @export
|
2020-11-19 12:15:16 +01:00
|
|
|
#' @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).
|
2020-11-19 10:48:33 +01:00
|
|
|
matlab2r <- function(
|
2020-11-19 12:15:16 +01:00
|
|
|
filename, output = "clean", improve_formatting=TRUE, change_assignment=TRUE,
|
|
|
|
|
append=FALSE
|
2020-11-19 10:48:33 +01:00
|
|
|
) {
|
2020-11-19 12:15:16 +01:00
|
|
|
# TODO: this function is too long! Split into subfunctions
|
|
|
|
|
# (say, by rule and/or section)
|
2020-11-19 08:45:50 +01:00
|
|
|
# ======================================================== #
|
|
|
|
|
# Verification #
|
|
|
|
|
# ======================================================== #
|
2020-11-09 14:30:09 +01:00
|
|
|
if (!file.exists(filename)) stop("File not found")
|
2020-11-19 08:45:50 +01:00
|
|
|
|
|
|
|
|
# ======================================================== #
|
|
|
|
|
# Reading file into R #
|
|
|
|
|
# ======================================================== #
|
2020-11-09 14:30:09 +01:00
|
|
|
txt <- readLines(filename)
|
2020-11-19 08:45:50 +01:00
|
|
|
|
|
|
|
|
# ======================================================== #
|
|
|
|
|
# Replacing text #
|
|
|
|
|
# ======================================================== #
|
|
|
|
|
|
2020-11-19 10:48:33 +01:00
|
|
|
# Uncommenting ------------------------------------------- #
|
|
|
|
|
txt <- gsub("^#\\s?(.+)", "\\1", txt)
|
|
|
|
|
|
2020-11-19 08:45:50 +01:00
|
|
|
# Function header ---------------------------------------- #
|
2020-11-19 10:48:33 +01:00
|
|
|
out <- gsub(
|
2020-11-19 12:15:16 +01:00
|
|
|
pattern = "\\t*function (\\S+)\\s*=\\s*(.+)\\((.+)\\)",
|
2020-11-19 10:48:33 +01:00
|
|
|
replacement = "\treturn(\\1)",
|
|
|
|
|
x = txt[1]
|
2020-11-19 12:15:16 +01:00
|
|
|
) # TODO: improve by detecting listed outputs
|
2020-11-09 15:00:07 +01:00
|
|
|
txt <- gsub(
|
2020-11-19 10:48:33 +01:00
|
|
|
pattern = "\\t*function (.+)\\s*=\\s*(.+)\\((.+)\\)",
|
|
|
|
|
replacement = "\\2 <- function(\\3) {",
|
2020-11-09 15:00:07 +01:00
|
|
|
x = txt
|
|
|
|
|
)
|
2020-11-19 08:45:50 +01:00
|
|
|
txt <- gsub(
|
|
|
|
|
pattern = "function (.+)\\((.+)\\)",
|
|
|
|
|
replacement = "\\1 <- function(\\2) {",
|
|
|
|
|
x = txt
|
|
|
|
|
)
|
2020-11-19 10:48:33 +01:00
|
|
|
|
|
|
|
|
# Function body ------------------------------------------ #
|
|
|
|
|
txt <- gsub("(.+)\\.\\.\\.", "\\1", txt)
|
2020-11-09 14:30:09 +01:00
|
|
|
txt <- gsub(";", "", txt)
|
2020-11-19 10:48:33 +01:00
|
|
|
|
|
|
|
|
# Loops and if-statements
|
2020-11-09 14:30:09 +01:00
|
|
|
txt <- gsub("for (.+)=(.+)", "for (\\1 in \\2) {", txt)
|
2020-11-19 10:48:33 +01:00
|
|
|
txt <- gsub("end$", "}", txt)
|
2020-11-09 15:22:56 +01:00
|
|
|
txt <- gsub("if (.+)", "if (\\1) {", txt) # FIXME: paste comments after {
|
|
|
|
|
txt <- gsub("else$", "} else {", txt)
|
|
|
|
|
txt <- gsub("elseif", "} else if", txt)
|
|
|
|
|
txt <- gsub("while (.+)", "while \\1 {", txt)
|
2020-11-19 10:48:33 +01:00
|
|
|
|
|
|
|
|
# MATLAB-equivalent functions in R
|
|
|
|
|
txt <- gsub("gamma_ln", "log_gamma", txt)
|
2020-11-19 12:15:16 +01:00
|
|
|
txt <- gsub("nchoosek", "choose", txt)
|
|
|
|
|
txt <- gsub("isempty", "is.null", txt)
|
|
|
|
|
# txt <- gsub("(.+)\\'", "t(\\1)", txt)
|
2020-11-19 10:48:33 +01:00
|
|
|
|
|
|
|
|
# Subsets ------------------------------------------------ #
|
2020-11-19 12:15:16 +01:00
|
|
|
ass_op <- ifelse(change_assignment, "<-", "=")
|
|
|
|
|
txt <- gsub(
|
|
|
|
|
pattern = "([^\\(]+)\\(([^\\(]+)\\)=(.+)",
|
|
|
|
|
replacement = paste0("\\1[\\2] ", ass_op, "\\3"),
|
|
|
|
|
x = txt
|
|
|
|
|
)
|
|
|
|
|
txt <- gsub("\\(:\\)", "[, ]", txt)
|
|
|
|
|
txt <- gsub("(.+)(\\[|\\():,end(\\]|\\()", "\\1[, ncol()]", txt)
|
2020-11-19 10:48:33 +01:00
|
|
|
|
|
|
|
|
# Formatting --------------------------------------------- #
|
|
|
|
|
if (improve_formatting) {
|
|
|
|
|
txt <- gsub("(.),(\\S)", "\\1, \\2", txt)
|
|
|
|
|
# Math operators
|
|
|
|
|
txt <- gsub("(\\S)\\+(\\S)", "\\1 + \\2", txt)
|
|
|
|
|
txt <- gsub("(\\S)\\-(\\S)", "\\1 - \\2", txt)
|
|
|
|
|
txt <- gsub("(\\S)\\*(\\S)", "\\1 * \\2", txt)
|
|
|
|
|
# Logic operators
|
2020-11-19 12:15:16 +01:00
|
|
|
txt <- gsub("~", "!", txt)
|
|
|
|
|
txt <- gsub("(\\S)>=(\\S)", "\\1 >= \\2", txt)
|
|
|
|
|
txt <- gsub("(\\S)<=(\\S)", "\\1 <= \\2", txt)
|
|
|
|
|
txt <- gsub("(\\S)==(\\S)", "\\1 == \\2", txt)
|
2020-11-19 10:48:33 +01:00
|
|
|
# Assignment
|
2020-11-19 12:15:16 +01:00
|
|
|
txt <- gsub(
|
|
|
|
|
pattern = "(\\w)(\\s?)=(\\s?)(\\w)",
|
|
|
|
|
replacement = paste0("\\1 ", ass_op, " \\4"),
|
|
|
|
|
x = txt
|
|
|
|
|
)
|
|
|
|
|
# txt <- gsub(
|
|
|
|
|
# pattern = "(\\s+(.|\\_|\\[|\\])+)(\\s?)=(\\s?)(.+)",
|
|
|
|
|
# replacement = paste0("\\1 ", ass_op, "\\5"),
|
|
|
|
|
# x = txt
|
|
|
|
|
# )
|
2020-11-19 10:48:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Adding output and end-of-file brace -------------------- #
|
|
|
|
|
txt <- append(txt, paste(out, "\n}"))
|
|
|
|
|
|
|
|
|
|
# Returning converted code ------------------------------- #
|
|
|
|
|
if (output == "asis") {
|
|
|
|
|
return(txt)
|
|
|
|
|
} else if (output == "clean") {
|
2020-11-09 14:30:09 +01:00
|
|
|
return(cat(txt, sep="\n"))
|
2020-11-19 12:15:16 +01:00
|
|
|
} else if (output == "save") {
|
2020-11-09 14:30:09 +01:00
|
|
|
return(
|
|
|
|
|
write.table(
|
|
|
|
|
x = txt,
|
|
|
|
|
file = filename,
|
|
|
|
|
quote = FALSE,
|
|
|
|
|
row.names = FALSE,
|
2020-11-19 12:15:16 +01:00
|
|
|
col.names = FALSE,
|
|
|
|
|
append = append
|
2020-11-09 14:30:09 +01:00
|
|
|
)
|
|
|
|
|
)
|
2020-11-19 12:15:16 +01:00
|
|
|
} else {
|
|
|
|
|
stop ("Invalid output argument")
|
2020-11-09 14:30:09 +01:00
|
|
|
}
|
2020-11-09 15:00:07 +01:00
|
|
|
}
|