سؤال

I am taking over an existing package. The package was written with .Rd documentation before R required functions to be explicitly exported.

My first step was to convert the documentation to roxygen using Rd2roxygen (many many thanks to Yihui and Hadley for this package!).

But now the package does not work because the functions are not exported. I would rather just export all functions by default. At this point it seems that my general approach would be to identify a regexp that can be found for each function ( ##' @return would be a good target) and insert ##' @export in the line above it,

In pseudocode:

for all files in the `R/` directory{
   for each line starting in `##' @return`{ 
      insert `##' @export` in the preceeding line
}}

The result would be to replace:

##' @return something

with

##' @export
##' @return something that varies with each function

Getting a list of functions and adding them to NAMESPACE will not work because devtools::document("mypackage") will overwrite it if @export is not in the roxygen code above each function.

Question: what would be the most efficient way to add @export to the roxygen documentation for each function in the package?

An even better alternative would be to parse the NAMESPACE and parse export and method statements accordingly.

هل كانت مفيدة؟

المحلول

EDIT: Using only the functions exported in NAMESPACE, and assuming they do not have an @export statement somewhere in their Roxygen block, we can do a hacky parse of all the exported functions:

NAMESPACE <- readLines("NAMESPACE")
exported_fns <- grep( "^export\\(", NAMESPACE, value=TRUE )
exported_fn_names <- gsub( "export\\((.*)\\)", "\\1", exported_fns )
fn_match_re <- paste("^", exported_fn_names, " ?<- ?", sep="")

for( file in list.files("./R", full.names=TRUE) ) {
  doc <- readLines( file )
  for( i in seq_along(doc) ) {
    if( any( sapply( fn_match_re, function(x) {
      length( grep( x, doc[i] ) ) > 0
      } ) ) ) {
      doc[i] <- paste( "##' @export", doc[i], sep="\n" )
    }
  }
  writeLines( doc, file )
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top