How to export functions as S3 methods in my R package?
So, the answer was that i was using the wrong @export
directive.
I used @export print.marco. This overrides the creation of S3 methods. Putting simply @export works fine. Thanks to Roland for his comments above.
Exported S3 method not found during check
If you want to be able to call plot.foo
directly, then you will need to explicitly export that version as well. By convention, usually you do not export class-specific implemenations of generic functions from your package. Normally you just declare that the S3 method exists and leave the function unexported. Like if you call methods(plot)
you'll see a bunch with asterisks which means they are unexpected and are not meant to be called directly. If you do for some reason want to export it as a separate function, you can add an additional export statement. For example
#' @rdname foo-methods
#' @export plot.foo
#' @export
#' @importFrom graphics plot
plot.foo <- function(x, ...) {
class(x) <- setdiff(class(x), "foo")
plot(x)
invisible(NULL)
}
Export S3 method for a 'function' class object
According to Writing R Extensions section 1.5.2, you have to use regular quotes when registering S3 methods that work on objects of class function:
(Note that function and class names may be quoted, and reserved words and non-standard names such as [<- and function must be.)
How to use S3 methods from another package which uses export rather than S3method in its namespace without using Depends or library()
The key here is to import the specific methods in addition to the generic you want to use. Here is how you can get it to work for the default method.
Note: this assumes that the test.h5
file already exists.
#' @importFrom rhdf5 h5write.default
#' @importFrom rhdf5 h5write
#' @export
myFun <- function(){
h5write(1:4, "test.h5", "test")
}
I also have put up my own small package demonstrating this here.
R package documentation: Found the following apparent S3 methods exported but not registered
I don't know Roxygen2
very well, but it appears that you have declared is.nan.data.frame
to be the is.nan
method for class data.frame
. Since you did that, you should call it as is.nan(df)
in the help page example.
If you don't want it to be the method, you just want it to be a regular function using dots in the name, then you shouldn't have @method is.nan data.frame
. But you indicate that you do want it to be a method.
Edited to add: Just to summarize your comments, the following fixes got rid of all the errors:
- use @export by itself without naming the function (as suggested by @KonradRudolph)
- remove the @usage line
- use
is.nan(df)
in the example (as I suggested)
The correct way of accessing S3methods from the NAMESPACE
I suspect it’s for the same reason that there are lots of other inconsistencies in the base R packages: it is an organic system that grew over many years, and many aspects are very old and preserve compatibility with the S language.
Many of the guidelines that roxygen2 and other packages recommend have grown out of the experience gained from many years of working with R.
If the base R packages were rewritten from scratch today with no regards for backwards compatibility, they would look very different.
If you find yourself needing edit
and/or fix
to muck around in other peoples’ packages then this suggests that these packages have some major issues that should be resolved. Normally you should not need to do this — this is part of the reason for why fix
is a bad idea: it’s symptomatic of an underlying problem.
In fact, even with Hadley’s recommended mode of exporting package generics it’s still possible (albeit more difficult) to hack around in the non-exported methods of the package.
Exporting non-S3-methods with dots in the name using roxygen2 v4
As Mr Flick commented, appending the full function name to the roxygen line works correctly. If I change the line to:
#' @export check.arg
then the NAMESPACE
file contains:
export(check.arg)
R S3 method not exported from namespace
You get that error because vegan
doesn't export that function. It can give help for things that aren't exported. Using three colons vegan:::reorder.hclust
displays internal functions that are not exported; normally you should avoid using those.
However, when the vegan
package is loaded, its reorder.hclust
function will be added to the methods table for reorder
. So you just need to make sure that it is loaded, and then if hc
is an hclust
object, reorder(hc)
will call the reorder.hclust
method. You can do this by putting requireNamespace("vegan")
into your code, or importing something from vegan
in your NAMESPACE file.
If there are two reorder.hclust
methods (defined by different packages that are both loaded), then I don't think there's an easy way for you to specify the vegan
one other than using vegan:::reorder.hclust
, which CRAN will object to. You would need to ask the vegan
maintainer to export their function so you could access it using the legal vegan::reorder.hclust
, or copy the code into your own package, or some other inconvenient approach.
Related Topics
Get Rid of \Addlinespace in Kable
Emoticons in Twitter Sentiment Analysis in R
Improve Centering County Names Ggplot & Maps
Generate Correlated Random Numbers from Binomial Distributions
How 'Poly()' Generates Orthogonal Polynomials? How to Understand the "Coefs" Returned
Any Suggestions for How to Plot Mixem Type Data Using Ggplot2
Rscript: There Is No Package Called ...
How to Make Time Difference in Same Units When Subtracting Posixct
Different Breaks Per Facet in Ggplot2 Histogram
Shift Values in Single Column of Dataframe Up
Rstudio Shiny Error: There Is No Package Called "Shinydashboard"
How to Make Grouped Layout in Igraph
Shiny: Passing Input$Var to Aes() in Ggplot2
Create Column with Grouped Values Based on Another Column
Reason Behind Speed of Fread in Data.Table Package in R