From 228b4d808ac6647b71a603e417eaf09c9bad1147 Mon Sep 17 00:00:00 2001 From: Waldir Leoncio Date: Mon, 9 Nov 2020 15:00:07 +0100 Subject: [PATCH] Fixed syntax --- R/initialPopCounts.R | 4 ++-- R/matlab2r.R | 13 ++++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/R/initialPopCounts.R b/R/initialPopCounts.R index a865243..2cb4806 100644 --- a/R/initialPopCounts.R +++ b/R/initialPopCounts.R @@ -6,11 +6,11 @@ initialPopCounts <- function(data, npops, rows, noalle, adjprior) { for (i in 1:npops) { for (j in 1:nloci) { i_rivit <- rows(i, 1):rows(i, 2) - havainnotLokuksessa <- find(data(i_rivit, j) >= 0) + havainnotLokuksessa <- find(data[i_rivit, j] >= 0) sumcounts(i, j) <- length(havainnotLokuksessa) for (k in 1:noalle[j]) { alleleCode <- k - N_ijk <- length(find(data(i_rivit, j) == alleleCode)) + N_ijk <- length(find(data[i_rivit, j] == alleleCode)) counts(k, j, i) <- N_ijk } } diff --git a/R/matlab2r.R b/R/matlab2r.R index 12403d0..c54bde7 100644 --- a/R/matlab2r.R +++ b/R/matlab2r.R @@ -1,8 +1,8 @@ #' @title Convert Matlab function to R -#' @description Performs basic syntax conversion +#' @description Performs basic syntax conversion from Matlab to R #' @param filename name of the file #' @param saveOutput if `TRUE`, `filename` is overwritten. Defaults to `FALSE` -#' @return text converted to R +#' @return text converted to R, printed to screen or replacing input file #' @author Waldir Leoncio #' @export matlab2r <- function(filename, saveOutput = FALSE) { @@ -11,9 +11,16 @@ matlab2r <- function(filename, saveOutput = FALSE) { # Reading file into R txt <- readLines(filename) # Replacing text + txt <- gsub( + pattern = "function (.+)\\s*=\\s*(.+)\\((.+)\\)", + replacement = "\\2 <- function(\\3) { return(\\1)", + x = txt + ) txt <- gsub(";", "", txt) txt <- gsub("for (.+)=(.+)", "for (\\1 in \\2) {", txt) txt <- gsub("end", "}", txt) + # TODO: replace forms like (:,:) with [, ] + # TODO: reformat if statements # Returning converted code if (!saveOutput) { return(cat(txt, sep="\n")) @@ -28,4 +35,4 @@ matlab2r <- function(filename, saveOutput = FALSE) { ) ) } -} \ No newline at end of file +}