Re: [R-pkg-devel] R package does not find DLL routine

2020-07-01 Thread Lisa GM
Dear Dirk, dear Ivan,

thanks for your suggestions. I agree that R packages aren't meant to be
build that way, and even though this is an academic project but since the
source code wasn't written by me it is not my decision if, where and when
to publish it openly. In any case, directly linking to a DLL seems quite
unstable and I just came across the option to do a system call to an
executable instead which appears to maybe be the most straight forward
solution for me.

Thanks a lot for your time and effort.
Best,
Lisa


On Sun, Jun 28, 2020 at 10:39 PM Ivan Krylov  wrote:

> On Sun, 28 Jun 2020 12:43:53 +0200
> Lisa GM  wrote:
>
> >  "sum_c" not resolved from current namespace (sum)
>
> As mentioned by Dirk Eddelbuettel, this is not the way R packages are
> supposed to be built [*], but if you are absolutely positive you cannot
> build the DLL from source together with the package or link your
> package to externally installed DLL (as done by packages curl, rgdal,
> and many others), it still seems to be possible. I have been able to
> get a dummy package to work like this:
>
> .onLoad <- function(libname, pkgname)
>  library.dynam('dynl', pkgname, libname, TRUE)
>
> .onUnload <- function(libpath)
>  library.dynam.unload('dynl', libpath)
>
> do <- function()
>  .C('do_hello', x = as.integer(1L), y = as.numeric(2), PACKAGE = 'dynl')
>
> (Note the extra PACKAGE argument required in absence of native routine
> registration or useDynLib(...) declarations in NAMESPACE)
>
> I placed the DLL itself in file.path('inst', 'libs',
> paste0('dynl', .Platform$dynlib.ext)) under the package source
> directory and made sure that it exports a C function
> void do_hello(int * x, double * y).
>
> Needless to say, this goes against CRAN and Bioconductor policies. The
> preferred approach is described in Writing R Extensions, section 1.2.
>
> --
> Best regards,
> Ivan
>
> [*] See  sections
> 1.5.4 and 5.4 for preparing C functions to be called from an R package.
> The Rcpp package does wrap all this in a very convenient way, see the
> Rcpp.package.skeleton function.
>

[[alternative HTML version deleted]]

__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel


Re: [R-pkg-devel] R package does not find DLL routine

2020-06-28 Thread Ivan Krylov
On Sun, 28 Jun 2020 12:43:53 +0200
Lisa GM  wrote:

>  "sum_c" not resolved from current namespace (sum)

As mentioned by Dirk Eddelbuettel, this is not the way R packages are
supposed to be built [*], but if you are absolutely positive you cannot
build the DLL from source together with the package or link your
package to externally installed DLL (as done by packages curl, rgdal,
and many others), it still seems to be possible. I have been able to
get a dummy package to work like this:

.onLoad <- function(libname, pkgname)
 library.dynam('dynl', pkgname, libname, TRUE)

.onUnload <- function(libpath)
 library.dynam.unload('dynl', libpath)

do <- function()
 .C('do_hello', x = as.integer(1L), y = as.numeric(2), PACKAGE = 'dynl')

(Note the extra PACKAGE argument required in absence of native routine
registration or useDynLib(...) declarations in NAMESPACE)

I placed the DLL itself in file.path('inst', 'libs',
paste0('dynl', .Platform$dynlib.ext)) under the package source
directory and made sure that it exports a C function
void do_hello(int * x, double * y).

Needless to say, this goes against CRAN and Bioconductor policies. The
preferred approach is described in Writing R Extensions, section 1.2.

-- 
Best regards,
Ivan

[*] See  sections
1.5.4 and 5.4 for preparing C functions to be called from an R package.
The Rcpp package does wrap all this in a very convenient way, see the
Rcpp.package.skeleton function.

__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel


Re: [R-pkg-devel] R package does not find DLL routine

2020-06-28 Thread Dirk Eddelbuettel


Hi Lisa,

On 28 June 2020 at 20:32, Lisa GM wrote:
| Thank you so much for your quick reply. I'm not particularly set on the
| 'inline' package especially if it's becoming outdated. I looked into using

Please report bugs or shortcomings in an issue ticket at GitHub. I am also
its maintainer and unaware of any deficiencies. Thanks.

| Rcpp but I couldn't figure out how to compile my c++ code outside of the
| package and only include the DLL. My problem is that I cannot publish the
| c++ source code with the package but I do have access to it. So I'm sorry
| to ask in case this is obvious but can I compile the above to a DLL first
| using Rcpp and then just add the compiled .dylib to the package?

