Executing macOS command lines on Matlab
27 次查看(过去 30 天)
显示 更早的评论
Hi all,
I am trying to adapt the following code by Weirong Chen to MacOs.
function RunRcodemac(RscriptFileName,Rpath)
% This function calls R to run R script (.r file) under Matlab, and returns 'runR.log' in the same folder with the input R script file.
% This code only works in Windows environments. It might work in Mac by modifying 'FindRpath'.
% 'RscriptFileName' : path + filename of the R script to be run.
% 'Rpath' (optional) : the path for the installed 'R.exe'. e.g. Rpath = 'C:\Program Files\R\R-3.1.1\bin';
% If 'Rpath' is not provided, 'FindRpath' will be executed to automatically find the path for R in Windows Program Files folder.
% Example:
% >> Rpath = 'C:\Program Files\R\R-3.1.1\bin';
% >> RscriptFileName = 'D:\test\lmm.R';
% >> RunRcode(RscriptFileName, Rpath);
% Update:
% Ver. 1.4 Dec-14-2017 support parallel computing (run several R codes simultaneously)
% Weirong Chen March-8-2015
if nargin<2 || isempty(Rpath), Rpath=FindRpath;end
sep=filesep;
[p,f,~]=fileparts(RscriptFileName);
if isempty(p), p = pwd;end
logFName=[p sep f '.R.log'];
%oldcommandline=['"' Rpath sep 'R.exe" CMD BATCH "' RscriptFileName '" "' logFName '"'];
newcommandline=['/usr/local/bin/Rscript ' RscriptFileName];
system(newcommandline);
end
Specifically, my idea is to modify the commandline variable in order to pass it to system and run the equivalent of bash: Rscript mypath/myfile.R.
Since I am quite new to this, I would like to ask you two questions:
1) is there a way to locate the path of the Rscript function from matlab? Specifically, I found out the path "/usr/local/bin/" by typing in terminal "which Rscript". I tried to type system("which Rscript") or simply which "Rscript" in matlab but nothing happens;
2) Is there a way to call the equivalent of Rscript mypath/myfile.R using system function (or any other function) in MAtlab?
Thank you
0 个评论
回答(1 个)
Suraj
2023-9-11
编辑:Suraj
2023-9-11
Hi Federico,
I understand that you're trying to run a R script using the "system" command in MATLAB. The approach that you mentioned should work fine, let me just attach a code snippet for the same.
% Find out the installation path for Rscript
[status, result] = system('which Rscript');
rscriptPath = strtrim(result);
% Compose the command to run the desired script
newcommandline = [rscriptPath ' ' RscriptFileName];
system(newcommandline);
In some cases, though, when the "which" command is run through MATLAB, Rscript may not be located. For a possible workaround to this please refer to: https://www.mathworks.com/matlabcentral/answers/328024-bin-bash-r-command-not-found-when-trying-to-run-r-from-matlab-mac#answer_257239
I hope this helps!
Thanks & regards,
Suraj.
1 个评论
Walter Roberson
2023-9-11
On MacOS, R is usually not installed into one of the directories that is included in the default path provided to executables that are lauched by icon. Executables launched by icon do not go through login shell processes, so any ~/.bash_profile or similar will not have been executed for them. Chances are that the PATH that will have been provided to MATLAB will only include /usr/bin:/bin:/usr/sbin:/sbin -- and especially with newer releases, MacOS really does not want you to modify those directories yourself, so it makes it difficult to do that.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Search Path 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!