Turn R (Shiny) Scripts Into Double-clickable OS X Applications With One Line of Code

November 30, 2014
By

(This article was first published on Data Driven Security, and kindly contributed to R-bloggers)

I was playing with some non-security-oriented R+Shiny code the other day, and thought that Shiny apps would be even more useful if they were double-clickable applications that you could “just run”—provided R was installed on the target system—vs have to cut/paste code into R. Now, I know it’s not hard to enter:

shiny::runGist('95ec24c1b0cb433a76a5', launch.browser=TRUE)

at an R console, but I’ll wager many developers have users that would still appreciate a double-clickable icon. Since I’m running OS X Yosemite on some of my development machines, I thought this would be a good reason to try out Apple’s new Javascript for Applications (JXA) since I am loath to work in regular AppleScript.

If you fire up Apple’s Script Editor, you can choose which language your script is in from the popup on the script window:

With JXA, all you need is one line of code to run a Shiny gist in R:

Application("R").cmd("shiny::runGist('95ec24c1b0cb433a76a5', launch.browser=TRUE)")

If you like/prefer “pure” AppleScript or are on an older version of OS X you still only need four lines of code:

tell application "R"
  activate
    cmd "shiny::runGist('95ec24c1b0cb433a76a5', launch.browser=TRUE)"
end tell

Save the script as an application and your users will be greeted with your Shiny app in their default browser.

Caveat Scripter

When an application is created this way, it quits immediately after launching R.app and then the R.app window is left open (showing all the R console output from the script). I personally think this is A Very Good Thing, but some folks may not, so you can miniaturize it upon startup via:

R = Application("R")
R.windows[0].miniaturized = true
R.cmd("shiny::runGist('95ec24c1b0cb433a76a5', launch.browser=TRUE)")

or

tell application "R"
  activate
    set miniaturized of window 1 to true
    cmd "shiny::runGist('95ec24c1b0cb433a76a5', launch.browser=TRUE)"
end tell

Users will still need to quit out of R, but you could also add a Shiny actionButton to your app that does a quit(save="no", runLast=FALSE) on submit (example code in this gist) to make it feel like a “real” application.

This all assumes & relies on the fact that the shiny package is already installed on your systems. To ensure any application-dependent packages are installed without forcing the users to manually install them, you can use something like this:

pkg <- c("shiny", "zipcode", "pbapply", "data.table", "dplyr", 
         "ggplot2", "grid", "gridExtra", "stringi", "magrittr")
new.pkg <- pkg[!(pkg %in% installed.packages())]
if (length(new.pkg)) {
  install.packages(new.pkg)
}

at the top of server.R to ensure they are available to your application (note that said hack assumes a CRAN mirror is set).

Finally, for maximum compatibility, you’ll need to use the pure AppleScript version instead of the JXA version unless all your users are on Yosemite or higher.

Example Shiny Snowfall App

If you’re on OS X and have R and the shiny package installed, you can try out the sample “Shiny Snowfall”” app by downloading and unzipping this file (you may need to right/option-click->Save As):

Shiny Snowfall.zip

and then running the “Shiny Snowfall” app. (NOTE: You need to have your Security & Privacy settings set to “Allow apps downloaded from ‘Mac App Store and identified developers” to run the application or option/right-click “open” on the app icon”)

The icon used is from Adam Whitcroft’s (@adamwhitcroft) Climacons collection.

To leave a comment for the author, please follow the link and comment on their blog: Data Driven Security.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)