R Compiler Tools For Rcpp On Macos

0 views
Skip to first unread message

Cheryll Witting

unread,
Aug 3, 2024, 3:46:11 PM8/3/24
to acafwini

As of yet, RangeShiftR has to be built from source bythe user. This requires the R-version 3.6.0 orhigher, the packages Rcpp 1.0.1 or higher andRdpack 0.7 or higher, as well as a functional C++toolchain (see below). Further, the package devtools isrecommended for an easy installation directly from github.

In order to build the package from source, we need to set up acompiler toolchain. This involves installing a suitable C++ compiler andmaking it available to R. The exact procedure depends onyour operating system as well as your R installation. Unix-based systemslike Linux or MacOS might have the toolchain set up already along withthe installation of R. You can find detailed instructions here: -rtools-for-compiled-code-via-rcpp/

on Windows: the Rtools package on CRAN contains allthe required tools. Find installing instructions for either manualinstallation (top) or from within R (further down) on thewebsite mentioned above. (Tip: Pay extra attention to steps10/11.)
If you have upgraded to R version 4, make sure toinstall the corresponding Rtools40. More information here: -project.org/bin/windows/Rtools/ (Tip: Payextra attention to the instructions on the bottom of the page, regardingthe path variable.)

This description assumes a clean install of Monterey (no previous install of rstan or the C++ Toolchain for rstan).It borrows extensively from -compiler-tools-for-rcpp-on-macos/If you do have a previous install of the C++ Toolchain for rstan, see this post to uninstall relevant files.

Session 2 of the workshop covers two applications of process features extraction (n-grams and longest common subsequence). We will go over these applications in an interactive R tutorial. Running the tutorial locally requires the ngram and qualV R package. It can be installed with the following R command.

We will introduce ProcData package in session 3. You are encouraged to install ProcData and dependent packages and libraries before the session. This section provides instructions on the installation of ProcData. If you need help during the installation process, please post your issue on our GitHub page. We will respond as soon as possible.

ProcData depends on R packages Rcpp for incorporating C++ code in R code and keras for training neural networks. If you would like to install ProcData from source, then compiler tools are needed. ProcData calls Python library Keras for building and training neural networks in Python. Some functions in ProcData require Python to work. Note that the following two steps do not have to be performed before installing ProcData. You will be prompted if R detects a missing component.

Session 4 of the workshop covers several applications of process features to practical testing problems. We will go over these applications in an interactive R tutorial. Running the tutorial locally requires the learnr R package. It can be installed with the following R command.

The above command should return the directory that contains make.exe, for example, "C:\\rtools40\\usr\\bin\\make.exe". If this is what you get, you are all set. Otherwise, please make sure you have followed the Rtools installation guidelines at -project.org/bin/windows/Rtools/ and restart R before checking again.

Please try to source the src_partial_dif.R R script. If there are no error messages, you are set for Session 3. If you encounter the undefined reference to dgemm error message, chances are gfortran is missing from the system, or you are using an old version that conflicts with the latest R version. In this case, try the following:

It is worth taking a moment to explore VS Code for a moment. Since you installed the R extension, you have access to an interactive R terminal. You can access it by first opening the terminal window with Terminal -> New Terminal.

This example R package has a function named buggy_init() that we are going to be trying out. You give it a single integer size, and it should create a double vector of that size filled with 2s. If you try it out in RStudio, you should get something like this:

On the left hand side you will see the files in the package directory. This is the Explorer window. If you open the src/ directory and then open code.cpp, you can see the C++ file that contains buggy_init().

Then switch to the Run and Debug tab on the left hand side. At the top of the screen, you should see a green play arrow titled (lldb) Launch. We will discuss how this appeared in a bit, but for now just click it.

If all goes well, you should get something like this shown in the video below. Here I step through the function using the debugging menu that pops up in the top middle of the screen. You can print out variables along the way using the var command from lldb.

If you remember from my post on using lldb with R, we need to compile the package with optimization turned off to ensure that all of the debugging information is available. Luckily, this is easy. Run the following in the R Terminal of VS Code:

