简体   繁体   English

由于 reticulate_python 部署闪亮应用程序的问题

[英]Problems with deployment of shiny app due to reticulate_python

I have an app that I want to deploy on shinyapps.io.我有一个应用程序,我想在 Shinyapps.io 上部署。 I think it is important to note that in the app data is extracted from a database in athena.我认为重要的是要注意应用程序中的数据是从 athena 中的数据库中提取的。 I used the package Rathena to connect to the database and everything used to run fine locally from my computer.我使用Rathena包连接到数据库,并且所有东西都可以从我的计算机本地正常运行。 However I tried to deploy it on shinyapps.io and I get a message that an error has occurred.但是,我尝试将它部署在 Shinyapps.io 上,但收到一条消息,指出发生了错误。 I see this simply in the window that pops up after I deployed it.我只是在部署后弹出的窗口中看到了这一点。 I went and looked at the log section from shinyapps.io and I see the message that says:我去查看了 Shinyapps.io 的日志部分,我看到了一条消息:

warning: using reticulate but python was not specified;警告:使用网状但未指定python; will use python at /usr/bin/python3 Did you forget to set the RETICULATE_PYTHON environment variable in your .Rprofile before publishing?将在 /usr/bin/python3 使用 python 您是否忘记在发布前在 .Rprofile 中设置 RETICULATE_PYTHON 环境变量?

I have never used an .Rprofile and I don't know what it means by RETICULATE_PYTHON.我从未使用过 .Rprofile 并且我不知道 RETICULATE_PYTHON 的含义。 Anyways they say on stack overflow google is your best friend.无论如何,他们说堆栈溢出谷歌是你最好的朋友。 I found this tutorial on Github called Tutorial: using Shiny + reticulate to create apps with R and Python 3 .我在 Github 上找到了名为Tutorial: using Shiny + reticulate to create apps with R and Python 3 的教程 I did the initial steps like it says to and then it says at the end:我做了它所说的初始步骤,然后它在最后说:

Confirm that the .Rprofile file is included in your project's directory and was deployed along with server.R and ui.R to shinyapps.io.确认 .Rprofile 文件包含在您的项目目录中,并与 server.R 和 ui.R 一起部署到 Shinyapps.io。 This file sets the RETICULATE_PYTHON environment variable, which tells reticulate where to locate the Python virtual environment on the shinyapps.io servers.这个文件设置了 RETICULATE_PYTHON 环境变量,它告诉 reticulate 在 Shinyapps.io 服务器上找到 Python 虚拟环境的位置。

I looked up what .Rprofile is and I think it's just a script for code but It doesn't tell me what code I should put there.我查了一下 .Rprofile 是什么,我认为它只是一个代码脚本,但它没有告诉我应该把什么代码放在那里。 I think I need to mention that package Rathena uses Python code to connect to the database.我想我需要提一下,包Rathena使用 Python 代码连接到数据库。

I also read somewhere that I should place this line RETICULATE_PYTHON = /usr/local/bin/python3 in my .Renviron file.我还在某处读到我应该将此行RETICULATE_PYTHON = /usr/local/bin/python3放在我的 .Renviron 文件中。 I did this and now when I try to run the app locally on my computer I get the error message:我这样做了,现在当我尝试在我的计算机上本地运行该应用程序时,我收到错误消息:

Error: Boto3 is not detected please install boto3 using either: pip install boto3 numpy in terminal or install_boto() .错误:未检测到 Boto3 请使用以下任一方法pip install boto3 numpypip install boto3 numpy in terminal 或install_boto() If this doesn't work please set the python you are using with reticulate::use_python() or reticulate::use_condaenv()如果这不起作用,请使用reticulate::use_python()reticulate::use_condaenv()设置您正在使用的 python

This doesn't make sense because I already have installed boto3 and even in the terminal it says这没有意义,因为我已经安装了 boto3,甚至在终端中它说

Requirement already satisfied: boto3 in ./Library/r-miniconda/envs/r-reticulate/lib/python3.6/site-packages (1.14.28)要求已经满足:boto3 in ./Library/r-miniconda/envs/r-reticulate/lib/python3.6/site-packages (1.14.28)

I feel like I am going in circles here.我觉得我在这里兜了一圈。

This is actually a fairly annoying problem with running software locally and running in another environment and I'd say solutions are generally non-obvious.这实际上是在本地运行软件并在另一个环境中运行的一个相当烦人的问题,我想说解决方案通常不明显。 You probably aren't the first person to run into this.你可能不是第一个遇到这种情况的人。

Here are some facts that I think are relevant:以下是我认为相关的一些事实:

  • reticulate needs to know where your Python executable is on the computer reticulate需要知道你的Python可执行文件在计算机上
  • The locatl of the Python executable differs between your computer and the virtual computer shinyapps.io runs your Shiny app from Python 可执行文件的 locatl 在您的计算机和虚拟计算机 Shinyapps.io 之间不同
  • reticulate seems to determine where your Python executable is from the RETICULATE_PYTHON environment variable but also has some default behavior when the variable isn't set reticulate似乎确定您的 Python 可执行文件来自RETICULATE_PYTHON环境变量的位置,但在未设置变量时也有一些默认行为
  • The Python executable on your computer is at a different location than the executable on the virtual computer shinyapps.io runs your Shiny app from您计算机上的 Python 可执行文件与虚拟计算机上的可执行文件位于不同的位置 Shinyapps.io 运行您的 Shiny 应用程序
  • When you set the value of RETICULATE_PYTHON in your .Rprofile to the value shinyapps.io needs, your setup breaks because your Python environment is different当您设置的值RETICULATE_PYTHON.Rprofile的价值shinyapps.io需求,您的设置休息,因为你的Python环境是不同的

