diff --git a/DESCRIPTION b/DESCRIPTION index b3f9cbf..36c44a2 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -11,6 +11,7 @@ Authors@R: c( person("Adam", "Lyon", role = "aut"), person("Yalei", "Du", role = "aut") ) +Suggests: R.matlab Maintainer: Yihui Xie License: GPL URL: https://github.com/yihui/runr diff --git a/NAMESPACE b/NAMESPACE index ee1af53..8ab5d1d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,3 +2,4 @@ S3method(print,runr_results) export(proc_bash) export(proc_julia) export(proc_python) +export(proc_matlab) \ No newline at end of file diff --git a/R/matlab.R b/R/matlab.R new file mode 100644 index 0000000..57218f7 --- /dev/null +++ b/R/matlab.R @@ -0,0 +1,59 @@ +#' Run a matlab process +#' +#' This function returns a list of functions to start/run/stop a matlab process. +#' The code is sent to matlab via a socket connection, and the results are +#' written back in another socket connection. +#' @param port A TCP port number +#' @return A list of functions \code{start()}, \code{exec()}, \code{running()} +#' (check if the process has been running), and \code{stop()}. +#' @export +#' @examples \dontrun{ +#' mat = proc_matlab() +#' mat$start() +#' mat$exec('1+1') +#' mat$exec('x = 1 + 1; x = x + x; x;') # return nothing +#' mat$exec('x = 1 + 1\n x = x + x\n x') # return nothing +#' mat$exec('5:9') # [ 5 6 7 8 9] +#' mat$exec('x = 1; while x < 10\n disp(x);\n x = x + 1;\n end') #Prints numbers 1 to 9 +#' mat$running() # should be TRUE +#' mat$stop() +#' } + +proc_matlab <- function(port = 6011){ + matlab <- NULL + exec_code = function(...){ + if (is.null(matlab)) stop('the process has not been started yet') + code = as.character(c(...)) + result = sapply(code, function(x) R.matlab::evaluate(matlab, x, capture=TRUE)) + return(do.call(paste, c(as.list(result), sep = "\n"))) + } + + list( + start = function() { + if (!is.null(matlab)) { + warning('the program has been started') + return(invisible()) + } + R.matlab::Matlab$startServer(port=port) + matlab <<- R.matlab::Matlab(port=port) + isOpen = open(matlab, trials=30, interval = 0, timeout = 1) + if(!isOpen) + { + stop("Unable to connect to matlab server") + } + invisible() + }, + + exec = exec_code, + + running = function() !is.null(matlab), + + stop = function() { + close(matlab) + matlab <<- NULL + invisible() + } + ) +} + +