program: The path to the program to debug. This is OS specific, but most Mac users should have R in this location. In any case, the executable can generally be found below the folder revealed by R.home().

env: A list of "name" : "value" pairs for environment variables to set before starting up the program. The only one we seem to need is R_HOME, which you can get by calling R.home() in a typical R session. This should be right for most Mac users already.

terminal: Whether to use the Debug Console ("console"), the Integrated Terminal ("integrated") or an External Terminal (i.e. the Terminal application) ("external") for standard output from the program. This is user preference, but I find that using the "console" is the most useful with a launched R session because it means that the result of the lldb command expr Rf_PrintValue(x) shows up right in the Debug Console rather than in a separate terminal.

So the launch.json file controls what happens when the Run and Debug button is pressed, but what is in the tools/debug.R file that is being executed? Navigate to that now in the Explorer pane. It should have this in it:

The idea is that we set a breakpoint on a particular line, then Launch a new R session that compiles the code, loads it (at which point the breakpoints are locked in), and then tries to call some code that we know will pass through the C++ line where we put the breakpoint. Once the breakpoint is hit, we are free to step through the code and look around.

The other file worth looking at is c_cpp_properties.json in the .vscode folder. This controls the C/C++ Extension that you installed. In particular, it handles IntelliSense, the autocompletion and syntax error generator for C++.

compilerPath: The compiler that would in theory be used to build the project. It also enables more accurate IntelliSense because from this the C++ standard library paths are inferred. This allows us to use things like #include and then autocomplete on everything in that header. It is probably fine to leave this as clang, but you can change it to gcc as well. The easiest way to do this is in the configuration UI mentioned earlier, under the Compiler path section.

cStandard: Relevant if you are debugging C rather than C++ code (which does work!). R packages are typically compiled against C99, so this is probably a good default. Again, it controls how IntelliSense works.

cppStandard: The C++ standard version to use for IntelliSense. cpp11 projects use C++11 by default, so that is what we have chosen here. It is typically a good default unless you know you need a different version.

C++ template classes are a bit tricky to debug. The cpp11 doubles vector is an example of this, it is a template class on cpp11::r_vector. This class has a member function called .at() which will return the element at a specific location.

We have had partial success by explicitly instantiating the template class ahead of time, although this has not proven to be universally successful yet (in particular, it has failed to compile when used with the writable vectors in cpp11). This involves placing a line like this after #include , but before any code that tries to use a doubles vector.

This forces the compiler to fully instantiate all of doubles, including all of its member functions, meaning they are available to you at debug time. If you do this, it is probably a good idea to remove this line after you have finished debugging.

If you hit Command + , (i.e. command and comma), then you will be taken to a general settings menu for VS Code. You should see a tab for User (global settings) and Workspace (settings for this workspace in particular).

If hitting a breakpoint takes you to a view of the Assembly code rather than pausing you in the editor, then the first thing you should check is that the -g and -0O compiler flags have been set (you should be able to see them in the output that is emitted while R is compiling your package).

So, I've got the same problem, but when I put xcode-select --install into Terminal... it tells me "xcode-select: note: install requested for command line developer tools" but nothing else happens. Totally passive aggressive, like sending a job to the printer and it says Ok, Printing and nothing comes out. I also tried putting in sudo xcode-select --reset first, but still nothing. I don't have Xcode installed, I'm not a developer right now on this machine, but trying to install some compilers for RStudio to install some dev versions of R packages. Running Sonoma 14.2.1 on M1 MBA 16GB

MetaboAnalystR package is synchronized with the MetaboAnalyst website and is designed for metabolomics researchers who are comfortable using R coding platform. In this MetaboAnalystR 4.0, an unified metabolomics analysis workflow from LC-MS/MS raw spectral processing to a more accurate functional interpretation has been established. The following tutorials are meant to complement our web-based functions by providing step-by-step instructions for several of the most common tasks using the R package.

c80f0f1006
Reply all
Reply to author
Forward
0 new messages