roxygen2: Issue with exporting print method
Asked Answered
J

2

7

I have updated to roxygen2 v4.0.0 and am now attempting to convert @S3method and @method commands to @export commands following the directions here. This seems to have worked well for all of my methods except for those related to print.

Here is a toy example that illustrates my problem (I understand the silliness of the example). Here is the .R file ...

#' Test.
#' 
#' Test.
#'@aliases zzzTest print.zzzTest summary.zzzTest
#'@param v A numeric vector.
#'@param x A \code{zzzTest} object.
#'@param object A \code{zzzTest} object.
#'@param \dots Additional arguments for the S3 methods.
#'@return A \code{zzzTest} object.
#'@keywords manip
#'@examples
#'z <- zzzTest(runif(10,1,2))
#'print(z)
#'summary(z)
#'@rdname zzzTest
#'@export zzzTest
zzzTest <- function(v) {
  tmp <- log(v)
  class(tmp) <- "zzzTest"
}

#'@rdname zzzTest
#'@export
print.zzzTest <- function(x,...) { print(x, ...) }

#'@rdname zzzTest
#'@export
summary.zzzTest <- function(object,...) { summary(object) }

And this is the .Rd file that results from roxygenising ...

% Generated by roxygen2 (4.0.0): do not edit by hand
\name{zzzTest}
\alias{print.zzzTest}
\alias{summary.zzzTest}
\alias{zzzTest}
\title{Test.}
\usage{
zzzTest(v)

print.zzzTest(x, ...)

\method{summary}{zzzTest}(object, ...)
}
\arguments{
\item{v}{A numeric vector.}

\item{x}{A \code{zzzTest} object.}

\item{object}{A \code{zzzTest} object.}

\item{\dots}{Additional arguments for the S3 methods.}
}
\value{
A \code{zzzTest} object.
}
\description{
Test.
}
\examples{
z <- zzzTest(runif(10,1,2))
print(z)
summary(z)
}
\keyword{manip}

My use of @export seems to work fine for the summary method (note the \method()), but not for the print method (note no \method() and only print.zzzTest). I was also successful using @export for several other methods in other .R files. My problems only seem to occur with the print method.

Can someone point out where I am going wrong? Thank you in advance for any help with this problem.

For what it is worth, I am using R 3.1.0, RStudio 0.98.501, and roxygen2 4.0.0.

UPDATE 1: There is an export(print.zzzTemp) but not an S3method(print,zzzTemp) in the namespace ... i.e., the same problem as ZNK (in the comments).

UPDATE 2: I copied the exact .R file into another package, roxygenized that package, and the .Rd file (and corresponding namespace) were created properly. This implies that I have some "switch" related to roxygen2 different between the two packages, but I can't seem to isolate the difference or find such a "switch" (I believe that I have only controlled roxygen through the project options in RStudio).

Juna answered 18/5, 2014 at 17:26 Comment(1)
I've had a similar problem except I'm not documenting my print methods. My namespace has changed from S3method(print, myClass) to export(print.myClass). I'm thinking about submitting a bug report on this.Acaroid
A
12

I have found a solution for my problem (In comments above) and it may work for yours. In The NEWS.md file for v3.0.0, it is mentioned that @method tag is not needed as roxygen2 will figure it out, but it's sill available in the rare case that roxygen2 cannot do so. My solution:

#' @method print myClass    
#' @export    
print.myClass <- function(x) print("myClass")

This gives me back the S3method(print, myClass) in my NAMESPACE file.

Acaroid answered 18/5, 2014 at 22:19 Comment(2)
This worked for me as well; though, I would like to know why my original method did not work. Anyways, thanks for the workaround.Juna
It seems that these days (I'm currently using roxygen2-7.1.1), both @method and @S3method have been removed; instead, @exportS3Method generic class works.Formulary
A
0

The 16th line of your code

#'@export zzzTest

needs to be

#'@export

as mentioned by Hadley in this link.

You might also need to remove the 2nd and 3rd instance of @export. And don't forget to do devtools::document() before building/checking.

The coding I outlined above worked for me until I added a @useDynLib pkg-name tag. Then I had to resort to the @method tag to get the desired NAMESPACE after devtools::document().

Avian answered 24/11, 2016 at 1:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.