gpt4 book ai didi

r - 使用 roxygen2 处理示例文件 : backslashes are duplicated (\dontrun becomes\\dontrun)

转载 作者:行者123 更新时间:2023-12-02 23:32:02 25 4
gpt4 key购买 nike

实际问题

我怎样才能避免这种情况\dontrun{在包含示例的单独文件中变为 \\dontrun{氧化后在各自的 Rd 文件中?

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

详细信息

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

问题在于 \dontrun{ 行在相应的示例文件中变为 \\dontrun{在氧化后生成的 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{ 有问题在./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
>

最佳答案

这是一个错误,已在最新版本的 roxygen2 中修复。

关于r - 使用 roxygen2 处理示例文件 : backslashes are duplicated (\dontrun becomes\\dontrun),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12460347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com