11

实际问题

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

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

细节

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

问题是\dontrun{各个示例文件\\dontrun{中的行在 roxygenizing 后变成了生成的 Rd 文件。

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

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) 充氧

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{in存在问题./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
> 
4

2 回答 2

4

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

于 2014-03-23T23:05:07.717 回答
2

A (more simple) temporary fix is implemented in this branch: https://github.com/jeroenooms/roxygen/tree/patch_dontrun.

See the commit for the change. Its a one-line fix. I have also sent a pull request to peter, so hopefully it will get adopted in the main package.

于 2013-04-01T22:14:49.847 回答