roxygen2: Issue with exporting print method

独自空忆成欢 提交于 2020-12-01 10:48:46

问题


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).


回答1:


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.




回答2:


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().



来源:https://stackoverflow.com/questions/23724815/roxygen2-issue-with-exporting-print-method

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!