在同一个包装上使用 roxygen2和 doxygen?

我有一个使用 roxygen2R包。它在 /src中有一些 C代码,我刚刚开始使用 Doxygen。有没有什么方法可以将文档结合起来或者将编译与 roxygen2结合起来?关于把 C代码文档放在哪里,有什么“最佳实践”吗?

搜索 roxygen2和 doxygen 主要导致 氧气与氧气相似结果。我找到了几个包含 Doxyfiles 的软件包,但没有一致的组织结构。例如,lme4inst/doc/Doxyfile输出到 lme4源目录外的一个名为 doxygen的文件夹。Matrix 的根目录中还有一个 Doxyfile (但在以前的版本中是在 inst中)。此文档也导出到包目录之外。

是否有任何理由不将 C文件包含在一个包中,或者为什么尽管广泛使用 C,但在 R 包中却很少使用 Doxygen?

更新: 请参阅相关 < a href = “ https://github.com/klutometis/roxygen/questions/326”rel = “ nofollow noReferrer”> roxygen2功能请求

2603 次浏览

I personally use the following code in a "dataManagement" package I call in all my scripts. It has roxygen documentation and examples. You actually simply call document() and have doxygen ran on the C code, in src/ . The doc is put in inst/doxygen So that your package is CRAN ready.

The R documentation being designed for R end users not supposed to look at the C code I didn't integrate the C code documentation in the classic R documentation but it would probably be a good practice to copy the resulting C documentation as a "vignette".

    library("testthat")
library("devtools")


#' @title Replace a value for a given tag on file in memory
#' @description Scan the lines and change the value for the named tag if one line has this tag,
#'    add a line at the end if no line has this tag and return a warning if several lines
#'    matching the tag
#' @param fileStrings A vector with each string containing a line of the file
#' @param tag The tag to be searched for
#' @param newVal The new value for the tag
#' @return The vector of strings with the new value
#' @examples
#' fakeFileStrings <- c("Hello = world","SURE\t= indeed","Hello = you")
#'
#' expect_warning(ReplaceTag(fakeFileStrings,"Hello","me"))
#'
#' newFake <- ReplaceTag(fakeFileStrings,"SURE","me")
#' expect_equal(length(newFake), length(fakeFileStrings))
#' expect_equal(length(grep("SURE",newFake)), 1)
#' expect_equal(length(grep("me",newFake)), 1)
#'
#' newFake <- ReplaceTag(fakeFileStrings,"Bouh","frightened?")
#' expect_equal(length(newFake), length(fakeFileStrings)+1)
#' expect_equal(length(grep("Bouh",newFake)), 1)
#' expect_equal(length(grep("frightened?",newFake)), 1)
ReplaceTag <- function(fileStrings,tag,newVal){
iLine <- grep(paste0("^",tag,"\\>"),fileStrings)
nLines <- length(iLine)
if(nLines == 0){
line <- paste0(tag,"\t= ",newVal)
iLine <- length(fileStrings)+1
}else if (nLines > 0){
line <- gsub("=.*",paste0("= ",newVal),fileStrings[iLine])
if(nLines >1){
warning(paste0("File has",nLines,"for key",tag,"check it up manually"))
}
}
fileStrings[iLine] <- line
return(fileStrings)
}
#' Prepares the R package structure for use with doxygen
#' @description Makes a configuration file in inst/doxygen
#'     and set a few options:
#'     \itemize{
#'        \item{EXTRACT_ALL = YES}
#'        \item{INPUT = src/}
#'        \item{OUTPUT_DIRECTORY = inst/doxygen/}
#'     }
#' @param rootFolder The root of the R package
#' @return NULL
#' @examples
#' \dontrun{
#' DoxInit()
#' }
#' @export
DoxInit <- function(rootFolder="."){
doxyFileName <- "Doxyfile"
initFolder <- getwd()
if(rootFolder != "."){
setwd(rootFolder)
}
rootFileYes <- length(grep("DESCRIPTION",dir()))>0
# prepare the doxygen folder
doxDir <- "inst/doxygen"
if(!file.exists(doxDir)){
dir.create(doxDir,recursive=TRUE)
}
setwd(doxDir)


# prepare the doxygen configuration file
system(paste0("doxygen -g ",doxyFileName))
doxyfile <- readLines("Doxyfile")
doxyfile <- ReplaceTag(doxyfile,"EXTRACT_ALL","YES")
doxyfile <- ReplaceTag(doxyfile,"INPUT","src/")
doxyfile <- ReplaceTag(doxyfile,"OUTPUT_DIRECTORY","inst/doxygen/")
cat(doxyfile,file=doxyFileName,sep="\n")
setwd(initFolder)
return(NULL)
}


#' devtools document function when using doxygen
#' @description Overwrites devtools::document() to include the treatment of
#'    doxygen documentation in src/
#' @param doxygen A boolean: should doxygen be ran on documents in src?
#'     the default is TRUE if a src folder exist and FALSE if not
#' @return The value returned by devtools::document()
#' @example
#' \dontrun{
#' document()
#' }
#' @export
document <- function(doxygen=file.exists("src")){
if(doxygen){
doxyFileName<-"inst/doxygen/Doxyfile"
if(!file.exists(doxyFileName)){
DoxInit()
}
system(paste("doxygen",doxyFileName))
}
devtools::document()
}