Mrtrix package in R

Just an FYI - I have been adapting MRTrix for a port in R

We have been adapting neuroimaging packages to our Neuroconductor platform (

I have a working installation on Mac OSX and Linux, but the compile time is a bit high.

Let me know if this would be useful. I’m starting to adapt function from the initial tutorial (, such as dwi2mask, dwi2response, dwi2fod, and tckgen, and then moving to the other, lower-level functions. Let me know if have any feedback on an issue/PR

Thanks - always nice to see people using MRtrix3 in different ways.

Just a couple of questions though:

  • Quite a few years ago, I’d added support for compiling MRtrix3 as an R module, together with Sascha Frydman from University of Queensland. All it takes is to run configure with the -R switch, and then run build. Have you looked into that? This was ~5 years ago, mind you, so the chances of it still working out of the box are probably quite low… And I’m not aware of anyone currently using that. But I reckon it wouldn’t take an enormous amount of effort to bring it up to date if you were interested.

  • How is your port implemented exactly? You mention that you need to port each command one by one? I think the support we’d added covered all commands directly, without needing to manually port each one… But my recollection of this are very hazy.

I see that in the configuration, but it looks like it’s hard coded to a Linux build (using /usr/include/R).

I am building with .configure -R now to see what this produces for OS X.

Configuring OS-dependent flags

I think the code: include_dir=`Rscript -e "cat(R.home('include'))"` would produce a system-dependent includes. You may need to use Rscript.exe for Windows, but for Linux/OSX this should get the flags set:

export R_CFLAGS="-isystem `Rscript -e "cat(R.home('include'))"`"
export R_LDFLAGS="-L`Rscript -e "cat(R.home('lib'))"` -lR"

But you get the following error when running ./build:

ERROR: (  2/366) [CC] tmp/src/dwi/directions/file.o

clang++ -c -std=c++11 -DMRTRIX_MACOSX -fPIC -mmacosx-version-min=10.12 -DMRTRIX_WORD64 -isystem /usr/local/include -DMRTRIX_TIFF_SUPPORT -isystem /Library/Frameworks/GTK+.framework/Versions/2.24.X11/Resources/include -DEIGEN_FFTW_DEFAULT -isystem /usr/local/Cellar/fftw/3.3.6-pl2/include -isystem /Library/Frameworks/R.framework/Resources/include -DMRTRIX_AS_R_LIBRARY -Wall -O3 -DNDEBUG -Isrc -I./core -Icmd -isystem /usr/local/Cellar/eigen/3.3.4/include/eigen3 -DEIGEN_DONT_PARALLELIZE src/dwi/directions/file.cpp -o tmp/src/dwi/directions/file.o

failed with output

In file included from src/dwi/directions/file.cpp:15:
In file included from src/dwi/directions/file.h:18:
In file included from ./core/math/sphere.h:24:
In file included from ./core/math/math.h:24:
In file included from ./core/file/ofstream.h:19:
/Applications/ error: no member named 'Rf_error' in 'std::__1::codecvt_base'
        if (__r == codecvt_base::error)
/Library/Frameworks/R.framework/Resources/include/R_ext/Error.h:52:15: note: expanded from macro 'error'
#define error Rf_error
1 error generated.

ERROR: (  1/366) [CC] tmp/cmd/labelconvert.o

clang++ -c -std=c++11 -DMRTRIX_MACOSX -fPIC -mmacosx-version-min=10.12 -DMRTRIX_WORD64 -isystem /usr/local/include -DMRTRIX_TIFF_SUPPORT -isystem /Library/Frameworks/GTK+.framework/Versions/2.24.X11/Resources/include -DEIGEN_FFTW_DEFAULT -isystem /usr/local/Cellar/fftw/3.3.6-pl2/include -isystem /Library/Frameworks/R.framework/Resources/include -DMRTRIX_AS_R_LIBRARY -Wall -O3 -DNDEBUG -Isrc -I./core -Icmd -isystem /usr/local/Cellar/eigen/3.3.4/include/eigen3 -DEIGEN_DONT_PARALLELIZE cmd/labelconvert.cpp -o tmp/cmd/labelconvert.o

failed with output

In file included from cmd/labelconvert.cpp:15:
./core/command.h:33:3: error: use of undeclared identifier 'SET_MRTRIX_PROJECT_VERSION'
In file included from cmd/labelconvert.cpp:16:
In file included from ./core/image.h:23:
In file included from ./core/header.h:26:

Note, though, that './configure && ./build` works just fine without the R flag.

As an R Package

I don’t know what the output would be, but I’d want components of an R package (such as a DESCRIPTION file, a NAMESPACE file, etc.) so that someone could install MRTrix3 using install.packages("mrtrix") in R. The goal is to have all the options coded into R functions so that you’d be able to run dwi2response("/path/to/diffusion.nii.gz", bvals, bvecs,...) and then R would call the corresponding function (either at the command line or from a .Call to C/C++ code.

Does ./configure -R produce something like that?

Does ./configure -R produce an actual R package?

Sorry, forgot to follow up…

As far as i can tell, it produces a series of shared libaries (*so) in the bin/ folder. I’ve no idea how that relates to an actual R package - this was all a very long time ago, and I don’t use R regularly.

I’ve had a go at updating the code so it compiles successfully, at least on my Arch Linux system - see this branch. I haven’t tested it in any way though, but maybe you’ll be in a better position than me to figure this out…?