Export Rcpp modules - r

I have built a package that exposes an Rcpp module. The relevant Rcpp code is here.
RCPP_MODULE(mod_dde) {
using namespace Rcpp;
class_<DdeConv>("DdeConv")
.constructor<std::string, std::string>()
.field_readonly("server", &DdeConv::_server)
.field_readonly("topic", &DdeConv::_topic)
.method("requestItem", &DdeConv::requestItem)
;
}
I want the package client to initialize an instance from this class with as few typing as possible.
In zzz.R I have the following code
loadModule("mod_dde", TRUE)
But when I try this
d <- new(DdeConv, "EXCEL", "[DdeTest.xlsx]Sheet1")
I get
Error in .getClassFromCache(Class, where) : object 'DdeConv' not found
I have to do this
mod_dde <- Module(module = "mod_dde", PACKAGE = "rdde")
d <- new(mod_dde$DdeConv, "EXCEL", "[DdeTest.xlsx]Sheet1")
I know, it's not too much more typing, but I should be able to do the former per Dirk's excellent vignette
What am I doing wrong?

Related

Has the method annotations in R(NLP package) been deprecated or replaced?

I am following this article https://mylearnmachinelearning.com/category/linear-regression/ to create a Named Entity Extractor. Like required, I have installed all the openNLP, NLP, rJava, magrittr and openNLPmodels.en packages. All has gone to plan except when using this function annotations.:
# Extract entities from an AnnotatedPlainTextDocument
entities <- function(doc, kind) {
s <- doc$content
a <- annotations(doc)[[1]] #Point of error
if(hasArg(kind)) {
k <- sapply(a$features, `[[`, "kind")
s[a[k == kind]]
} else {
s[a[a$type == "entity"]]
}
}
by using this:
entities(text_doc, kind = "person").
The thing is even the intellisense in RStudio does not seem to know any function annotations. It show annotation,annotate and annotations_in_spans and what not but there is no annotations.
There is even a YouTube video which demonstrates the same. Strangely he is able to use annotations there.
Package versions:
openNLP: v0.2-6
openNLPmodels.en: v1.5-1
rJava - v0.9-9
magrittr - v1.5
NLP - v0.2-0
The annotations method was associated with objects of type AnnotatedPlainTextDocument in earlier versions of the NLP package.
Here is the documentation for version 0.1-11.
The latest NLP version is 0.2-0.
The method for AnnotatedPlainTextDocument is now called annotation (no 's' at the end). From the documentation it seems the main difference is that it returns an Annotation object, not a list of Annotation objects.
The function annotations is in a lot of packages, please see here:
https://www.rdocumentation.org/search?q=annotations
Albeit probably not the best way, if you are looking for a specific function without knowing which package the function belongs to, this site may help you find such a package.
try this:
# Extract entities from an AnnotatedPlainTextDocument
entities <- function(doc, kind) {
s <- doc$content
a <- annotation(doc)
if(hasArg(kind)) {
k <- sapply(a$features, `[[`, "kind")
s[a[k == kind]]
} else {
s[a[a$type == "entity"]]
}
}

R dependend package seems not available in package

