Skip to content
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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Conversation

rohan-shah
Copy link

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)
})

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))
Copy link
Owner

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.
@rohan-shah
Copy link
Author

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.

@rohan-shah
Copy link
Author

Hi, is this patch likely to be accepted?

@yihui
Copy link
Owner

yihui commented Oct 7, 2015

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 paste(code, collapse = '\n') and get rid of sapply().

@rohan-shah
Copy link
Author

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?

@yihui
Copy link
Owner

yihui commented Oct 7, 2015

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants