应始终检查 RStudio connect 目前支持哪些选项:
https ://docs.rstudio.com/connect/admin/r/package-management/#private-packages
就我个人而言,我不喜欢包含本地/私有包的所有选项,因为它违背了部署闪亮应用程序的简单目标的目的。在许多情况下,我不能只在组织中设置本地存储库,因为我没有权限。我必须给 IT 支持发送电子邮件以让他们手动安装新软件包也很不方便。总的来说,我认为 RS connect 是一个很棒的产品,因为它很简单,但是当涉及到本地包时,它真的不是。
我找到了 Rstudio 官方推荐的一个不错的替代方案/Hack。我想thise也可以与shinyapps.io一起使用,但我还没有尝试过。解决方案如下:
添加到 global.R if(!require(local_package) ) devtools::load_all("./local_package")
编写一个复制所有源文件的脚本,这样您就可以得到一个带有本地包源目录的闪亮应用程序,您可以调用该目录,./inst/shinyconnect/
或者将本地包复制到./inst/shinyconnect/local_package
清单中。
将脚本添加./shinyconnect/packrat_sees_these_dependencies.R
到闪亮文件夹,这将由 packrat-manifest 拾取
破解 rsconnet/packrat 以在构建时忽略特定命名的包
(1)
#start of global.R...
#load more packages for shiny
library(devtools) #need for load_all
library(shiny)
library(htmltools) #or what ever you need
#load already built local_package or for shiny connection pseudo-build on-the-fly and load
if(!require(local_package)) {
#if local_package here, just build on 2 sec with devtools::load_all()
if(file.exists("./DESCRIPTION")) load_all(".") #for local test on PC/Mac, where the shinyapp is inside the local_package
if(file.exists("./local_package/DESCRIPTION")) load_all("./local_package/") #for shiny conenct where local_package is inside shinyapp
}
library(local_package) #now local_package must load
(3)
make script 加载你本地包的所有依赖。Packrat 会看到这一点。该脚本永远不会被实际执行。把它放在./shinyconnect/packrat_sees_these_dependencies.R
#these codelines will be recognized by packrat and package will be added to manifest
library(randomForest)
library(MASS)
library(whateverpackageyouneed)
(4)在部署期间,清单生成器 (packrat) 将忽略任何命名的 local_package 的存在。这是 packrat 中的一个选项,但 rsconnect 不公开此选项。一个技巧是将 rsconnect 加载到内存并修改 sub-sub-sub-functionperformPackratSnapshot()
以允许这样做。在下面的脚本中,我这样做并部署了一个闪亮的应用程序。
library(rsconnect)
orig_fun = getFromNamespace("performPackratSnapshot", pos="package:rsconnect")
#packages you want include manually, and packrat to ignore
ignored_packages = c("local_package")
#highjack rsconnect
local({
assignInNamespace("performPackratSnapshot",value = function (bundleDir) {
owd <- getwd()
on.exit(setwd(owd), add = TRUE)
setwd(bundleDir)
srp <- packrat::opts$snapshot.recommended.packages()
packrat::opts$snapshot.recommended.packages(TRUE, persist = FALSE)
packrat::opts$ignored.packages(get("ignored_packages",envir = .GlobalEnv)) #ignoreing packages mentioned here
print("ignoring following packages")
print(get("ignored_packages",envir = .GlobalEnv))
on.exit(packrat::opts$snapshot.recommended.packages(srp,persist = FALSE), add = TRUE)
packages <- c("BiocManager", "BiocInstaller")
for (package in packages) {
if (length(find.package(package, quiet = TRUE))) {
requireNamespace(package, quietly = TRUE)
break
}
}
suppressMessages(packrat::.snapshotImpl(project = bundleDir,
snapshot.sources = FALSE, fallback.ok = TRUE, verbose = FALSE,
implicit.packrat.dependency = FALSE))
TRUE
},
pos = "package:rsconnect"
)},
envir = as.environment("package:rsconnect")
)
new_fun = getFromNamespace("performPackratSnapshot", pos="package:rsconnect")
rsconnect::deployApp(appDir="./inst/shinyconnect/",appName ="shinyapp_name",logLevel = "verbose",forceUpdate = TRUE)