A brief intro to writing web applications in R with Shiny

How to write web apps in R with Shiny

How to write web apps in R with Shiny
Image by : 

opensource.com

x

Get the newsletter

Join the 85,000 open source advocates who receive our giveaway alerts and article roundups.

Happy new year! I'm in the thick of working on a couple of larger articles this month, so look for those in the coming weeks. For this month's Nooks and Crannies, I want to point out briefly a great R library I've been playing with for my own self-education.

A close friend of mine has been hacking things in R lately. I've been intrigued, and I've been trying to squeeze in a little time to at least learn more about R and the kinds of things you can do with it. Figuring out the number-crunching capabilities is an ongoing struggle for me, as I'm not nearly the brilliant math-oriented mind my friend is. It's kind of a slow go for me, but I've been trying to relate it to my experience in other areas, and I started thinking about even very simple web applications.

Shiny is a toolkit from RStudio that makes creating web applications much easier. Installation is easy from an R console, just one line, and the latest stable version will be loaded up for you to use. There's a great tutorial that walks you through the concepts of setting up application, building one skill on top of prior lessons. Shiny is licensed GPLv3, and the source is available on GitHub.

Here's a simple little web application written with Shiny:


library(shiny)

server <- function(input, output, session) {
    observe({
            myText <- paste("Value above is: ", input$textIn)
                updateTextInput(session, "textOut", value=myText)
    })
}

ui <- basicPage(
    h3("My very own sample application!"),
        textInput("textIn", "Input goes here, please."),
        textInput("textOut", "Results will be printed in this box")
)

shinyApp(ui = ui, server = server)

When you type in the input box, the text is copied after the prefix in the output box. This is nothing fancy, but it shows you the fundamental structure of a Shiny application. The "server" section lets you handle all your back-end work, such as calculations, database retrieval, or whatever else the app needs to happen. The "UI" section defines the interface, which can be as simple or as complicated as needed.

Included in Shiny are extensive capabilities for styling and display themes using Bootstrap, so you can, after learning a bit, create extensive, feature-rich applications for the web in R. Add-on packages can extend the capabilities to even more advanced JavaScript applications, templating, and more.

You can handle the back-end work of Shiny in several ways. If you're just running your application locally, having the library loaded will do the trick. For applications you want to serve out to the web, you can share them on RStudio's Shiny website, run an open source version of the Shiny server, or buy Shiny Server Pro from RStudio via a yearly subscription service.

Experienced R gurus may already know all about Shiny; it's been around a couple of years now. For people like me who come from a completely different sort of programming and want to learn a bit about R, I've found it quite helpful.

About the author

Ruth Holloway - Ruth Holloway has been a system administrator and software developer for a long, long time, getting her professional start on a VAX 11/780, way back when. She spent a lot of her career (so far) serving the technology needs of libraries, and has been a contributor since 2008 to the Koha open source library automation suite.cvRuth is currently a Perl Developer at cPanel in Houston. You can find out more about Ruth's passions and career at her site....