我编写了一个使用rstudioapi::viewer()
函数的R包。显然,不是每个人都使用 RStudio 。我现在正在试图正确配置NAMESPACE
和DESCRIPTION
。
为了不强迫用户安装他们不需要(和/或在他们的系统上无用)的软件包,我尝试将rstudioapi
放在Suggests
部分,称其为可用性:
if(.Platform$GUI == "RStudio") {
if ("rstudioapi" %in% rownames(installed.packages())) {
rstudioapi::viewer(outfile_path)
} else {
message("To view html content in RStudio, run install.packages('rstudioapi').")
message("Switching method to 'browser'")
method <- "browser"
}
但在R CMD CHECK
,我得到:
checking dependencies in R code ... WARNING
'::' or ':::' import not declared from: 'rstudioapi'
所以我去宣传,将importFrom(rstudioapi, viewer)
添加到我的NAMESPACE
。结果:
checking package dependencies ... ERROR
Namespace dependency not required: 'rstudioapi'
回到official docs,我也尝试了以下内容:
if (requireNamespace("rstudioapi", quietly = TRUE)) {
rstudioapi::viewer(outfile_path)
} else { ...
无济于事:
checking dependencies in R code ... WARNING
'::' or ':::' import not declared from: 'rstudioapi'
'loadNamespace' or 'requireNamespace' call not declared from: 'rstudioapi'
所以我要么是因为没有声明它而得到警告,要么是因为声明而发出错误。如果你不诅咒就该诅咒,如果你做的话,甚至会更诅咒。任何帮助表示赞赏。
答案 0 :(得分:3)
我遇到的另一个选择是,不要在rstudioapi
文件中的任何位置列出DESCRIPTION
。 RStudio有tips for using the viewer可能有帮助,并且不需要依赖rstudioapi
。
他们建议使用
viewer <- getOption("viewer")
if (!is.null(viewer))
viewer("http://localhost:8100")
else
utils::browseURL("http://localhost:8100")
这是有效的,因为在启动时,RStudio会创建一个名为viewer
的选项,并使用函数填充它。如果您不使用RStudio,getOption("viewer")
将返回NULL
,您可以重定向它以使用系统的默认浏览器。这基本上就是您已经完成的工作,但不需要额外的依赖项。
这也有比检查installed.packages
快约1000倍的好处,但仍然以纳秒为单位测量,所以可能不是一个大问题(请注意,我只安装了192个软件包。在已下载CRAN上的所有内容的系统上可能需要更长的时间。
答案 1 :(得分:2)
我弄明白为什么会有这些警告/错误。当我将rstudioapi
添加到Suggests:
列表时,我无意中创建了第二个 Suggests:
列表。只有第二个被考虑在内(是的,现有的一个是在DESCRIPTION文件的底部,完全我错过了。我不会删除它以防万一发生在别人身上......
答案 2 :(得分:0)
I have a few ideas that might help.
It might be useful to refer to Hadley Wickham's online R packages book: http://r-pkgs.had.co.nz/description.html. I learned most of what (little) I know about packages from his book.
I think that you can add to your DESCRIPTION file, under the "Suggests" field the package rstudioapi
with the following code:
devtools::use_package("rstudioapi", "Suggests")
I've always used the Roxygen2
package (and more recently devtools
) for interacting with the NAMESPACE file, so I don't manually edit NAMESPACE.
Here is what my DESCRIPTION file looks like:
Package: stack3
Type: Package
Title: What the Package Does (Title Case)
Version: 0.1.0
Author: Who wrote it
Maintainer: The package maintainer <yourself@somewhere.net>
Description: More about what it does (maybe more than one line)
Use four spaces when indenting paragraphs within the Description.
License: What license is it under?
Encoding: UTF-8
LazyData: true
Suggests:
rstudioapi
RoxygenNote: 5.0.1
I then ran
devtools::build()
to get the stack3_0.1.0.tar.gz file. Note that I named my package stack3
. In running R CMD CHECK stack3_0.1.0.tar.gz, I see that I get no errors and one warning. The warning is due to the text that by default follows License: in the DESCRIPTION file.
R CMD CHECK stack3_0.1.0.tar.gz
* using log directory ‘/Users/frederickboehm/Box Sync/stack3.Rcheck’
* using R version 3.3.3 (2017-03-06)
* using platform: x86_64-apple-darwin13.4.0 (64-bit)
* using session charset: UTF-8
* checking for file ‘stack3/DESCRIPTION’ ... OK
* checking extension type ... Package
* this is package ‘stack3’ version ‘0.1.0’
* package encoding: UTF-8
* checking package namespace information ... OK
* checking package dependencies ... OK
* checking if this is a source package ... OK
* checking if there is a namespace ... OK
* checking for executable files ... OK
* checking for hidden files and directories ... OK
* checking for portable file names ... OK
* checking for sufficient/correct file permissions ... OK
* checking whether package ‘stack3’ can be installed ... OK
* checking installed package size ... OK
* checking package directory ... OK
* checking DESCRIPTION meta-information ... WARNING
Non-standard license specification:
What license is it under?
Standardizable: FALSE
* checking top-level files ... OK
* checking for left-over files ... OK
* checking index information ... OK
* checking package subdirectories ... OK
* checking whether the package can be loaded ... OK
* checking whether the package can be loaded with stated dependencies ... OK
* checking whether the package can be unloaded cleanly ... OK
* checking whether the namespace can be loaded with stated dependencies ... OK
* checking whether the namespace can be unloaded cleanly ... OK
* checking loading without being on the library search path ... OK
* checking examples ... NONE
* checking PDF version of manual ... OK
* DONE
Status: 1 WARNING
See
‘/Users/frederickboehm/Box Sync/stack3.Rcheck/00check.log’ for details.
I hope that I understood your question and that this response is helpful.