Garrett Catlin

August 18, 2022

Running R Shiny Applications from Zip Files

I've been tooling around with R Shiny for a little over a year now and while it hasn't always been a love story, I've sure learned a lot. If built with a focus on the user, I believe R Shiny applications can be incredible communication tools conveying the results of complex statistical modeling in a way that the uninitiated can understand.

I don't expect everyone to hang with the jargon involved with my Bayesian models, cluster solutions, discrete event simulations, and so on. I do, however, expect that most folks can see the effects of dragging a slider. This thought makes the part of my brain that wanted to get (and did get) a minor in technical writing very happy. Good communication, in my view, should be prized in any avenue it appears - R Shiny is no exception!

However, I have occasionally made apps that contain sensitive information. Publishing them on the internet whether through shinyapps.io  or otherwise is a no-go. What are my options? I could certainly go with an in-house server, but I don't need to do this often enough to warrant that (which is another way of saying it scares me). Surely, there must be a simpler solution?

Something that I've cobbled together is a function that will run a Shiny app out of a .zip file. I was surprised this was not a default functionality of shiny::runApp() so here we are! In tandem with rstudioapi::selectFile(), I find that most folks can get it working pretty easily:

runZip = function(filePath) {
  zipfiles = as.character(unzip(filePath, list=TRUE)$Name)
  dirname = dirname(zipfiles[grep('/server\\.[rR]$', zipfiles)])
  unzip(filePath, exdir = dirname(filePath))
  appdir <- file.path(dirname(filePath), dirname)
  on.exit(unlink(appdir, recursive = TRUE), add = TRUE)
  shiny::runApp(appdir)
}

runZip(rstudioapi::selectFile())

Of course, the user will need R & RStudio installed as well as any packages your app requires installed. It's probably a good idea to put any package the app uses at the top of a script so hopefully RStudio will yell at them if they don't have the necessary packages. You could probably implement this more cleanly in terms of having the user not have to open RStudio to begin with but that also pushes my scripting knowledge. So, I find this to be a decent solution for the time being!

200.gif


Hoping yours is a .zip-a-dee-doo-dah day,

- Garrett C.
catlin@hey.com

About Garrett Catlin

Your friendly neighborhood data scientist.