Installing Shiny Server on Mac OS X
Shiny is a great way to quickly build interactive web apps from R. Running an app in local mode from R is easy but sharing it with a larger audience requires Shiny Server. At the moment, pre-built binaries are available only for Linux (Red Hat and Ubuntu). Since Mac OS X has its roots in Unix, I decided to investigate the possibility of compiling it from source on that platform. Turns out it works quite well but the process is somewhat involved. I’m documenting the steps below in case anyone comes across the same difficulties.
brew install python
With these in place, you must now install Shiny in the system-wide library. This can be done by running R as root, then typing:
install.packages("shiny", repo="http://cran.rstudio.org", type="source")
It needs to be installed from source since no pre-compiled binary is available for the development version of R (the same is true for any other package).
With this done, you can proceed with the steps under “Installation” on the official page BUT stop before the “cmake” step. The current launcher.cc source file must be edited to use the proc_pidpath() function on OSX instead of Linux proc (see this thread). Use this version from Nathan Weeks instead. After replacing the file, you can proceed with cmake and all subsequent installation steps.
The compilation should succeed and at this point you have a functional binary of shiny-server. It still needs to be configured though, using the “Post-Install” instructions from the official page. On my system, the default configuration file was apparently not installed by the build script – just copy and paste it as /etc/shiny-server/shiny-server.conf . There is also no “useradd” command on Mac OS X to add the necessary “shiny” user: adding a user from the command line requires several calls to “dscl” instead. This script makes it much easier and can be used to create the user.
After all the “Post-Install” steps, you should be able to run the server by simply typing:
Go to the address returned (e.g. http://0.0.0.0:3838) to make sure everything is fine (it should be a simple “Index of” blank page at this stage). You can now move your Shiny app(s) in /srv/shiny-server , following the same structure as on your local machine (directory named after the app, then at a minimum UI.R and server.R inside). If your app is simple, it should just run. However, if like mine it requires packages beyond the base R distribution, you need to make sure they are installed first. That means another “sudo R” and calls to
install.packages("[PACKAGE_NAME_HERE]", repo="http://cran.rstudio.org", type="source")
as necessary. You may run into additional difficulties here. In particular, if the source for your package is built using Fortran (such as “rworldmap“, which I am using), you’ll need to install the compiler:
brew install gfortran
The source for rworldmap calls gfortran-4.8 explicitly – to avoid compilation error, you can symlink to the brew install:
ln -s /usr/local/bin/gfortran /usr/local/bin/gfortran-4.8
You will also need to add the lgfortran location explicitly to your path:
Your mileage may vary depending on the dependencies of your apps and packages but overall, it is clearly feasible to compile and run Shiny Server on Mac OS X. Back to developing my apps!