-1

I am currently trying to get my r-application running via shinyproxy in combination with docker - unfortunately without success.

The app works without problems when started via R-Studio. However, as soon as I create the Docker image and try to call it, I get the error message "function %>% not found".

I think that the packages are not loaded correctly, but I can't say why. In my Docker file I refer to an "installLibrarys.R" file, which contains all the libraries I need for the app. I have also adapted the rProfile file so that every R session should load all the libraries.

With a similar app, which was less extensive (but with an almost identical dockerfile as well as applications.yml), hosting via shinyproxy and docker worked without any problems....

da You can find my Docker file, my Rprofile.site, my application.yml and my installLibrarys.R at the end of the post.

Maybe someone has an idea what else I could do. If you need more info, feel free to contact me! :-)

Thank you and best regards

P.S.: I know that many libraries are "contained" in others and loaded unnecessarily - I haven't got around to sorting yet :D


Rprofile.site

    ##                      Emacs please make this -*- R -*-
## empty Rprofile.site for R on Debian
##
## Copyright (C) 2008 - 2018  Dirk Eddelbuettel and GPL'ed
##
## see help(Startup) for documentation on ~/.Rprofile and Rprofile.site

# ## Example of .Rprofile
# options(width=65, digits=5)
# options(show.signif.stars=FALSE)
# setHook(packageEvent("grDevices", "onLoad"),
#         function(...) grDevices::ps.options(horizontal=FALSE))
# set.seed(1234)
# .First <- function() cat("\n   Welcome to R!\n\n")
# .Last <- function()  cat("\n   Goodbye!\n\n")

# ## Example of Rprofile.site
# local({
#  # add MASS to the default packages, set a CRAN mirror
#  old <- getOption("defaultPackages"); r <- getOption("repos")
#  r["CRAN"] <- "http://my.local.cran"
#  options(defaultPackages = c(old, "MASS"), repos = r)
#})

## We set the cloud mirror, which is 'network-close' to everybody, as default
local({
    r <- getOption("repos")
    r["CRAN"] <- "https://cloud.r-project.org"
    options(repos = r)
})

  library("shiny")
  library("rwhatsapp") 
  library("dplyr")     
  library("stringr")    
  library("grid")       
  library("gridExtra")  
  library("cowplot")    
  library("magick")     
  library("ggmap")      
  library("stopwords")  
  library("tidytext")   
  library("stringi")    
  library("ggplot2")
  library("tidyr")
  library("lubridate")
  library("ggtext")
  library("lattice")
  library("extrafont")
  library("showtext")
  library("ggfittext")
  library("sysfonts")
  library("png")
  library("shinyBS")
  library("shinyWidgets")
  library("bslib")
  library("shinycustomloader")
  library("magrittr")
  library("knitr")

installPackages.R

r = getOption("repos")
r["CRAN"] = "http://cran.us.r-project.org"
options(repos = r)


  install.packages("shiny")
  install.packages("rwhatsapp") 
  install.packages("dplyr")     
  install.packages("stringr")    
  install.packages("grid")       
  install.packages("gridExtra") 
  install.packages("cowplot")    
  install.packages("magick")     
  install.packages("ggmap")      
  install.packages("stopwords")  
  install.packages("tidytext")   
  install.packages("stringi")    
  install.packages("ggplot2")
  install.packages("tidyr")
  install.packages("lubridate")
  install.packages("ggtext")
  install.packages("lattice")
  install.packages("extrafont")
  install.packages("showtext")
  install.packages("ggfittext")
  install.packages("sysfonts")  
  install.packages("png")
  install.packages("googleway")
  install.packages("shinyBS")
  install.packages("shinyWidgets")
  install.packages("bslib")
  install.packages("shinycustomloader")

  library(shiny)
  library(rwhatsapp)  
  library(dplyr)      
  library(stringr)    
  library(grid)       
  library(gridExtra)  
  library(cowplot)    
  library(magick)     
  library(ggmap)      
  library(stopwords)  
  library(tidytext)  
  library(stringi)    
  library(ggplot2)    
  library(tidyr)     
  library(lubridate)  
  library(ggtext)     
  library(lattice)    
  library(extrafont)    
  library(showtext)
  library(ggfittext)
  library(sysfonts)
  library(png)
  library(googleway)
  library(shinyBS)
  library(shinyWidgets)
  library(bslib)
  library(shinycustomloader)

Dockerfile

