1

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
owalt
  • 13
  • 2

1 Answers1

1

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}
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • Thank you so much for your help. I am super close to getting this! – owalt May 11 '22 at 18:16
  • I am trying to run this in my makevars file `R_VERSION = Rscript -e 'cat(gsub("(\\.\\d)?$", "", format(getRversion())))'` yet it's not returning the re-formated R version. I works if I just `Rscript -e 'cat(format(getRversion()))'` – owalt May 11 '22 at 18:16
  • Note the backticks in my code example. – Dirk Eddelbuettel May 11 '22 at 18:29
  • I included the back ticks! It just won't show in my comment R_VERSION = `Rscript -e 'cat(gsub("(\\.\\d)?$", "", format(getRversion())))'` – owalt May 11 '22 at 18:31
  • ^This does not work – owalt May 11 '22 at 18:57
  • There are tricks in going from shell (which my edit showed) to `make`. I edited the answer again. – Dirk Eddelbuettel May 11 '22 at 19:18
  • `Rscript -e 'cat(gsub("(\\\\\.\\\\\d)?$$", "", format(getRversion())))'` adding an extra slash, respectively, made it work. Thank you thank you thank you – owalt May 11 '22 at 21:08
  • Glad it worked, and yes, it is a mess: dual dollar signs, espaped slashes become quadruple escaped, you have to be careful what variable expands where ... but when it works it will work unaltered for years :) Also thanks for accecpting, please feel free to upvote (for which you need more than the most minimal karma). But I upvoted your question now that it all makes sense so that may help... – Dirk Eddelbuettel May 11 '22 at 21:51