0

I develope a package using Rcpp, cpp include "windows.h" library, but this package is not available on Linux, so I decided to achieve the same function using reticulate package to call python package, how should I make package detect installed on which platform ? If on Windows platform, installing my package will compile cpp file , if on Linux , installing my package will not compile cpp file but using my r code inside. For example:

Windows:

myfunction<-function(){
   .Call(C++ function)
}

And also avoid compile cpp file in src folder or there will be error : "no windows.h library"

Linux:

myfunction<-function(){
    library(reticulate)
    import(py_package)
}
Jim Chen
  • 3,262
  • 3
  • 21
  • 35
  • I would first try to make the C++ code platform independent using suitable `ifdef __WIN32` etc. What functionality from `windows.h` do you require? Note that it can be problematic to use it in conjunction with `R.h`, c. f. https://stackoverflow.com/questions/11588765/using-rcpp-with-windows-specific-includes – Ralf Stubner Aug 21 '18 at 16:57
  • I use keybd_event function – Jim Chen Aug 21 '18 at 17:11
  • 1
    Interesting. I cannot imagine why one would want to simulate a key press within an R package. However, for Linux you could look at https://www.kernel.org/doc/html/latest/input/uinput.html. – Ralf Stubner Aug 21 '18 at 17:20
  • mmm.....for those people who only knows R but other languages ? And make R more comprehensive. – Jim Chen Aug 21 '18 at 18:40

1 Answers1

1

Maybe you can rethink your approach and provide a C++ solution on platforms other than Windows too? But in short you can the following:

In R

Use eg if (Sys.info()[["sysname"]] == "Windows") to test for windows, and in the else branch call the replacement

In C/C++

Use #define tests such as #if defined(_WIN32) to bracket the #include <windows>, and also the body of your function. Just return eg nothing on other platforms.

Many packages do things depending on the platform they are on. Feel free to look around at existing sources -- CRAN has over 12k and GitHub is treasure trove too.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • You can allow the code to compile on Linux. That is the only way to go as you cannot alter what `install.packages()` does -- but you can influence and alter how your package is set up and _you can make it installable_. Start with reading more of "Writing R Extensions" particular towards the end of it. – Dirk Eddelbuettel Aug 21 '18 at 17:12