There is generally somewhat limited patience on these lists for support of
code hiding and obfuscation, but methinks you are simply doing this
wrong. Create a source package. Hand your client a binary package built from
it. End of story. And still no messing with loading shared libries by hand.

Dirk

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org

__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel


Re: [R-pkg-devel] R package does not find DLL routine

2020-06-28 Thread Lisa GM
Dear Dirk,

Thank you so much for your quick reply. I'm not particularly set on the
'inline' package especially if it's becoming outdated. I looked into using
Rcpp but I couldn't figure out how to compile my c++ code outside of the
package and only include the DLL. My problem is that I cannot publish the
c++ source code with the package but I do have access to it. So I'm sorry
to ask in case this is obvious but can I compile the above to a DLL first
using Rcpp and then just add the compiled .dylib to the package?

Best,
Lisa

On Sun, Jun 28, 2020 at 7:30 PM Dirk Eddelbuettel  wrote:

>
> Lisa,
>
> Sorry, I misread your code. There is a possible array overrun, so we need
> argument n and p, apparently.  A likely better version, and demo running
> the
> right code are below.  The rest of the reasoning likely stands, methinks.
>
> Code
>
> -
> #include 
>
> // [[Rcpp::export]]
> void mysum(int p, int n, std::vector array) {
>   double res = 0;
>   int myend = std::min(p * n, (int)array.size());
>   for (int i = 0; i < myend; ++i) {
> res += array[i];
>   }
>   std::cout << "result : " << res << std::endl;
> }
>
> /*** R
> myvec <- sqrt(1:10)
> mysum(2,3,myvec)
> */
>
> -
>
> Demo usage
>
> -
> R> Rcpp::sourceCpp("/tmp/lisademo.cpp")
>
> R> myvec <- sqrt(1:10)
>
> R> mysum(2,3,myvec)
> result : 10.8318
> R>
>
> -
>
> Dirk
>
> --
> http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org
>

[[alternative HTML version deleted]]

__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel


Re: [R-pkg-devel] R package does not find DLL routine

2020-06-28 Thread Dirk Eddelbuettel


Lisa,

Sorry, I misread your code. There is a possible array overrun, so we need
argument n and p, apparently.  A likely better version, and demo running the
right code are below.  The rest of the reasoning likely stands, methinks.

Code
-
#include 

// [[Rcpp::export]]
void mysum(int p, int n, std::vector array) {
  double res = 0;
  int myend = std::min(p * n, (int)array.size());
  for (int i = 0; i < myend; ++i) {
res += array[i];
  }
  std::cout << "result : " << res << std::endl;
}

/*** R
myvec <- sqrt(1:10)
mysum(2,3,myvec)
*/
-

Demo usage
-
R> Rcpp::sourceCpp("/tmp/lisademo.cpp")

R> myvec <- sqrt(1:10)

R> mysum(2,3,myvec)
result : 10.8318
R> 
-

Dirk

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org

__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel


Re: [R-pkg-devel] R package does not find DLL routine

2020-06-28 Thread Dirk Eddelbuettel


Lisa,

One can do what you do, but it is fraught with some difficulties (as you
experienced) and even more so once you try to do this portably. Helper
packages exists: the `inline` package is the oldest of this class and still
supports the .C() interface you used here. And which for a few years now has
generally been recommended against.  But if you insist, you can; and `inline`
will take care of compiling, linking and loading for you.

But you already use C++ as evidenced by the iostream use. So why not use just
a wee bit more of it?  It makes you program simpler (one less argument on the
signature) and opens the door to more tooling--in the snippet below we use
Rcpp without using any of its data types. It even runs the R example for us.

Code first:
-
#include 

// [[Rcpp::export]]
void sum_c(int& p, std::vector array) {
  int n = array.size();
  double res = 0;
  for (int i = 0; i < p * n; ++i) {
res += array[i];
  }
  std::cout << "result : " << res << std::endl;
}

/*** R
myvec <- sqrt(1:10)
sum(77, myvec)
*/
-

Usage demo:
-
R> Rcpp::sourceCpp("/tmp/lisademo.cpp")

R> myvec <- sqrt(1:10)

R> sum(77, myvec)
[1] 99.4683
R> 
-

There are other helper packages, and if you still want to do it by hand all
details are in _Writing R Extensions_.

Dirk

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org

__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel