使用roxygen2:反斜杠处理示例文件是重复的(\ dontrun变为\\ dontrun)

时间:2012-09-17 13:32:51

标签: r roxygen roxygen2

实际问题

如何在roxygenizing之后的相应Rd文件中避免包含示例的单独文件中的\dontrun{变为\\dontrun{

我确实找到了一种解决方法,但感觉好像我可能只是遗漏了一些明显的东西,即roxigenize()的一些设置。

详细

我认为我注意到一个可能的错误,或者恕我直言,在处理存储在单独文件中的roxygen2的示例时,至少有一个不受欢迎的行为(而不是在实际的roxygen代码)。

问题是,在氧化后生成的Rd文件中各个示例文件中的行\dontrun{变为\\dontrun{

您可以在下面找到行为的说明以及简单的解决方法

1)确保目录

dir.create("src", recursive=TRUE, showWarnings=FALSE)
dir.create("package", recursive=TRUE, showWarnings=FALSE)

# Ensure empty package directory
subdirs <- list.files("package", full.names=TRUE)
if (length(subdirs)) {
    sapply(subdirs, unlink, recursive=TRUE)
}

2)使用两种不同的嵌入示例

创建示例函数
foo1 <- function(x) {message("I'm foo #1"); return(TRUE)}
roxy.1 <- c(
    "#' Title foo1()",
    "#'", 
    "#' Description foo1().",
    "##' This line is commented out",
    "#'", 
    "#' @param x Some R object that doesn't matter.",
    "#' @return \\code{TRUE}.",
    "#' @references \\url{http://www.something.com/}",
    "#' @author John Doe \\email{john.doe@@something.com}",
    "#' @seealso \\code{\\link{foo2}}",
    "#' @example inst/examples/foo1.R"
)
ex.1 <- c(
    "\\dontrun{",
    "foo1()",
    "}"
)

foo2 <- function(y) {message("I'm foo #2"); return(FALSE)}
roxy.2 <- c(
    "#' Title foo2()",
    "#'", 
    "#' Description foo2().",
    "##' This line is commented out",
    "#'", 
    "#' @param y Some R object that doesn't matter.",
    "#' @return \\code{FALSE}.",
    "#' @references \\url{http://www.something.com/}",
    "#' @author John Doe \\email{john.doe@@something.com}",
    "#' @seealso \\code{\\link{foo1}}",
    "#' @examples", 
    "#' \\dontrun{",
    "#' foo2()}",
    "#' }"
)

write(roxy.1, file="src/foo1.R")
write(c("foo1 <-", deparse(foo1)), file="src/foo1.R", append=TRUE)
write(roxy.2, file="src/foo2.R")
write(c("foo2 <-", deparse(foo2)), file="src/foo2.R", append=TRUE)

3)创建包骨架

package.skeleton(name="test", path="package", 
    code_files=c("src/foo1.R", "src/foo2.R"))

4)为foo1()

创建单独的示例文件
dir.create("package/test/inst/examples", recursive=TRUE, showWarnings=FALSE)
write(ex.1, file="package/test/inst/examples/foo1.R")

5)Roxygenize

require("roxygen2")
roxygenize(
    package.dir="package/test",
    overwrite=TRUE, 
    unlink.target=FALSE,
    roclets = c("collate", "namespace", "rd")
)

6)检查包

shell("R CMD check package/test", intern=FALSE)

R CMD检查的截断输出,显示\dontrun{

中的./package/test/man/foo1.Rd存在问题
[...]
Warning: parse error in file 'test-Ex.R':
1: unexpected input
19: 
20: \
    ^
* checking examples ... ERROR
Running examples in 'test-Ex.R' failed
The error most likely occurred in:

> ### Name: foo1
> ### Title: Title foo1()
> ### Aliases: foo1
> 
> ### ** Examples
> 
> \dontrun{
Error: unexpected input in "\"
Execution halted
Warning message:
In shell(expr, intern = FALSE) :
  'R CMD check package/test' execution failed with error code 1
> 

7)解决方法

patchRdFiles <- function( 
    path="package",
    name,
    ...
) {
    path <- file.path(path, name, "man")
    if (!file.exists(path)) {
        stop(paste("Invalid directory path: '", path, "'", sep=""))
    }
    files <- list.files(path, full.names=TRUE)  
#ii=files[1]    
    .dead <- sapply(files, function(ii) {
        cnt <- readLines(ii, warn=FALSE)
        if (length(grep("\\\\\\\\dontrun", cnt, perl=TRUE))) {
            message(paste("Correcting: '", ii, "'", sep=""))
            write(gsub("\\\\dontrun", "\\dontrun", cnt), file=ii)
        }
        return(NULL)
    })
    return(TRUE)
}

这将删除Rd文件中所有重复的反斜杠:

patchRdFiles(name="test")

8)再次检查包裹

# CHECK PACKAGE AGAIN
path <- "package/test"
expr <- paste("R CMD check", path)
shell(expr, intern=FALSE)

再次检查R CMD的截断输出。有问题的Rd文件现在通过了检查。当前错误是由不完整的./package/test/man/test-package.Rd引起的,此时此情况不错

[...]
Warning: parse error in file 'test-Ex.R':
11: unexpected symbol
56: 
57: ~~ simple examples
              ^
* checking examples ... ERROR
Running examples in 'test-Ex.R' failed
The error most likely occurred in:

> ### Name: test-package
> ### Title: What the package does (short line) ~~ package title ~~
> ### Aliases: test-package test
> ### Keywords: package
> 
> ### ** Examples
> 
> ~~ simple examples of the most important functions ~~
Error: unexpected symbol in "~~ simple examples"
Execution halted
Warning message:
In shell(expr, intern = FALSE) :
  'R CMD check package/test' execution failed with error code 1
> 

2 个答案:

答案 0 :(得分:3)

这是在最新版本的roxygen2中修复的错误。

答案 1 :(得分:2)

在此分支中实现(更简单)temporary fixhttps://github.com/jeroenooms/roxygen/tree/patch_dontrun

请参阅commit了解更改。它是一个单线修复。我也向彼得发送了拉动请求,希望它能在主包中被采用。