実際の質問
roxygenizing後に、例を含む別のファイルがそれぞれのRdファイルになることを回避する\dontrun{
にはどうすればよいですか?\\dontrun{
私は回避策を見つけましたが、明らかに何かが欠けているように感じます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) サンプルを埋め込む 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
>