将闪亮的应用程序作为软件包开发,并将其部署到闪亮的服务器 [英] developing shiny app as a package and deploying it to shiny server
问题描述
我正在开发一个闪亮的应用程序,并且由于我想使用该功能的自动化测试和文档,所以我开始在包中开发界面(如推荐此处)。
I am developing a shiny app and since I wanted to use automated testing and documentation of the function, I started to develop the interface within a package (as recommended here).
我在RStudio中开发了这个闪亮的应用程序,并且有一个 server.R
文件,可以单击RStudio中的 Run App (运行应用程序)按钮。一切正常。我将软件包提交到github存储库,从这一点开始,我想使用 devtools
和 R闪亮服务器上> install_github
函数。
I develop this shiny app within RStudio and I have a server.R
file which I can click on the Run App button within RStudio and everything works. I commit my package to a github repository and from this point I want to install it on my R shiny server using devtools
and install_github
function.
现在我想知道如何在服务器中启动我的应用程序。我天真地想安装该软件包并指向 server.R
文件,但这在某种程度上是行不通的。不知道问题出在哪里。
Now I am wondering how to start my app within the server. I was naively thinking to install the package and pointing to the server.R
file, but this is somehow not working. Not sure where the problems are.
我的第二次尝试是在可访问的文件夹中创建名为 app.R
的文件通过闪亮的服务器,在这个文件中,我基本上加载了我的程序包,闪亮的程序和其他需要的程序包,但是由于缺少变量而以某种方式抱怨...
My second try was creating a file called app.R
in a folder accessible by the shiny server and in this file I basically load my package, shiny and other needed packages but it somehow complains because of missing variables...
有人可以给我一个建议?我也很高兴回答任何问题,因为我不确定如何正确地提出这个问题。
预先感谢。
Can somebody give me an advice? I am also happy to answer any question since I am not sure how to ask for this problem properly. Thanks in advance.
EDIT部署到闪亮的服务器
由于 Colin D 在评论中询问,如何在闪亮的服务器上部署这些软件包,所以我想演示如何做到这一点。
Since Colin D was asking in the comments, how to deploy these packages on a shiny server, I wanted to demonstrate how I do this.
首先,我使用以下命令通过命令行以 root 的身份将软件包直接安装在闪亮的服务器上。
First of all I install my package on the shiny server directly via the command line as root with the following command.
su - -c "R -e \"devtools::install_github('user/shinypackage')\""
下一步是更改软件包文件夹的所有者
The next step is to change the owner of the package folder
chown -R shiny:shiny /usr/local/lib/R/site-library/shinypackage/
然后我重新启动闪亮的
systemctl restart shiny-server
这些是我更新闪亮的应用程序时要执行的步骤。我通常以单行root身份再次执行此操作
These are the steps I do when I update my shiny app. I do this normally again as root in a single line
su - -c "R -e \"devtools::install_github('user/shinypackage')\"" & chown -R shiny:shiny /usr/local/lib/R/site-library/shinypackage/ & systemctl restart shiny-server
我们仍然需要做的一件事是在 shiny-server.conf
文件。我刚刚添加了软件包路径+应用程序目录,其中保存了 ui.R
和 server.R
。
One thing we still need to do is to setup the directory in the shiny-server.conf
file. I just added the package path+the application directory where the ui.R
and server.R
is saved.
location /shinypackage {
app_dir /usr/local/lib/R/site-library/shinypackage/application;
log_dir /var/log/shiny-server;
}
然后我必须使用重新启动服务器systemctl restart Shiny-server
。
这是在Ubuntu服务器上使用的。
This is in use on a Ubuntu Server.
推荐答案
当我将闪亮的应用程序作为独立程序包制作时,通常会这样组织文件:
When I make shiny applications as a stand-alone package, I usually organize the files as so:
在R中目录:
- 我所有用于支持应用程序的方法(如果将这些方法用于
ui.R
,server.R
或global.R
文件) - A
launch_application
函数
- All of my methods to support the application (these should be exported if they will be used in either the
ui.R
,server.R
, orglobal.R
files) - A
launch_application
function
定义启动应用程序
的内容类似于:
launch_application <- function(x, ...)
{
shiny::runApp(appDir = system.file("application", package = [my_pkg]),
...)
}
在inst目录中
-
应用程序/服务器.R
-
应用程序/ui.R
-
application / global.R
application/server.R
application/ui.R
application/global.R
构建后并安装软件包,然后只需要运行
After building and installing the package, I then just need to run
library(my_pkg)
launch_application(...)
这篇关于将闪亮的应用程序作为软件包开发,并将其部署到闪亮的服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!