How to install packages through `packarat` while creating shiny Docker image.

Hi, I use below Dockerfile to create the docker image for word-cloud shiny app. I explicitly use packrat to make sure that package version remain same. It executes all steps without error and creates image successfully. However, when i run the docker image, it throws an error that package not found as soon as it hit the first library call. I suspect it might due to incorrect installation path. Can any tell possible issue in the Dockerfile ??

## shiny server with specific r version 
FROM rocker/shiny:latest

## install libcurl4 and libv8
RUN apt-get update &&\
apt-get install libcurl4-openssl-dev libv8-3.14-dev  libssl-dev libxml2-dev libxslt-dev -y &&\
  mkdir -p /var/lib/shiny-server/bookmarks/shiny
  
  
## Download and install library
RUN R -e "install.packages(c('packrat'),  dependencies = TRUE, repos='http://cran.rstudio.com/')"

## run copy the app to the image 
COPY shiny-wordcloud /srv/shiny-server/

# copy lock file & install deps
COPY  packrat/packrat.* /srv/shiny-server/shiny-wordcloud/packrat/
RUN R -e 'packrat::restore(project="/srv/shiny-server/shiny-wordcloud");'


## make all app files readable (solves issue when dev in Windows, but building in Ubuntu)
RUN chmod -R 755 /srv/shiny-server/


## make app file readable
RUN chmod -R +r /srv/shiny-server/

EXPOSE 3838

CMD ["/usr/bin/shiny-server.sh"]

I wonder if you are not missing the .Rprofile packrat uses to automatically call packrat::on() when R is launch with the packrat projet as working dir.

Locally, you should have in your packrat folder a file *.opts and init.R, the last one is used by the R profile to activate the packrat mode.

Or maybe it is just the call to packrat::on(). Currently there is nothing in your dockerfile that activate he packrat mode to make the app use the packrat library instead of the global library.

I'll try to find my last dev where I use this to see exactly the commands I used.

Also, here is an article that could help

Also, know that packrat V2 will be call renv

Thanks. I changed the Dockerfile as per the steps given in the link you shared. However, I still get error while generating the image. Again, it seems like path issue. The new Dockerfile and error given below.

## shiny server with specific r version 
FROM rocker/shiny:latest

## install libcurl4 and libv8
RUN apt-get update &&\
apt-get install libcurl4-openssl-dev libv8-3.14-dev  libssl-dev libxml2-dev libxslt-dev -y &&\
  mkdir -p /var/lib/shiny-server/bookmarks/shiny
  
  
## Download and install library
RUN R -e "install.packages(c('packrat'),  dependencies = TRUE, repos='http://cran.rstudio.com/')"

## run copy the app to the image 
#COPY shiny-wordcloud /srv/shiny-server/

## copy bundle to server 
COPY packrat/bundles/docker_test-2019-04-04.tar.gz /srv/shiny-server/

## change directory 
RUN cd /srv/shiny-server/

## unbundle 

RUN R -e 'packrat::unbundle("/srv/shiny-server/docker_test-2019-04-04.tar.gz", where = "/srv/shiny-server/")'
RUN R -e 'packrat::on(project = "/srv/shiny-server/docker_test")'

## make all app files readable (solves issue when dev in Windows, but building in Ubuntu)
RUN chmod -R 755 /srv/shiny-server/

## make app file readable
RUN chmod -R +r /srv/shiny-server/

EXPOSE 3838
CMD ["/usr/bin/shiny-server.sh"] 


########################

> packrat::on(project = "/srv/shiny-server/docker_test")
Error in read.dcf(...) : cannot open the connection
Calls: <Anonymous> ... getPackageDependencies -> readLockFilePackages -> readDcf -> read.dcf
In addition: Warning message:
In read.dcf(...) :
  cannot open compressed file '//packrat/packrat.lock', probable reason 'No such file or directory'
Execution halted

Finally...... !!!!!!! i managed to solve the issue by changing the the following line

RUN R -e 'packrat::on(project = "/srv/shiny-server/docker_test")'

to

RUN R -e 'packrat::on(project = "/srv/shiny-server/docker_test/shiny-wordcloud")'

I still don't understand the logic though. :frowning:

I found the lines I used:

RUN R -e "packrat::unbundle('/tmp/bundleapp.tar.gz', '/srv/shiny-server/')" \
    && R -e "setwd('/srv/shiny-server/appnameinsidetargz');source('.Rprofile');packrat::on()" 

I did this because there was an issue with packrat::on(project = "")

I see it is working with you :confused:

the logic is you need to activate the packrat project so that the library project are used. If you do not, R cannot guess that you don't want to use the default .libPaths() but want to use a project .libPaths().

I believe all this will be improve with renv

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.