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

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.

Related

BertModel transformers outputs string instead of tensor

I'm following this tutorial that codes a sentiment analysis classifier using BERT with the huggingface library and I'm having a very odd behavior. When trying the BERT model with a sample text I get a string instead of the hidden state. This is the code I'm using:
import transformers
from transformers import BertModel, BertTokenizer
print(transformers.__version__)
PRE_TRAINED_MODEL_NAME = 'bert-base-cased'
PATH_OF_CACHE = "/home/mwon/data-mwon/paperChega/src_classificador/data/hugingface"
tokenizer = BertTokenizer.from_pretrained(PRE_TRAINED_MODEL_NAME,cache_dir = PATH_OF_CACHE)
sample_txt = 'When was I last outside? I am stuck at home for 2 weeks.'
encoding_sample = tokenizer.encode_plus(
sample_txt,
max_length=32,
add_special_tokens=True, # Add '[CLS]' and '[SEP]'
return_token_type_ids=False,
padding=True,
truncation = True,
return_attention_mask=True,
return_tensors='pt', # Return PyTorch tensors
)
bert_model = BertModel.from_pretrained(PRE_TRAINED_MODEL_NAME,cache_dir = PATH_OF_CACHE)
last_hidden_state, pooled_output = bert_model(
encoding_sample['input_ids'],
encoding_sample['attention_mask']
)
print([last_hidden_state,pooled_output])
that outputs:
4.0.0
['last_hidden_state', 'pooler_output']
While the answer from Aakash provides a solution to the problem, it does not explain the issue. Since one of the 3.X releases of the transformers library, the models do not return tuples anymore but specific output objects:
o = bert_model(
encoding_sample['input_ids'],
encoding_sample['attention_mask']
)
print(type(o))
print(o.keys())
Output:
transformers.modeling_outputs.BaseModelOutputWithPoolingAndCrossAttentions
odict_keys(['last_hidden_state', 'pooler_output'])
You can return to the previous behavior by adding return_dict=False to get a tuple:
o = bert_model(
encoding_sample['input_ids'],
encoding_sample['attention_mask'],
return_dict=False
)
print(type(o))
Output:
<class 'tuple'>
I do not recommend that, because it is now unambiguous to select a specific part of the output without turning to the documentation as shown in the example below:
o = bert_model(encoding_sample['input_ids'], encoding_sample['attention_mask'], return_dict=False, output_attentions=True, output_hidden_states=True)
print('I am a tuple with {} elements. You do not know what each element presents without checking the documentation'.format(len(o)))
o = bert_model(encoding_sample['input_ids'], encoding_sample['attention_mask'], output_attentions=True, output_hidden_states=True)
print('I am a cool object and you can acces my elements with o.last_hidden_state, o["last_hidden_state"] or even o[0]. My keys are; {} '.format(o.keys()))
Output:
I am a tuple with 4 elements. You do not know what each element presents without checking the documentation
I am a cool object and you can acces my elements with o.last_hidden_state, o["last_hidden_state"] or even o[0]. My keys are; odict_keys(['last_hidden_state', 'pooler_output', 'hidden_states', 'attentions'])
I faced the same issue while learning how to implement Bert. I noticed that using
last_hidden_state, pooled_output = bert_model(encoding_sample['input_ids'], encoding_sample['attention_mask'])
is the issue. Use:
outputs = bert_model(encoding_sample['input_ids'], encoding_sample['attention_mask'])
and extract the last_hidden state using
output[0]
You can refer to the documentation here which tells you what is returned by the BertModel

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!

Export Rcpp modules

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?

Documentation of squared bracket `[` function

