Dynamic library not loading in R binary package build - r

I am trying to build a package with compiled C code in R using 'RStudio' and 'devtools' in a Windows environment.
Only one of the function uses the C code in src folder. The source package works fine. I can use all the functions. I am able to compile the C code using devtools::document() and the corresponding .dll and .o file also appears in the src folder. Then I can load the code using dev_tools::load_all or Ctrl+Shift+L and run all the functions.
However when I am building and reloading the package using Ctrl+Shift+B, I am not able to use the particular function. The function is missing from the package even thought the documentation is retained. I also get the error telling that the corresponding .dll is not loaded.
Error in library.dynam.unload(name, system.file(package = name)) :
DLL ‘mypackage.dll’ was not loaded
I get the same results when I am using devtools::build with binary=TRUE.
However I can find the .dll file in the library Documents\R\win-library\3.0\mypackage\libs\i386\mypackage.dll. Why is this dynamic library from compiled code not being loaded?
PS: 1) devtools::has_devel() is giving TRUE
2) I am forced to use .C instead of .Call.
This is the result of the R CMD INSTALL
* installing to library 'C:/Users/lenovo/Documents/R/win-library/3.0'
* installing *source* package 'mypackage' ...
** libs
make: Nothing to be done for `all'.
installing to C:/Users/lenovo/Documents/R/win-library/3.0/mypackage/libs/i386
** R
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (mypackage)

I was able to solve this on Windows 10 with R 3.5 by adding the following function to a file in the R/ folder.
.onUnload <- function (libpath) { library.dynam.unload("mypackage", libpath)}
Here is the reference where I found it.

Related

After building a package, where is its zip file?

I have developed a very simple basic package called : mypackage in windows environment which contains one function
na2zero()
in R-Studio IDE i just did Build --> Clean and Rebuild resulted into below logs
==> Rcmd.exe INSTALL --preclean --no-multiarch --with-keep.source package
* installing to library 'C:/Users/NAME/Documents/R/R-4.0.0/library'
* installing *source* package 'mypackage' ...
** using staged installation
** R
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
converting help for package 'mypackage'
finding HTML links ... hello html
na2zero html
done
** building package indices
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (mypackage)
i'm able to invoke the functions which resides in mypackage, but my question is, i want to see the location of the package, i believe the extension will be .zip
so in which folder the package (mypackage) exists.?
In RStudio, the "Install and Restart" menu item in the Build pane will install the package directly into R, without creating an archive file.
If you want to create an archive, there are two kinds of those. Use "Build Source Package" (in the "More..." menu) to create a .tar.gz file, which can be installed in any current version of R, and also some past and future ones, on any supported platform.
Use "Build Binary Package" to create a binary package. On Windows, that would be a .zip file. It is only good for Windows and a very small range of R versions (typically if you build in R version X.Y.Z, you'll be able to use it for different value of Z, but not different values of X or Y). You won't be able to use it on Linux or MacOS.
For a simple package, you should almost always use the "Source Package" format. Only when you have compiled code (C, C++, Fortran) is it a good idea to use the "Binary Package" format: your users may not have the compiler needed to install it themselves. But you'll need to match your users' R versions very carefully.
For both Source and Binary packages, RStudio will put the archive file in the same folder as your package source is in, e.g. if your package version 1.0.0 is in
path/to/mypackage, it will produce path/to/mypackage_1.0.0.zip.

Update .Rd of my Package

What is the best way to update .Rd files for my new functions of my package ?
I added a function to my R script and create a .Rd file to explain this function but when I rebuilt my package with :
install("CAPepitesPRO",dependencies = T)
library(CAPepitesPRO)
The pkg compilation works fine :
installing help indices
converting help for package 'mypkg'
finding HTML links ... fini
mypkg-package html
func1 html
func2 html
func3 html
** building package indices
** testing if installed package can be loaded
*** arch - i386
*** arch - x64
* DONE (mypkg)
In R CMD INSTALL
but when I try to print the help of func3 whith ?func3 I have this error :
No documentation for ‘func3’ in specified packages and libraries:
you could try ‘??func3’
you should run the following
document("yourpath/CAPepitesPRO")
before the following
install("CAPepitesPRO",dependencies = T)

roxygen2 not creating .Rd documentation

I am unable to generate .Rd documentation files for my package using RStudio and Roxygen2. First, let me mention that I have gone through similar problems posted here and have already done the following:
Roxygen2 blocks initiated at the beginning of file with a #'
Configured Build Tools>Checked generate documentation with Roxygen> Configure > Checked all fields under 'Use roxygen to generate' and 'Automatically roxygenize when running'
Made sure there were no .Rd files in the 'man' folder
And even after that, when I perform a 'Build and Reload' on RStudio I get the following output (please note the line that reads: No man pages found in package MYPACKAGE:
=
=> devtools::document(roclets=c('rd', 'collate', 'namespace', 'vignette'))
>
Updating MYPACKAGE documentation
Loading MYPACKAGE
Documentation completed
==> Rcmd.exe INSTALL --no-multiarch --with-keep.source MYPACKAGE
installing to library C:/Users/user/Documents/R/win-library/3.3
installing source package 'MYPACKAGE' ...
** R
** data
* moving datasets to lazyload DB
** preparing package for lazy loading
No man pages found in package 'MYPACKAGE'
** help
* installing help indices
** building package indices
** testing if installed package can be loaded
DONE (MYPACKAGE)
Edit:
Upon further investigation, it appears that this was caused by the fact that I have sub-directories within my R directory, which is not supported by default. A possible solution was located here which, however, I haven't yet tried out. I will report back with the outcome as soon as I am able to perform the tests.
I had the same error. roxygen2 was creating correctly the md files in the /man directory but they were not found at compilation time. I had the following error.
No man pages found in package
After a bit of time I found that in one of the R file I had a source statement
source("C:/Users/vaulot/Google Drive/Scripts/R library/dv_function_pr2.R")
My guess is that there was some code in the source file interfering with roxygen2.

Building R package: "No man pages found in package"

My previously-functioning R package lllcrc recently broke, so I tweaked it, and now I'm having trouble building it again because it acts like it can't see my documentation files:
R CMD INSTALL lllcrc
* installing to library ‘/home/[...]/3.1’
* installing *source* package ‘lllcrc’ ...
** R
** preparing package for lazy loading
** help
No man pages found in package ‘lllcrc’
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (lllcrc)
This is totally weird because I definitely have a complete set of .Rd files in the man folder; in fact, I generated all of these using roxygen2, and all of this worked previously. The R CMD INSTALL even works enough so that the package actually installs and all of its examples run. But documentation is missing. Running ?foo just gives a "no documentation" message.
Another thing: R CMD Rd2pdf lllcrc generates the .pdf documentation as I would expect.
Any ideas?
I just had the same error message ... if you are using roxygen and RStudio then your problem might be the same. The reason became apparent when looking at "Configure build tools" in the "Build" drop-down menu: you need to tick the checkbox "Generate documentation with Roxygen". After that, everything worked.

Building an R package with a pre-compiled shared library

I am facing a problem with an R package that I am writing and trying to build with a pre-compiled shared library. Let me try to briefly describe the problem:
this package (let's call it mypack) relies on a shared library mylib.dll that is already compiled and that I cannot compile on the fly while building the R package.
the library mylib.dll has been compiled on a x64 machine under Windows and can be loaded in R with dyn.load.
the package contains the required file NAMESPACE, where useDynLib(mylib.dll) is specified. The function .onLoad containing the instruction library.dynam('mylib.dll', pkg, lib) is also specified in a file zzz.R.
the R package mypack is built with Rtools using the usual command Rcmd INSTALL, and I then add a directory libs where I save mylib.dll.
when I try to load the package in R with library(mypack), I get the following error message:
Error: package 'mypack' is not installed for 'arch=x64'
This is puzzling me. Why can the shared library be loaded smoothly in R, but when I build a package using it I am getting this weird error message?
Thank you very much in advance for your help!
That error message comes from this code in library:
if (nzchar(r_arch) && file.exists(file.path(pkgpath,
"libs")) && !file.exists(file.path(pkgpath, "libs",
r_arch)))
stop(gettextf("package %s is not installed for 'arch=%s'",
sQuote(pkgname), r_arch), call. = FALSE, domain = NA)
which is telling me you need a {package}/libs/{arch} folder in your built package (ie the installed directory) with an {arch} that matches your system's arch, as given by r_arch <- .Platform$r_arch
I'm guessing your build has failed to make this correctly. Is there any C code in your source?

Resources