我目前正在开发一个名为“package_name”的 Golem Shiny 应用程序(这是我的要求),我创建的一些函数需要使用
{furrr}
和 {future}
包中的函数。
但是,每当我尝试运行它们时,我都会收到以下错误:
错误:没有名为“package_name”的包
请注意,只要任何不使用任一包的函数都可以正常工作。
有人知道问题出在哪里吗?
谢谢!
使用
{golem}
构建应用程序时,包含该应用程序的软件包未安装在您的计算机上。
当您使用 {future}
时,代码在 另一个 R 会话 中运行,这意味着对象被传输并重新加载库。
但是,如果您尝试在未来使用当前应用程序中的功能,则需要使其“可移植”,并且使用
package_name::function()
将不起作用,因为您的软件包尚未安装。
假设您需要使用包内定义的
current_app_fun()
。
从技术上讲,{future}
将能够传输此函数,因为它使用 {globals}
来识别要传输到新 R 会话的对象。
observeEvent( input$bla , {
# future() will identify that it needs to
# transport current_app_fun()
future({
current_app_fun()
})
})
您还可以执行额外的步骤以格外小心:
observeEvent( input$bla , {
func_for_future <- current_app_fun
future({
func_for_future()
})
})
干杯, 科林
解决方案来自这里:https://github.com/r-lib/devtools/issues/1741
你应该添加
devtools::install_local(".")
在你的 run_dev.R 脚本中
事实是 plan(multisession) 不能与 devtools::load_all 一起使用,而 devtools::load_all 正是在后台运行 Golem 的。
我的 run_dev.R 脚本看起来像最后那样:
# Set options here
options(golem.app.prod = FALSE) # TRUE = production mode, FALSE = development mode
# Comment this if you don't want the app to be served on a random port
options(shiny.port = httpuv::randomPort())
# Detach all loaded packages and clean your environment
golem::detach_all_attached()
# rm(list=ls(all.names = TRUE))
# Document and reload your package
golem::document_and_reload()
# Install the dev package so that it is found by future session
devtools::install_local(".", upgrade = "never", force = TRUE)
# Run the application
run_app()