I have a function in R that looks somewhat like this:
setMethod('[', signature(x="stack"),definition=function(x,i,j,drop){
new('class', as(x, "SpatialPointsDataFrame")[i,]) })
I use it to get a single element out of a stacked object. For the package I'm building I need a .Rd file to document the function. I stored it as [.Rd but somehow the R CMD check does not see this. It returns:
Undocumented S4 methods: generic '[' and siglist 'MoveStack,ANY,ANY'
The [.Rd file starts with these lines:
\name{[}
\alias{[}
\alias{[,stack,ANY,ANY-method}
\docType{methods}
\title{Returns an object from a stack}
\description{Returning a single object}
\usage{
\S4method{\[}{stack,ANY,ANY}(x,i,y,drop)
}
Any idea how I make R CMD check aware of this file?
If you look at the source code of the sp package, for example SpatialPolygons-class.Rd, the Methods section:
\section{Methods}{
Methods defined with class "SpatialPolygons" in the signature:
\describe{
\item{[}{\code{signature(obj = "SpatialPolygons")}: select subset of (sets of) polygons; NAs are not permitted in the row index}
\item{plot}{\code{signature(x = "SpatialPolygons", y = "missing")}:
plot polygons in SpatialPolygons object}
\item{summary}{\code{signature(object = "SpatialPolygons")}: summarize object}
\item{rbind}{\code{signature(object = "SpatialPolygons")}: rbind-like method}
}
}
method for [ is defined.
Name and class of the file are
\name{SpatialPolygons-class}
\alias{[,SpatialPolygons-method}
If you look at the help page for ?SpatialPolygons you should see
> Methods
>
> Methods defined with class "SpatialPolygons" in the signature:
>
> [ signature(obj = "SpatialPolygons"): select subset of (sets of)
> polygons; NAs are not permitted in the row index
>
So I would venture a guess that if you specify a proper (ASCII named) file name, give it an alias as in the above example, you should be fine.

Inspect S4 methods

How can I view the definition of a S4 function? For instance, I would like to see the definition of TSconnect in package TSdbi. The command
showMethods("TSconnect")
reveals that there is, among others, a function for drv="histQuoteDriver", dbname="character".
How can I see the definition of this function? If it were a S3 function, there would be only the first argument definable (drv), which could be inspected with print(TSconnect.histQuoteDriver).
Edit: From r-forge I found out the desired output:
setMethod("TSconnect", signature(drv="histQuoteDriver", dbname="character"),
definition= function(drv, dbname, user="", password="", host="", ...){
# user / password / host for future consideration
if (is.null(dbname)) stop("dbname must be specified")
if (dbname == "yahoo") {
con <- try(url("http://quote.yahoo.com"), silent = TRUE)
if(inherits(con, "try-error"))
stop("Could not establish TShistQuoteConnection to ", dbname)
close(con)
}
else if (dbname == "oanda") {
con <- try(url("http://www.oanda.com"), silent = TRUE)
if(inherits(con, "try-error"))
stop("Could not establish TShistQuoteConnection to ", dbname)
close(con)
}
else
warning(dbname, "not recognized. Connection assumed working, but not tested.")
new("TShistQuoteConnection", drv="histQuote", dbname=dbname, hasVintages=FALSE, hasPanels=FALSE,
user = user, password = password, host = host )
} )
Is there a way to get this definition from within an R session?
S4 classes are relatively complicated, so I would suggest reading this introduction.
In this case, TSdbi is an example of a generic S4 class that gets extended by all the specific databases packages (e.g. TSMySQL, TSPostgreSQL, etc.). There is nothing more to the TSconnect() method in TSdbi than what you're seeing: drv="character", dbname="character" are parameters to the function, not functions in and of themselves. If you install some of the specific database packages and use showMethods("TSconnect") you will see all the specific instances of that function. If you then call TSconnect() with a specific database driver it will go and use the appropriate function.
This is how functions such as summary work too. For instance, try calling showMethods(summary). Depending upon which packages are loaded, you should see multiple methods returned
You can easily see the source code for it on R-Forge: http://r-forge.r-project.org/plugins/scmsvn/viewcvs.php/pkg/TSdbi/R/TSdbi.R?rev=70&root=tsdbi&view=markup. This is the extent of that function:
setGeneric("TSconnect", def= function(drv, dbname, ...) standardGeneric("TSconnect"))
setMethod("TSconnect", signature(drv="character", dbname="character"),
definition=function(drv, dbname, ...)
TSconnect(dbDriver(drv), dbname=dbname, ...))

Resources