-
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?
Conversation
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 comment
The 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 evaluatec()
fail because the Matlab code is not complete?
I thought it might be a good idea to re-use the matlab instance, but it's probably unnecessary. They start pretty fast.
You're right, I didn't think about multi-line expressions properly. This required a change to R.matlab (HenrikBengtsson/R.matlab#19). I have fixed the coding style. |
Hi, is this patch likely to be accepted? |
Yes, as long as a multi-line expression can be written in separate lines like this mat$exec(c('x = 1;', 'while x < 10', 'disp(x);', 'x = x + 1;', 'end')) Otherwise I think you should just |
There are two choices here. Either every entry in the vector is required to be a complete evaluate-able expression, in which case it is possible to get out the result of each part of the command separately (as a string). Or we accept that the whole thing has to be run together (as in your example), in which case it is no longer possible to get out separate outputs for each part. You prefer the second approach? |
Ideally, we should parse the code and split it into multiple complete expressions, then evaluate these expressions one by one. See https://github.com/yihui/highr/blob/master/R/utils.R#L8-L23 for an example of grouping lines of R code into expressions. If that is not possible for Matlab, I'd just go with the second approach you mentioned, because this runr package was designed mainly for knitr, and when evaluating a Matlab code chunk in knitr, there is no way for users to specify which lines should be considered as a complete unit. |
I'd like to incorporate the ability to run matlab into knitr. As a step towards that I've added matlab support to runr. This uses code in HenrikBengtsson/R.matlab#18. The relevant knitr code will be
library(knitr)
library(runr)
matlab = proc_matlab(9999)
matlab$start()
knit_engines$set(matlab = function(options) {
out <- matlab$exec(options$code)
engine_output(options, options$code, out)
})