What is packrat?
packrat
is a dependency management system for R, and could be used to isolate package libraries for a specific project/application from the rest of the system. This is particularly useful when applications are being deployed to Shiny Server (Pro) that require a particular version of certain package(s) while the packages installed globally have a different version.
For more information about packrat please refer to the following:
Prepare your packrat project
Let's assume you have a shiny application on your desktop located at ~/applications/my-shiny-app
.
While you are still working on this application, or when you are done with it, you can convert it into a packrat project using the following command: (Please refer to packrat documentation for more info.)
packrat::init("~/applications/my-shiny-app")
When you are ready to deploy your application to Shiny Server (Pro), use the following commands while you are in the packrat project (your application directory):
packrat::snapshot()
packrat::bundle()
The last command creates a zipped bundle of your application within the packrat directory of your project. This bundle is now ready to be transported to another system.
Deploying to Shiny Server (Pro)
The zipped bundle that you created on your desktop could now be copied to the physical machine where Shiny Server (Pro) is running. You need to have the packrat package installed on that machine.
Let's assume you have copied the bundle to this location: ~/Downloads/my-shiny-app-<date>.tar.gz
Open RStudio IDE or an R session on this machine, and run the following command to unbundle the project/application into the specified project directory:
packrat::unbundle("~/Downloads/my-shiny-app-<date>.tar.gz", "~/ShinyApps")
A new project directory called my-shiny-app
is now created with all the contents of your original packrat application/project.
Change your working directory to this project directory:
setwd("~/ShinyApps/my-shiny-app")
And now run the following command to enable packrat
mode:
packrat::on()
Here we are assuming that your Shiny Server (Pro) is configured to run in user_dirs
mode, and your packrat project/application can be served from your ~/ShinyApps
directory. With this assumption, you can now point your browser to this application. You can use a URL such as:
http://localhost:3838/username/my-shiny-app
If your Shiny Server (Pro) is configured to run in site_dir
mode, assuming your site_dir
is pointing to /srv/shiny-server
, you can copy the packrat application directory to that location. For example:
cp -r ~/ShinyApp/my-shiny-app /srv/shiny-server
Now you can access your application from the site_dir
location using a URL such as:
http://localhost:3838/my-shiny-app
If you notice permission errors when you load the application for the first time, make sure that the destination directory /srv/shiny-server/my-shiny-app
has write permissions for the shiny
user (or whichever user your R process will be running as). This should not happen if you have run the previous steps correctly, but just in case.
sudo chown -R shiny:shiny /srv/shiny-server/my-shiny-app
Comments