繁体   English   中英

构建二进制包 R

[英]Building binary package R

我是 R 的新手,我正在尝试制作一个独立的可执行文件,以便我的脚本可以在没有开发工具的情况下运行。 我创建了多个包含不同函数的 R 脚本,并且一直在使用main.r脚本来连接其他脚本。 我一直在使用 RStudio 并在每个文件上使用 Source 将它们添加到全局环境中,最后在我的主文件上使用 Source 开始执行我的程序。 尝试通过以下方式构建二进制包时:

Build > Build Binary Package

我收到错误:

ERROR: The build directory does not contain a DESCRIPTION 
file so cannot be built as a package.

所以我创建了一个包,现在我得到的错误是

** preparing package for lazy loading
Error in reorderPopulation(pop_fitness_list) : 
  could not find function "reorderPopulation"
Error : unable to load R code in package 'EAtsp'
ERROR: lazy loading failed for package 'EAtsp'
* removing 'C:/Users/Ryan/AppData/Local/Temp/RtmpsXbv0j/temp_libpath27ec59515c59/EAtsp'
Error: Command failed (1)
Execution halted

Exited with status 1.

有人可以向我解释如何解决这个问题吗?

编辑:我已经为我的每个函数添加了roxygen注释,它们都显示在NAMESPACE文件中,但仍然存在相同的问题。

这些是我的 R 目录包含的文件:

fitness.r
initDataset.r
main.r
operators.r
selection.r

内的功能fitness.r可以找到main.r没有问题,所以我感动reorderPopulation这是以前在功能selection.rfitness.r ,它可以被发现。 为什么无法找到selection.r文件中的函数以及可能找不到其他函数?

没有什么可重现的,所以我将通过一个有效的黑客示例,也许您可​​以将其用作模板来解释什么是不同的以及为什么您的仍然可以工作。

./DESCRIPTION

Package: Porteous96
Title: This package does nothing
Version: 0.0.0.9000
Authors@R: person('r2evans', email='r2evans@ignore.stackoverflow.com', role=c('aut','cre'))
Description: This package still does nothing
Depends: R (>= 3.3.3)
License: MIT
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.0.1

(继续尝试在那里发送电子邮件......我认为它不会打扰我......)

./NAMESPACE

create

# Generated by roxygen2: fake comment so roxygen2 overwrites silently.
exportPattern("^[^\\.]")

document

# Generated by roxygen2: do not edit by hand

export(reorderPopulation)

(无论如何,这个文件不需要手动编辑,你需要使用假设roxygen2#' @export条款,或者你使用的是默认的‘出口几乎所有的东西’不roxygen2 。)

./R/reorderPopulation.R

#' Do or do not
#'
#' (There is no try.)
#' @param ... any arguments ultimately ignored
#' @return nothing, invisibly
#' @export
reorderPopulation <- function(...) {
  cat("do nothing\n")
  invisible(NULL)
}

unorderPopulation <- function(...) {
  reorderPopulation()
  cat("should not be found\n")
  invisible(NULL)
}

./R/zzz.R

我添加这个文件只是为了尝试从这个包中“找到”一个导出的函数。

.onLoad <- function(libname, pkgname) {
  reorderPopulation("ignored", "stuff")
}

我可以假设函数可用,每个?.onLoad

请注意,'.onLoad' 和 '.onUnload' 中的代码不应假设任何包,除了基本包在搜索路径上。 当前包中的对象将是可见的(除非绕过),但应导入其他包中的对象或应使用双冒号运算符。

构建和执行

我实际上是通过在预期目录中启动并运行创建的模板目录开始这项工作的:

devtools::create(".")
# Creating package 'Porteous96' in 'C:/Users/r2/Projects/StackOverflow'
# No DESCRIPTION found. Creating with values:
# Package: Porteous96
# Title: What the Package Does (one line, title case)
# Version: 0.0.0.9000
# Authors@R: "My Real Name <myreal@@email.address.com> [aut,cre]"
# Description: What the package does (one paragraph).
# Depends: R (>= 3.3.3)
# License: Call for information, please
# Encoding: UTF-8
# LazyData: true
# * Creating `Porteous96.Rproj` from template.
# * Adding `.Rproj.user`, `.Rhistory`, `.RData` to ./.gitignore

但是,您可以轻松地使用我上面提供的示例并继续前进,而无需调用create (它还包括一些其他文件,例如./.gitignore./Porteous96.Rproj./.Rbuildignore ,在我这里的其余过程中都不需要这些文件。如果你有它们并且它们有非默认值,这可能很好知道。)

从那里,我编辑/创建了上述文件,然后:

devtools::document(".")
# Updating Porteous96 documentation
# Loading Porteous96
# do nothing
# First time using roxygen2. Upgrading automatically...
# Writing NAMESPACE
# Writing reorderPopulation.Rd

(你在上面和下面看到“什么都不做”的原因是我把它放在一个名为.onLoad的函数中,每次加载库时触发。这包括在devtools::documentdevtools::install以及明显的library(Porteous96)

这样做的一个副作用是./man/目录与适用的帮助文件一起创建。 在这种情况下,单个文件reorderPopulation.Rd无需在此处显示。

devtools::install(".")
# Installing Porteous96
# "c:/R/R-3.3.3/bin/x64/R" --no-site-file --no-environ --no-save --no-restore  \
#   --quiet CMD INSTALL "C:/Users/r2/Projects/StackOverflow/Porteous96"  \
#   --library="C:/Users/r2/R/win-library/3.3" --install-tests 
# * installing *source* package 'Porteous96' ...
# ** R
# ** preparing package for lazy loading
# ** help
# *** installing help indices
# ** building package indices
# ** testing if installed package can be loaded
# *** arch - i386
# do nothing
# *** arch - x64
# do nothing
# * DONE (Porteous96)
# Reloading installed Porteous96
# do nothing

为了更好的衡量,我关闭 R 并重新打开它。 (一般不需要。)

library(Porteous96)
# do nothing

(同样,由于.onLoad ,这被转储到控制台。)

reorderPopulation()
# do nothing
unorderPopulation()
# Error: could not find function "unorderPopulation"
Porteous96:::unorderPopulation()
# do nothing
# should not be found

包起来

我猜这并不能解决您的问题。 它突出了我从您的问题中收集到的尽可能多的信息。 也许它提供了足够的框架,您可以在其中提及我的文件和您的文件之间的显着差异。 虽然答案不是为了解决方案前的讨论,但我认为它有时是必要和有用的。

在@r2evans 的帮助下,我设法找到了问题的解决方案。

我的main.r文件只是一堆没有函数包装它们的函数调用。 因此,我将函数调用包装在一个函数中,该函数现在如下所示:

mainFunction <- function() {

    source("R/initSetup.r")
    initSetup()

    ... 
}

initSetup.r包含更多对我使用的其他文件的source()调用。 然后在 R 控制台中使用命令 mainFunction() 运行该程序

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM