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

Rap*_*ter 11 r roxygen roxygen2

实际问题

如何避免\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)
}
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

3)创建包骨架

package.skeleton(name="test", path="package", 
    code_files=c("src/foo1.R", "src/foo2.R"))
Run Code Online (Sandbox Code Playgroud)

4)为.创建单独的示例文件 foo1()

dir.create("package/test/inst/examples", recursive=TRUE, showWarnings=FALSE)
write(ex.1, file="package/test/inst/examples/foo1.R")
Run Code Online (Sandbox Code Playgroud)

5)氧化

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

6)检查包裹

shell("R CMD check package/test", intern=FALSE)
Run Code Online (Sandbox Code Playgroud)

[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
> 
Run Code Online (Sandbox Code Playgroud)

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)
}
Run Code Online (Sandbox Code Playgroud)

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

patchRdFiles(name="test")
Run Code Online (Sandbox Code Playgroud)

8)再次检查包裹

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

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
> 
Run Code Online (Sandbox Code Playgroud)

Jer*_*oen 2

此分支中实现了(更简单的)临时修复: https://github.com/jeroenooms/roxygen/tree/patch_dontrun

请参阅更改的提交。这是一个单行修复。我还向 Peter 发送了一个 Pull 请求,所以希望它能在主包中得到采用。