首页 文章

在同一个包装上使用roxygen2和doxygen? [关闭]

提问于
浏览
91

我有一个使用 roxygen2R 包 . 它在 /src 中有一些 C 代码,我刚刚开始使用Doxygen . 有没有办法组合文档,或集成编译与roxygen2?任何"best practices"用于放置 C 代码文档的位置?

谷歌搜索roxygen2和doxygen主要导致 roxygen is similar to doxygen 结果 . 我找到了一些包含Doxyfiles的软件包,但没有一致的组织 . 例如,lme4具有inst/doc/Doxyfile输出到 lme4 源目录之外的名为 doxygen 的文件夹 . Matrix的根目录中还有一个Doxyfile(但在之前的版本中是 inst . 此文档也会导出到包目录之外 .

是否有任何理由不在包中包含 C 文档,或者为什么Doxygen在R包中很少使用,尽管广泛使用 C

update: 见相关roxygen2 feature request

1 回答

  • 4

    我个人在我所有脚本中调用的“dataManagement”包中使用以下代码 . 它有roxygen文档和示例 . 你实际上只是调用document()并在src /中运行C代码 . doc放在inst / doxygen中,这样你的软件包就可以使用CRAN了 .

    为R端用户设计的R文档不应该查看C代码我没有将C代码文档集成到经典R文档中,但将生成的C文档复制为"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()
        }
    

相关问题