用 roxygen2 覆盖 NAMESPACE 和 Rd

Overwriting NAMESPACE and Rd with roxygen2

我用 RStudio 创建了一个新包。在 "Configure Build Tools" 中,我检查 "Generate documentation with Roxygen".

我第一次点击 "Build" 窗格中的 "Document" 时,一切正常:

==> roxygen2::roxygenize('.', roclets=c('rd', 'collate', 'namespace'))

First time using roxygen2. Upgrading automatically...
Writing hello.Rd
Writing NAMESPACE
Documentation completed

我得到这个命名空间:

# Generated by roxygen2: do not edit by hand

export(hello)

和这个文件 hello.Rd:

% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/hello.R
\name{hello}
\alias{hello}
\title{Hello}
\usage{
hello(x)
}
\arguments{
\item{x}{string}
}
\value{
a string
}

但是现在,我修改文件hello.R,然后遇到两个问题。 首先,这个 window 出现:

如果我点击 "Yes",没有任何反应。

其次,roxygen2 似乎无法覆盖 hello.Rd,因为我在 "Build" 窗格中得到了这段文字:

==> roxygen2::roxygenize('.', roclets=c('rd', 'collate', 'namespace'))

Error: The specified file is not readable: U:\Data\Rtests\testPackage\man/hello.Rd
Execution halted

Exited with status 1.

我发现更新文档的唯一方法是 运行:

roxygen2::roxygenize(clean=TRUE)

此命令首先清除所有内容,NAMESPACE 和 Rd 文件,然后生成 NAMESPACE 和 Rd 文件。

不知道是不是Rtools路径的问题。我尝试通过以下方式设置路径:

Sys.setenv(PATH="%PATH%;C:/Program Files/Rtools/gcc-4.6.3/bin;C:/Program Files/Rtools/gcc-4.6.3/bin64;C:/Program Files/Rtools/gcc-4.6.3/i686-w64-mingw32/bin;C:/Program Files/Rtools/bin")

但这并不能解决问题。

我正在使用:

问题原因。

roxygen2 包依赖于 digest 包。错误(The specified file is not readable)是由digest包的digest函数产生的,在这个函数调用file.access函数:https://github.com/eddelbuettel/digest/blob/master/R/digest.R#L102.

我得到:

> file.access("U:/Data", 4)
U:/Data 
     -1 

也就是说U:/Data没有读取权限。但事实并非如此:它具有读取权限。问题是我的 U: 驱动器是 "network drive",网络驱动器的 file.access 功能存在一些问题,例如我们可以在这里看到:https://github.com/eddelbuettel/digest/issues/13

解决方法

如果在digest::digest函数中使用R.utils::fileAccess代替file.access,问题就解决了。

所以,先把digest::digest函数的代码拿来修改如下

mydigest <- function (object, algo = c("md5", "sha1", "crc32", "sha256", 
    "sha512", "xxhash32", "xxhash64", "murmur32"), serialize = TRUE, 
    file = FALSE, length = Inf, skip = "auto", ascii = FALSE, 
    raw = FALSE, seed = 0, errormode = c("stop", "warn", "silent")) 
{
  file.access <- R.utils::fileAccess
  .... the code of the digest function here ...
}

然后做:

library(digest)
R.utils::reassignInPackage("digest", "digest", mydigest)

现在可以通过以下方式更新文档:

roxygen2::roxygenize()