I can't figure out what I'm missing. Following Situation:
I wrote a R-Package lets call it "pkg_A"
It depends on "Rcpp" and I have a Module loaded and a class set up like :
Rcpp::loadModule("pkg_A_modul", what = "pkg_A_cppClass_A")
cppClass_A <- setRcppClass(
Class = "pkg_A_cppClass_A",
CppClass = "pkg_A_cppClass_A",
module = "pkg_A_modul",
fields = c(
remark = "character"
)
)
and a additional constructor
classA <- function(a,b){
# some stuff
tmpObj <- cppClass_A()
# some more stuff
return(tmpObj)
}
class(classA ) <- "classA "
The only Thing what I export to the NAMESPACE is the classA function/class.
That all works fine and I can build that package without any warnings and even can check it with "--as-cran" flag.
Now I want to build a second package on top of that package, lets call that "pkg_B" Therefore I list "pkg_A" in the DESCRIPTION Depends of "pkg_B"
as well as I load the class with importFrom(pkg_A, classA) in the NAMESPACE of "pkg_B".
Now I want to implement a class
classB <- setRefClass(
"classB",
contains = c("classA"),
fields = c( b = "numeric)
)
But when I now want to build "pkg_B" I get the error:
Error in getClass(what, where = where) : "classA" is not a defined
class
I also tried to use the "pkg_A_cppClass_A" instead or imprt the whole pkg_A or use pkg_A::classA. Nothing changed.
Hope the question is complete enough. If you missing some info let me know.
Thankful for any suggestions!

Document S4 class and "undocumented code object" during R CMD check

After struggling with documenting a S4 class with roxygen2, I decided to take a step back and create a minimum example using package.skeleton, promptClass, and promptMethod.
My problem is that R CMD check still gives a warning about "undocumented code objects", although I think that I have documented them properly.
The files I have now are:
testClass.R:
setClass("testClass",
slots = c(a = "numeric"),
prototype = prototype( a = 0 ),
validity = function(object) return(TRUE))
setGeneric(name = "testMethod",
def = function(object, ...) standardGeneric("testMethod") )
setMethod(f = "testMethod", signature = "testClass",
definition=function(object, x)
{
cat("testMethod:",x,"\n")
invisible(object)
}
)
testClass-class.Rd
\name{testClass-class}
\Rdversion{1.1}
\docType{class}
\alias{testClass-class}
%%\alias{testMethod,testClass-method}
\title{Class \code{"testClass"}}
\description{bla bla}
\section{Objects from the Class}{bla bla}
\section{Slots}{\describe{\item{\code{a}:}{Object of class \code{"numeric"} ~~ }}}
\section{Methods}{\describe{\item{testMethod}{\code{signature(object = "testClass")}: ... }}}
\keyword{classes}
and testMethod.Rd
\name{testMethod-methods}
\docType{methods}
\alias{testMethod-methods}
\alias{testMethod,testClass-method}
\title{ ~~ Methods for Function \code{testMethod} ~~}
\description{blabla}
\section{Methods}{
\describe{\item{\code{signature(object = "testClass")}}{blabla}}}
\keyword{methods}
There is also a package documentation file, but I think it is not relevant here.
R CMD check gives:
* checking for missing documentation entries ... WARNING
Undocumented code objects:
‘testMethod’
All user-level objects in a package should have documentation entries.
See chapter ‘Writing R documentation files’ in the ‘Writing R
Extensions’ manual.
I have consulted these sections, and what I took from these is that I needed at least an alias to generic,signature-list-method, which in this case would be alias{testMethod,testClass-method} which was placed in the documentation file automatically by my call of promtMethod (I have commented it out from the class .Rd file because it was duplicated there).
What do I need to change in the .Rd file to get rid of this warning?
In the meanwhile, I figured out the problem. It seems that I also needed to ass \alias{testMethod} to the .Rd file. I find it strange, however, that the file generated by promptMethod did not include this alias.

Which library is the pr_DB object defined in?

I am completely new to R.
I am trying to use the dist object with a custom function based on the specification here, but I was unable to pass the custom function directly by name, so I tried to add it using the registry described here, but it appears that I am missing a library.
However, I'm not sure which library I need and cannot find a reference to find the name of the library.
Here's a code sample that I'm trying to run:
library(cluster)
myfun <- function(x,y) {
numDiffs <- 0;
for (i in x) {
if (x[i] != y[i])
numDiffs <- numDiffs + 1;
}
return(numDiffs);
}
summary(pr_DB)
pr_DB$set_entry(FUN = myfun, names = c("myfun", "vectorham"))
pr_DB$get_entry("MYFUN")
Here's the error:
Error in summary(pr_DB) : object 'pr_DB' not found
Execution halted
You need to learn the conventions used by R help pages. That "{proxy}" at the top of the page you linked to is really the answer to your question. The convention for the help page construction is "topic {package_name}".

rJava: using java/lang/Vector with a certain template class

I'm currently programming an R-script which uses a java .jar that makes use of the java/lang/Vector class, which in this case uses a class in a method that is not native. In java source code:
public static Vector<ClassName> methodname(String param)
I found nothing in the documentation of rJava on how to handle a template class like vector and what to write when using jcall or any other method.
I'm currently trying to do something like this:
v <- .jnew("java/util/Vector")
b <- .jcall(v, returnSig = "Ljava/util/Vector", method = "methodname",param)
but R obviously throws an exception:
method methodname with signature (Ljava/lang/String;)Ljava/util/Vector not found
How do I work the template class into this command? Or for that matter, how do I create a vector of a certain class in the first place? Is this possible?
rJava does not know java generics, there is no syntax that will create a Vector of a given type. You can only create Vectors of Objects.
Why are you sticking with the old .jcall api when you can use the J system, which lets you use java objects much more nicely:
> v <- new( J("java.util.Vector") )
> v$add( 1:10 )
[1] TRUE
> v$size()
[1] 1
# code completion
> v$
v$add( v$getClass() v$removeElement(
v$addAll( v$hashCode() v$removeElementAt(
v$addElement( v$indexOf( v$retainAll(
v$capacity() v$insertElementAt( v$set(
v$clear() v$isEmpty() v$setElementAt(
v$clone() v$iterator() v$setSize(
v$contains( v$lastElement() v$size()
v$containsAll( v$lastIndexOf( v$subList(
v$copyInto( v$listIterator( v$toArray(
v$elementAt( v$listIterator() v$toArray()
v$elements() v$notify() v$toString()
v$ensureCapacity( v$notifyAll() v$trimToSize()
v$equals( v$remove( v$wait(
v$firstElement() v$removeAll( v$wait()
v$get( v$removeAllElements()

Resources