-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added code to call matlab #14
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ S3method(print,runr_results) | |
export(proc_bash) | ||
export(proc_julia) | ||
export(proc_python) | ||
export(proc_matlab) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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('5:9') # [ 5 6 7 8 9] | ||
#' mat$running() # should be TRUE | ||
#' mat$stop() | ||
#' } | ||
|
||
proc_matlab <- function(port = 6011, existing=FALSE){ | ||
require(R.matlab) | ||
matlab <- NULL | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll appreciate it if you use |
||
exec_code = function(...){ | ||
if (is.null(matlab)) stop('the process has not been started yet') | ||
code = as.character(c(...)) | ||
code = unlist(strsplit(code, "\n")) | ||
result <- sapply(code, function(x) evaluatec(matlab, x)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure this works? I mean what if an expression has multiple lines, then will |
||
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()) | ||
} | ||
Matlab$startServer(port=port) | ||
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() | ||
} | ||
) | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please avoid
require()
. Import functions from the package instead, or just useR.matlab::function()
. The latter seems to be more appropriate in this case.