R Shiny Tutorials For Beginners: How To Build A Simple App

R Shiny tutorials for beginners

R isn’t just about writing and running code in your console. With our R Shiny tutorials for beginners, you can start building your interface web app right away using the R language.

What Is Shiny?

Shiny is a package you can use to create interactive web applications directly in R and then publish them so others can access them from web browsers. Thanks to Shiny, R users can start designing and making polished web apps with this language’s syntax and functionalities.

What You Can Do With Shiny

You can use it to make pretty dashboards that can track important performance indicators and drill down into particular metrics for low-level investigation at the same time. Your users can quickly pick the report and piece of information they want right away. This is obviously more intuitive than having to go through hundreds and hundreds of PDF pages.

Shiny can come in handy when you need to communicate with non-technical audiences. They can present complex models with interactive analysis and informative visualizations. Sophisticated analyses can get done within Shiny even when the user has no R or programming skills.

On top of that, Shiny apps can replace some tools in the workflow of a statistician or data scientist too. You can build a user-friendly self-service interface where people can request and download data for their analysis.

Interactive and educational demos are another common use of Shiny. Learners can tweak their inputs, make analyses, and observe the outcome when learning data science and statistics concepts.

What You Should Learn Shiny As A R Programmer

Normally, you will have to get a deep understanding of essential web technologies like HTML, CSS, and JavaScript to make an interactive web app. It can be hard for R users.

They may only need something to display their data and graphics and let readers play with them. It may take them a long time to learn other languages and tools just to make apps with just some common tasks.

Shiny makes this process much less painful for R programmers. It comes with a set of UI (user interface) functions. You won’t need to know the actual details under the hood, how these components are built, and so on. Everything is ready for use in your app right away.

This package also makes it easier for you to update outputs based on input changes. This kind of reactive programming is a common task in data-heavy web apps, but doing it from scratch in HTML, CSS, and JavaScript would be a huge challenge.

Shiny comes with the necessary tools for you to build an app like that without having to deal with the mundane and complicated underlying code.

R Shiny Tutorials For Beginners

Let’s take the R Shiny tutorials for beginners.

Install Shiny

You should have R already installed in your system. An IDE (integrated development environment) like RStudio is also recommended. It can help you build and test Shiny apps with nice features for debugging, authoring, and deploying them.

Use this R command to install Shiny:

install.packages("shiny")

If you aren’t sure whether your system already has it, check the version of the shiny package:

packageVersion("shiny")

When everything is ready, load the shiny package into your R session:

library(shiny)

Create A Shiny App

Open RStudio, select File > New File > Shiny Web App….

Give your application a name by entering it into the Application name field, set your application’s type to Single File, and choose a place to save your app in your filesystem. When done, select Create.

RStudio will automatically generate a template app.R file in the root of your application and open it for you.

You can delete this template to write your app from scratch. But for now, we will leave it there to learn about the layout of a basic Shiny app.

Structure Of A Shiny App

At the top of the app.R file, you can see one or several library() function calls:

library(shiny)

This is where you can declare the R packages your app needs to run. The shiny package is, of course, a must-have, and you can also add others, like ggplot2, depending on which functions you use in your code.

Note: whenever you use extra capabilities from a package, remember to declare it in the app.R file to prevent errors.

Right below that section, you can see the user interface object (ui):

ui <- fluidPage(
    titlePanel("Old Faithful Geyser Data"),
    sidebarLayout(
        mainPanel(
           plotOutput("distPlot")
        )
    )
)

This object allows you to describe the layout of your app when it appears in web browsers. It controls how the interface and appearance look.

The fluidPage() function here creates, as its name suggests, a fluid page layout. Each fluid page layout includes several rows, each of which consists of rows. The row-first design makes sure elements in the same row are displayed in the same line in browsers. Shiny apps scale their fluid page layouts to fit the available width of the browser.

Below this ui object is the server() function:

server <- function(input, output) {}

R programmers use to define the logic code for their Shiny apps. The server() function is where you can take inputs, make computations, draw maps, tables, and plots, as well as make other changes to the front-end interface.

At the end of the app.R file is the shineApp() function:

shinyApp(ui = ui, server = server)

It creates and runs a Shiny app using the ui object and the server() function above. Before version 0.10.2, you will need to put them in two separate files called server.R and ui.R. While Shiny still supports this file structure, combining them into a single-file app with the app.R file is a better idea in most cases.

Write And Run A Simple Shiny App

Replace the content of the default app.R file with this code:

library(shiny)

ui <- fluidPage(
  "Welcome to LearnShareIT!"
)

server <- function(input, output) {}

shinyApp(ui, server)

This app only needs the shiny package to do its job. Its layout is also incredibly simple, with just one element containing the text “Welcome to LearnShareIT!”.

There is no R code in the server() function. This means this Shiny app doesn’t do anything other than display the HTML page defined in the ui object.

Save the file and click Run App in the toolbar.

RStudio will read the app.R file and launch it with a simple web server. Its built-in browser will appear to run your Shiny app. If you can want to open the app on a web browser of your choice, look for the address in the RStudio console, such as:

> runApp(LearnShareIt’)

Listening on http://127.0.0.1:3675

The http://127.0.0.1:3675 address means you are running the app on localhost (your current computer) and port 3627.

There will also be a busy button on the R console, indicating that there is something running in the console, and you can’t enter new commands for the time being. To stop the Shiny app, click this button or simply close the window of your app.

In addition to RStudio’s editor, you can also run RShiny apps in any R console with the shiny::runApp() function. You just need to provide the root directory of your app:

shiny::runApp(‘/home/admin/LearnShareIT’)

This can come in handy when you don’t want to block the current R process in RStudio or elsewhere. Just open a new R console, and you will have a separate place to run and test your app.

Reloading Shiny Apps

You don’t need to stop and restart a Shiny app after modifying its server script or user interface. Just save your changes and reload your web browser. R will load the current version of the app.R file when you make a new request to access the app.

Add Some UI Controls And Change Behaviors

You can start adding elements to the UI layout and add logic code behind them together, giving your app some functionalities.

For example, you can add a select list input control to the UI definition with selectInput(). This input control allows users to provide a value to the app.

Example

selectInput(intputID = “language”, label = “Language”, choices = c(“Python”, “C++”, “Java”))

This creates an input control in the app where users can select from three provided options. The inputID argument is the ID you can use to access the value of this input control, while label specifies which label will be displayed above it. Use the choices argument to set the set of values users can select from.

You can add a text to confirm your selection:

textOutput("message")

Your ui object now should look like this:

ui <- fluidPage(
  selectInput("language", label = "Language", choices = c("Python", "C++", "Java")),
  textOutput("message")
)

Modify the server() function so it can observe your selection and change the message on the web page:

server <- function(input, output) {
  output$message <- renderText({ 
    paste("You have selected", input$language)
  })
}

Save your file and reload the app. It will now show a different message depending on the language you have chosen in the input control.

Summary

Our R Shiny tutorials for beginners should give you an overview of this package and how its apps work. You can add more UI controls and other functions to make more complex interactive web applications, all from your R environment.

Maybe you are interested:

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *