install_github install package but I can't call the functions - r

I can see my package is installed with library(), but when I load the package from the library, I can't call any of the functions.
> install_github("pdfHarvester", "hansthompson")
> library(pdfHarvester)
> Convert
Error: object 'Convert' not found
> Parse_Tables
Error: object 'Convert' not found

You need to make sure you package actually exports the functions you want to use. This is a package development issue - not an issue with install_github.
Note that your NAMESPACE file is empty: https://github.com/hansthompson/pdfHarvester/blob/master/NAMESPACE
It looks like you're using roxygen for your .R files. If you're using devtools for the package creation you need to use document() to generate your help file and populate the NAMESPACE file.

Related

Importing an Rcpp header file in NAMESPACE within an R Package

This is my first package in R, I already have working package but I would remove some rewriting function in cpp file, so I do an header file that work with single function.
How can I put this header in package?
Note that header.h and header.cpp are in src/ directory of package
and the #include "header.h" is in the .cpp file where I use this function
I tried to modify the NAMESPACE file with:
import(myheader)
But, when I do:
R CMD INSTALL mypackage
I receive this error:
Error: package or namespace load failed for 'mypackage' in namespaceExport(ns, exports):
undefined exports: myheader
How can I solve this error?
As #RalfStubner pointed out in the comments, the NAMESPACE file is meant for exporting and importing R functions and data.
The primary requirement for a NAMESPACE files in a package using Rcpp is to ensure:
A single function from Rcpp package is imported for registration reasons.
Generally, either evalCpp or sourceCpp is used.
Provide the name of the shared object via useDynLib(),
This is the name of the R package being built.
importFrom(Rcpp, sourceCpp)
useDynLib(<PACKAGE_NAME_HERE>, .registration = TRUE)
where <PACKAGE_NAME_HERE> is the name of the package without <>.
If you're interested in using headers to share code between R packages, consider looking at:
https://github.com/r-pkg-examples/rcpp-shared-cpp-functions
The main design pattern is using inst/include directory to place a header-only library. Then, in src/ write bindings to the library. Ensure that src/Makevars and src/Makevars.win has:
# Register where the header files for the package can be found
PKG_CXXFLAGS=-I../inst/include/
If you want to share function definitions between .cpp files in the same R package, see:
https://github.com/r-pkg-examples/rcpp-headers-src
This avoids a single monolithic .cpp file, but does not allow for sharing the compiled code routines between R packages outside of the exported R wrapper.

How to properly use Fortran shared object in R package

I'm writing an R package with the help of devtools.
I have a R function which uses Fortran subroutines (called with .Fortran() function) from a packagename.so file located in the src folder.
I put #useDynLib packagename in R/packagename.R and ran devtools::document() to add useDynLib(packagename) to NAMESPACE but when I run devtools::check() an error occures.
I read the R packages documentation and googled the question but I haven't found a solution yet.
Two related questions come to my mind:
do I need the Fortran source code and let R compile it?
should the shared object have the same name of the package or not?
TL;DR
How do I get rid of the following error after running devtools::check()?
Error: package or namespace load failed for ‘ROCkerMeth’ in library.dynam(lib, package, package.lib):
shared object ‘ROCkerMeth.so’ not found

Install package "locClass" in R

I want to install a package named "locClass" in R but always failed and kept getting the following warning:
install.packages("locClass", repos="http://download.r-forge.r-project.org/")
Package which is only available in source form, and may need compilation of C/C++/Fortran: ‘locClass’
These will not be installed
The install command is referred to: https://r-forge.r-project.org/R/?group_id=1187
Also, I downloaded the package source from the above website. But R returned the following:
library(locClass)
Error in library(locClass) : ‘locClass’ is not a valid installed package
My R version is 3.3.3. I tried the 3.4.0 but failed the same way.
This is not exactly a solution but an alternative to utilize functionality of package locClass. Download the package file of locClass from here and source(functions.R) present here. This way you can use all functions available in package.
Note : function.R means FLXMCL.R, FLXMCLconstant.R etc.
I'm still looking for a better solution but this is a temporary alternative.

Package "Imports" not loading in R development package

I am building a package in R in a windows environment using Rstudio, devtools roxygen2 and Rtools.
The package is showing no problems in R CMD CHECK. However when I try to load the package using library("mypkg"), the packages specified under Imports in DESCRIPTION are not being loaded (Loading required package: message is not there). On using pkgDepends("mypkg"), the $Depends is shown as character(0).
I have to load the required packages using library() for mypkg to function.
I am using namespace imports instead of package::function() syntax. All the required packages are there in the NAMESPACE as imports().
Why is this happening? How to solve this?
That's the correct behaviour. Imports just means that code inside your package can see the functions that you import from other packages. The other packages aren't placed on the search path like with Depends.
Further reading:
Better explanation of when to use Imports/Depends

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