Executing macOS command lines on Matlab

10 visualizaciones (últimos 30 días)
federico nutarelli
federico nutarelli el 16 de Nov. de 2022
Comentada: Walter Roberson el 11 de Sept. de 2023
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

Respuestas (1)

Suraj
Suraj el 11 de Sept. de 2023
Editada: Suraj el 11 de Sept. de 2023
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 comentario
Walter Roberson
Walter Roberson el 11 de Sept. de 2023
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.
My post that @Suraj linked to discusses the work-arounds.

Iniciar sesión para comentar.

Categorías

Más información sobre Search Path en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by