rcppr-packagercpparmadilloarrayfire

Detecting R Version in Rcpp/arrayfire in makevars


I am new to building R packages so I need some help :) I am using Rcpp/arrayfire and want a line in my makevars file to detect the users R version. Currently I have it set in 4.0, but I anticipate users having different R versions.

If this question has been answered, I apologize for not finding one!

Here are my global variables in the makevars file

R_VERSION = 4.0
AF_CXXFLAGS = -I/opt/arrayfire/include
AF_LIBS   = -L/opt/arrayfire/lib -laf -Wl,-rpath,/opt/arrayfire/lib /Library/Frameworks/R.framework/Versions/$(R_VERSION)/Resources/library/RcppArrayFire/libs/RcppArrayFire.so -Wl,-rpath,/Library/Frameworks/R.framework/Versions/$(R_VERSION)/Resources/library/RcppArrayFire/libs


Solution

  • The usual workflow is to use a script called configure (which can be written in any language) which 'detects this' and then writes or alters src/Makevars accordingly.

    If you know a little about make or want to learn it you can also do in a Makefile -- and our script src/Makevars is one. So something like this saved in a file Makefile

    RVER = `Rscript -e 'cat(R.Version()$$major)'`
    
    SOMEDIR = "/opt/foo/bar/"${RVER}"/some/more"
    
    all:
        @echo Using ${SOMEDIR}
    

    results in

    $ make 
    Using /opt/foo/bar/4/some/more
    $ 
    

    Edit And if you wanted just "4.2" out of the version, one way might be

    > gsub("(\\.\\d)?$", "", format(getRversion()))
    [1] "4.2"
    > 
    

    Edit 2 As a full Makefile it becomes

    #RVER = `Rscript -e 'cat(R.Version()$$major)'`
    RVER = `Rscript -e 'cat(gsub("(\\\\.\\\\d)?$$", "", format(getRversion())))'`
    
    SOMEDIR = "/opt/foo/bar/"${RVER}"/some/more"
    
    all:
        @echo Using ${SOMEDIR}