Web application development with R using Shiny new version of code

The new version of Shiny (0.9) is wonderful, hopefully I will talk about it again soon, but it does break the code in my book, Web application development with R using Shiny. It’s only a small difference, the selectInput() function used to ask for preselected elements to be given as the name of a list (i.e. the friendly version that appears to users in the UI, “Egg Salad”) whereas now it asks for the actual value (“eggSalad”, e.g.).

I wrote the book with 0.7 and I want to stay true to what’s in the book so I’ve added a couple of lines in to the Google Analytics Advanced application to make it work with 0.9 and 0.7.

The code now starts with a version check like this:


# test - are they using the older version of Shiny?

packageCheck = unlist(packageVersion("shiny"))

if(packageCheck[1] == 0 & packageCheck[2] < 9){
  
  shinyOld = TRUE
} else {
  shinyOld = FALSE
}

And there is one place in server.R and one in ui.R that checks whether that is true or false and changes its behaviour accordingly. It’s probably quicker for you to just control-F to shinyOld once in each codefile than have me tell you where they are.

As far as I can tell it now works beautifully on 0.9, please do let me know on here or on Twitter @chrisbeeley and I’ll be glad to have a look. Or if you have any other questions or comments I’m always glad to talk about them.

Shiny server working on Windows based virtual machine

Working in the NHS as I do, which runs all of its IT on Windows, I’d always despaired of being able to get Shiny Server working. However, I went to see the IT department and they have very kindly given me access to a virtual machine with Ubuntu 12.04.04 LTS running on it. It’s completely firewalled from the internet and is only visible from within the network, which is quite useful because it means I can put up things on it which are not for public consumption.

So the question was, will it run Shiny server without any problems? I’m very happy to say yes. This is a great development since it means I can run Shiny Server behind our corporate firewall and share things with people in the organisation with it very easily. Here’s a quick guide to what I did to get it working. I can’t say for sure if it will all work for you, depends on ports and probably lots of random other things, but it can’t hurt to try.

First job, of course, is to install R. Follow the link for a great summary of installing at the command line.

DON’T, as the Shiny Server documentation suggests, just run apt-get install r-base because the version in the repository is only 2.14 for which a lot of important packages are not available

I found on an NHS network that I had port problems, so instead of running:


gpg --keyserver keyserver.ubuntu.com --recv-key E084DAB9

Run this instead.


gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-key E084DAB9

Worked for me anyway.

Now run:


sudo apt-get update

For me, this generated an error (again because of port problems):

GPG error: […] The following signatures couldn’t be verified because the public key is not available: NO_PUBKEY 51716619E084DAB9

So I ran this:


sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 51716619E084DAB9

Now install:


sudo apt-get install r-base-dev

Now let’s install Shiny Server, follow the instructions here.

Boom. Navigate to 127.0.0.1:3838 (put your actual IP in here, obviously) and you should find the example application, give it a test, and you’re off.

Once you start running applications you will no doubt need to install R packages, I find it easier to launch R, run .libPaths() and make myself the owner of one of these directories:


sudo chown chrisbeeley /usr/lib/R/site-library/

That way you can just run install.packages(“ggplot2”) from within R rather than faffing around with R CMD INSTALL and all the packages will install into the site library.

Enjoy!