I am trying to deploy a Plumber API that calls python functions to R-Connect via the rsconnect::deployApp()
function in RStudio Pro 1.2.5.
I get errors that indicate python files are not found during deployment.
reticulate 1.18
python 3.7.7
R 4.0.2
plumber 1.0.0
Rstudio project structure
project dir
~/test_reticulate
~/test_reticulate/API/plumber.R
~/test_reticulate/python/two_dataframes.py
**My code to deploy to r-connect: **
rsconnect::deployApp(
appDir = "~/test_reticulate",
appPrimaryDoc = "PlumberAPI/plumber.R",
python = '/path/to/env/python',
account = "me",
server ="connect-<deletedtext>.com",
appName = "test_reticulate_1",
appTitle = "test_reticulate_1",
contentCategory = "api",
launch.browser = function(url) {
message("Deployment completed: ", url)
},
logLevel = "verbose"
)
PlumberAPI/plumber.R
library('plumber')
library('reticulate')
#reticulate by default creates virtualenv in home directory, not project directory. seems to work.
reticulate::use_virtualenv(virtualenv = "~/.virtualenvs/myvenv")
# Tried several different ways to import python. Works in Rstudio, seems to fail on deployApp().
import_from_path('two_dataframes', path="./python")
reticulate::source_python('python/two_dataframes.py')
#* @get /first_df
function(){
list_two_dfs <- get_list_dfs()
#return first dataframe
list_two_dfs[[1]]
}
/python/two_dataframes.py
import pandas_dataframe as pd_df
get_random_df = pd_df.get_random_df
def get_list_dfs():
df_list = [get_random_df(), get_random_df()]
return df_list
In the deploy logs,
[Connect] Completed packrat build against R version: '4.0.2'
[Connect] Bundle requested Python version 3.7.7; using /opt/python/3.7.7/bin/python3.7 which has version 3.7.7
[Connect] 2020/12/22 20:50:23.748944548 Running on host: kteusorprdcn
[Connect] 2020/12/22 20:50:23.748960471 Environment will be built with Python "3.7.7 (default, May 7 2020, 21:25:33) [GCC 7.3.0]" at /opt/python/3.7.7/bin/python3.7
[Connect] 2020/12/22 20:50:23.749202730 Running as user: rstudio-connect
[Connect] 2020/12/22 20:50:23.795201586 Using cached environment: 560zcqgpAFJQS9whHqK1vg
GET /__api__/tasks/ksUEWl4XDqSbC8hj?first_status=139 9ms
GET /__api__/tasks/ksUEWl4XDqSbC8hj?first_status=139 10ms
[Connect] 2020/12/22 20:50:25.176787877 Packages in the environment: appdirs==1.4.4, certifi==2020.4.5.1, cffi==1.14.0, chardet==3.0.4, colorful==0.5.4, conda==4.8.3, conda-package-handling==1.7.0, cryptography==2.9.2, distlib==0.3.1, filelock==3.0.12, idna==2.9, importlib-metadata==3.3.0, joblib==1.0.0, numpy==1.19.4, pandas==1.1.5, prettyprinter==0.18.0, pycosat==0.6.3, pycparser==2.20, Pygments==2.7.3, pyOpenSSL==19.1.0, PySocks==1.7.1, python-dateutil==2.8.1, pytz==2020.4, requests==2.23.0, ruamel-yaml==0.15.87, scikit-learn==0.24.0, scipy==1.5.4, six==1.15.0, threadpoolctl==2.1.0, tqdm==4.46.0, typing-extensions==3.7.4.3, urllib3==1.25.8, virtualenv==20.2.2, zipp==3.4.0,
[Connect] 2020/12/22 20:50:25.180609687 Creating lockfile: python/requirements.txt.lock
GET /__api__/tasks/ksUEWl4XDqSbC8hj?first_status=141 10ms
[Connect] Completed Python build against Python version: '3.7.7'
[Connect] Launching Shiny application...
GET /__api__/applications/118/config 14ms
Deployment completed: https://connect-<deletedtext>.com/connect/#/apps/118
----- Deployment log finished at 2020-12-22 20:50:27 -----
Warning messages:
1: invalid uid value replaced by that for user 'nobody'
2: invalid gid value replaced by that for user 'nobody'
When I go to https://connect-.com/connect/#/apps/118, there is always some sort of error where python files are not found, or do not exist.
2020/12/22 20:50:34.972173750 Using Packrat dir /opt/rstudio-connect/mnt/app/packrat/lib/x86_64-pc-linux-gnu/4.0.2
2020/12/22 20:50:38.729973447 Error in value[[3L]](cond) :
2020/12/22 20:50:38.729983385 Unable to open file 'python/two_dataframes.py' (does it exist?)
2020/12/22 20:50:38.730020440 Calls: local ... tryCatch -> tryCatchList -> tryCatchOne -> <Anonymous>
2020/12/22 20:50:38.730026329 Execution halted
I think I need to add either appFiles or parameters to the deployApp() function, but the docs don't say what structure the params should be (a string? a list? a vector?)
https://rdrr.io/cran/rsconnect/man/deployApp.html
rsconnect::deployApp(
appFiles = c("PlumberAPI/plumber.R", "python/two_dataframes.py", "python/pandas_dataframe.py"),
appDir = "~/test_reticulate",
appPrimaryDoc = "PlumberAPI/plumber.R",
python = '/path/to/env/python',
account = "me",
server = "connect-<deletedtext>.com",
appName = "test_reticulate_1",
appTitle = "test_reticulate_1",
contentCategory = "api",
launch.browser = function(url) {
message("Deployment completed: ", url)
},
logLevel = "verbose"
)
Any suggestions would be greatly appreciated. Reticulate and plumber both work well when I Run_API in local RStudio Server Pro.