I haven't tested this as a solution but I think the fix is actually in your link!我尚未将此作为解决方案进行测试,但我认为该修复程序实际上在您的链接中! Check out https://github.com/ranikay/shiny-reticulate-app/blob/master/.Rprofile and see how they include a bit more than just a simple assignment of a value to RETICULATE_PYTHON and actually use conditional statements to set various values depending on the result of (Sys.info()[['user']] .查看https://github.com/ranikay/shiny-reticulate-app/blob/master/.Rprofile并了解它们如何包含不仅仅是简单地为RETICULATE_PYTHON赋值,并且实际使用条件语句来设置各种值取决于(Sys.info()[['user']]

The value of (Sys.info()[['user']] will differ based upon whether R executes .Rprofile on your local computer versus the virtual computer shinyapps.io runs your Shiny app on and the linked .Rprofile is doing the work of setting up various values based upon whether R is running on your local computer or on another computer. This all works because shinyapps.io (Sys.info()[['user']]将根据 R .Rprofile在您的本地计算机上执行.Rprofile在虚拟计算机 Shinyapps.io 上运行您的 Shiny 应用程序以及链接的.Rprofile正在执行工作而.Rprofile根据 R 是在本地计算机上运行还是在另一台计算机上运行来设置各种值。这一切都有效,因为 Shinyapps.io

From the code in the .Rprofile, I can tell that shinyapps.io runs your Shiny app under the user named "shiny",从 .Rprofile 中的代码,我可以看出 Shinyapps.io 在名为“shiny”的用户下运行您的 Shiny 应用程序,

if (Sys.info()[['user']] == 'shiny'){
  Sys.setenv(RETICULATE_PYTHON = paste0('/home/shiny/.virtualenvs/', VIRTUALENV_NAME, '/bin/python'))
}

and they even add a conditional block in their to support RStudio Connect, which apparently runs your Shiny apps under the user named rstudio-connect instead of "shiny":他们甚至在其中添加了一个条件块来支持 RStudio Connect,它显然在名为rstudio-connect而不是“shiny”的用户下运行您的 Shiny 应用程序:

} else if (Sys.info()[['user']] == 'rstudio-connect'){
  Sys.setenv(RETICULATE_PYTHON = paste0(VIRTUALENV_NAME, '/bin/python'))
}

Lastly, the .Rprofile uses an else statement to catch any other case and this will match the case when you're running on your own computer:最后, .Rprofile使用else语句来捕获任何其他情况,这将与您在自己的计算机上运行时的情况相匹配:

} else {
  # RETICULATE_PYTHON is not required locally, RStudio infers it based on the ~/.virtualenvs path

From what you've described above and what I see at the linked tutorial , I think you could nearly copy the entire .Rprofile they have there into your project and, with maybe only a minor modification or two, you should be able to get your Shiny app running locally and on shinyapps.io with reticulate .根据您上面的描述以及我在链接教程中看到的内容,我认为您几乎可以将他们拥有的整个.Rprofile复制到您的项目中,并且可能只需要一两个小的修改,您就应该能够获得您的在本地和 Shinyapps.io 上使用reticulate运行的 Shiny 应用程序。

However, we see that not setting RETICULATE_PYTHON defers to behavior defined by running a virtualenv so I might recommend setting RETICULATE_PYTHON here to a value such as the location or your miniconda-based Python executable.然而,我们看到,没有设置RETICULATE_PYTHON推迟到行为通过运行virtualenv中定义的,所以我可能会建议设置RETICULATE_PYTHON这里的值,如地点或基于miniconda的Python可执行文件。 I'm guessing that value might be ./Library/r-miniconda/envs/r-reticulate/bin/python3 or similar.我猜这个值可能是./Library/r-miniconda/envs/r-reticulate/bin/python3或类似的。

If you don't want to copy the linked .Rprofile and modify it as you need, you might just change yours to only contain this line:如果您不想复制链接的.Rprofile并根据需要修改它,您可以将您的更改为仅包含以下行:

if (Sys.info()[['user']] == 'shiny'){
  Sys.setenv(RETICULATE_PYTHON = "/usr/local/bin/python3")
}

And then try running locally and deploying.然后尝试在本地运行并部署。

This pattern of setting environment variables differently based upon the environment a program is running under is actually a fairly widely-accepted best practice in software engineering [1] so it's a pattern that may serve you in the future as you build applications locally and deploy to other environments.这种根据程序运行的环境以不同方式设置环境变量的模式实际上是软件工程中相当广泛接受的最佳实践 [1] 因此,当您在本地构建应用程序并将其部署到其他环境。

[1] https://12factor.net [1] https://12factor.net

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

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