How can I add roxygen @export all functions in a package

I am using an existing package. The package was written with .Rd documentation before R functions had to be explicitly exported.

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

But now the package doesn't work because the functions are not exported. I would rather just export all the default functions. At the moment it seems like my general approach would be to identify a regex that can be found for each function ( ##' @return

would be a good target) and insert ##' @export

into 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 will be a replacement:

##' @return something

      

from

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

      

Getting a list of functions and adding them to NAMESPACE won't work because it devtools::document("mypackage")

will overwrite it if it's @export

not in 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 NAMESPACE

parse the expressions and parse export

and method

.

+3


source to share


1 answer


EDIT: using only functions exported to NAMESPACE

, and if they don't have an operator @export

somewhere in their Roxygen block, we can do a hack analysis of all export

ed 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 )
}

      

+4


source







All Articles