c++rrdcomclientrtools

Can't build RDCOMClient using rtools40 and R 4.0


A while back, I created a fork of the RDCOMClient package to keep it working with R 3.6 (https://github.com/dkyleward/RDCOMClient). People are now running into issues again because it won't work with R 4.0. The problem doesn't seem as easy to fix, and I'm hoping for some help.

If I flip Rstudio back to R 3.6 (and rtools35), I can use the package after installing with devtools::install_github(). When I try in R 4.0 (and rtools40), the package builds and I can connect over COM to an application. The first line of code below works, and xl is a COM pointer; however, trying to do anything with it (like set Excel to visible) will crash R.

xl <-  RDCOMClient::COMCreate("Excel.Application")
xl[["Visible"]] <- TRUE

Again, the above works in R 3.6.

Is there is a way to continue building with the previous rtools? I came across https://github.com/r-windows/rtools-backports#readme, which talks about using rtools35 to keep building packages, so I have hope, but I don't understand how to make it happen.

Alternatively, if there are minor changes I can make to the R or cpp code that will solve my problem, I'm all ears. I'm a cpp novice, though.


Solution

  • Rik's answer was incredibly helpful and got a version working; however, after spending a day on it, I was able to improve on it. I want to put that here in case I have to do it again. The main improvement is being able to build a working package for both 32- and 64-bit architectures. By default, R installs both, and this makes things easier when installing dependent packages.

    The first two steps are the same:

    Install R-4.0.0 (https://cran.r-project.org/bin/windows/base/old/4.0.0/R-4.0.0-win.exe)

    Install Rtools35 (https://cran.r-project.org/bin/windows/Rtools/Rtools35.exe) in directory c:\Rtools

    If (like me) you had already installed rtools40, a system environment variable named RTOOLS40_HOME is created. The first step is to change that to:

    C:\rtools

    If you don't have rtools40 installed, then create the RTOOLS40_HOME system environment variable.

    Two changes are still needed in the make files. These are found in your R installation directory.

    In etc\x64\Makeconf, add underscores to match the rtools35 directory structure by setting these values:

    MINGW_PREFIX = /mingw_$(WIN)
    BINPREF ?= "$(RTOOLS40_ROOT)/mingw_64/bin/"
    

    Do the same in etc\i386\Makeconf:

    MINGW_PREFIX = /mingw_$(WIN)
    BINPREF ?= "$(RTOOLS40_ROOT)/mingw_32/bin/"
    

    Do not set BINPREF as an environment variable, or this will overwrite the makefile changes (like RTOOLS40_HOME does). With these complete, finish off with the same steps that Rik outlined:

    Open windows command prompt and change to the directory that contains the RDCOMClient subdirectory and type:

    R CMD INSTALL RDCOMClient –-build RDCOMClient.zip

    This installs RDCOMClient in the local installation of R-4.0.0 and additionally creates the file RDCOMClient_0.94-0.zip that can be installed on other systems using the following command:

    install.packages("RDCOMClient_0.94-0.zip", repos = NULL, type = "win.binary")