FROM rocker/r-base:latest
LABEL maintainer="USER <user@example.com>"
RUN apt-get update && apt-get install -y --no-install-recommends \
    sudo \
    libcurl4-gnutls-dev \
    libgtk2.0-dev\
    xvfb\
    xauth\
    xfonts-base\
    libcairo2-dev \
    libxt-dev \
    libssl-dev \
    libssh2-1-dev \
    libxml2-dev \
    libmagick++-dev\
        && rm -rf /var/lib/apt/lists/*
RUN install.r shiny
RUN echo "local(options(shiny.port = 3838, shiny.host = '0.0.0.0'))" > /usr/lib/R/etc/Rprofile.site
RUN addgroup --system app \
    && adduser --system --ingroup app app
WORKDIR /home/app
COPY app .
RUN Rscript installPackages.R

RUN chown app:app -R /home/app
USER app
EXPOSE 3838
CMD ["R", "-e", "shiny::runApp('/home/app')"]

application.yml

proxy:
  title: ShinyProxy
#  logo-url: https://link/to/your/logo.png
  landing-page: /
  favicon-path: favicon.ico
  heartbeat-rate: 10000
  heartbeat-timeout: 600000
  port: 8080
  container-wait-time: 60000
  admin-groups: admins
  hide-navbar: true
  # Docker configuration
  docker:
    cert-path: /home/none
    url: http://localhost:2375
    port-range-start: 20000
  specs:
  - id: 01_hello
    display-name: Hello Shiny App
    description: A simple reactive histogram
    container-image: #######:latest
    logo-url: ########
    access-groups: [admins, users]
  

logging:
  file:
    shinyproxy.log

spring:
  servlet:
    multipart:
      max-file-size: 2000MB
      max-request-size: 2000MB
LePyka
  • 181
  • 8
  • 2
    What does the code for your shiny app look like? Does your shiny code have `library(dpyr)`? Or are you trying to avoid that with the `Rprofile.site` file? I'm not clear where you are actually using that. Because in your dockerfile you have `RUN echo "local(options(shiny.port = 3838, shiny.host = '0.0.0.0'))" > /usr/lib/R/etc/Rprofile.site` which does not involve that other file at all. Did you mean to include that file in the dockerfile somewhere? – MrFlick Sep 16 '21 at 19:27
  • @MrFlick i just googled my problem and someone gave the advice to add the librarys into the Rprofile.site so i tried it :D I think the libraries are additionally loaded within the app - can this be the reason? – LePyka Sep 16 '21 at 19:33
  • 1
    But you didn't actually use that file any where. So you didn't actually do that. Your dockerfile doesn't use that file, it creates one with an echo statement. You could fix that, but generally it's better to explicitly put your library dependencies in the application code itself. But you haven't provided any of that code so it's not clear what's going on there. – MrFlick Sep 16 '21 at 19:35
  • @MrFlick my app consists of a relatively large number of files, each of which references itself - it would be difficult to upload all the data. But would you recommend loading the libraries directly in the code instead of using the installLibrarys.R file? – LePyka Sep 16 '21 at 19:38
  • 1
    Installing the packages is one thing, feel free to keep that. However, the environment in which that script runs is discarded by the time the image is finished, so any `library(XX)` calls in that script are short-lived. If you app needs a package, use `library(XX)` *in the app itself*; often I see a `global.R` file that has all of the `library(XX)` calls in it, and other files `source(.)` that as needed. – r2evans Sep 16 '21 at 19:59
  • @r2evans i have a function: "source("01-R/InstallPackages-v1.R", local = TRUE)$value #InstallLibrarys() GetLibrarys()" where my packages are loaded in the server.r-file – LePyka Sep 16 '21 at 20:06
  • I know nothing of the `InstallLibrarys()` and `GetLibrarys()` functions, nor what the "value" in `source(.)$value` is supposed to be doing (`source` operates solely in side-effect and returns `NULL`). – r2evans Sep 16 '21 at 20:15
  • @r2evans is there a easy possibility to share a zip-file containing my codes? – LePyka Sep 16 '21 at 20:21
  • Not natively within the Stack interface. – r2evans Sep 16 '21 at 20:24

1 Answers1

0

I have found the solution.

I loaded all libraries once at the start of the ui.r file - not via a function, but directly. Doesn't make the code any nicer, but it works :D

bad_coder
  • 11,289
  • 20
  • 44
  • 72
LePyka
  • 181
  • 8