How to: Build a Data Exploration App in Shiny

This article is aimed at assisting you in the development of a Shiny application focused on Exploratory Data Analysis (EDA). The Shiny framework enhances data management capabilities, facilitating the creation of web applications. These applications can provide dynamic environments for interactive data exploration.

In EDA , the ability to interact with data is crucial. Shiny applications allows users to filter, sort, and visualize data in various ways, fostering a deeper and more intuitive understanding of the underlying trends and patterns.

As you progress through this article, you’ll acquire the skills to use Shiny for various EDA tasks. This includes uploading and processing data, creating visual representations, and building interactive features that enhance data exploration. By the end, you’ll be equipped with the skills to develop Shiny applications that enable comprehensive and insightful data analyses.

Data Management in Shiny

Handling Multiple Data Formats

Efficient management of diverse data formats is important in EDA . This section will take you through setting up a Shiny app capable of processing file uploads and API data, thereby building your data analysis toolkit.

User Interface

It’s important to design your Shiny app’s user interface (UI) to accommodate different data inputs. This part focuses on configuring the UI for CSV file uploads and API data retrieval.

ui  fluidPage( titlePanel("Data Exploration App"), sidebarLayout( sidebarPanel( fileInput("fileCSV", "Upload CSV File"), # For CSV file uploads actionButton("loadData", "Load Data from World Bank API") # Button to trigger API data loading ), mainPanel( tableOutput("dataDisplay") # Area to display the data ) ) ) 

Server

The server function is where the CSV file uploads and fetched data from an API are retrieved, such as the World Bank API. This example illustrates basic yet useful data processing techniques in Shiny .

server  function(input, output)  # Reactive value to store data dataStorage  reactiveVal() # Observe CSV File Uploads observeEvent(input$fileCSV,  # Check if a file is uploaded req(input$fileCSV) # Read and store CSV data csvData  read.csv(input$fileCSV$datapath, stringsAsFactors = FALSE) dataStorage(csvData) >) # Observe API Data Loading observeEvent(input$loadData,  # Fetch and store data from an API (example API call) apiData  WDI::WDI(country = "all", indicator = "NY.GDP.MKTP.CD", start = 2019, end = 2019) dataStorage(apiData) >) # Render the data in the UI output$dataDisplay  renderTable( dataStorage() # Access the stored data >) > 

Dynamic Data Response and Management

Your Shiny app incorporates the following elements:

Incorporating Additional File Types

To extend the capabilities of the Shiny application to handle other file types, such as Excel or JSON, you can incorporate additional logic into the server function. Here are some tips for doing so:

# Specify in UI fileInput("fileExcel", "Upload Excel File"), fileInput("fileJSON", "Upload JSON File") # Specify in Server observeEvent(input$fileExcel,  req(input$fileExcel) excelData  readxl::read_excel(input$fileExcel$datapath) dataStorage(excelData) >) observeEvent(input$fileJSON,  req(input$fileJSON) jsonData  jsonlite::fromJSON(file = input$fileJSON$datapath) dataStorage(jsonData) >) 

Use Your Own Data

While we use the iris dataset for demonstration in the rest of this article, you now have the skills to adapt this code to work with your own data sources and formats.

Interactive Tables in Shiny with DT

This section will guide you in transforming static tables, like in the previous example, into dynamic elements within your Shiny app. Interactive tables allow users to sort, filter, and paginate data, which are useful features for managing extensive datasets and complex information. To turn static tables into interactive tables we will use the DT package, a powerful tool within Shiny .

User Interface

The first step in adding interactivity is to modify the UI to accommodate interactive tables. This involves replacing the traditional tableOutput with DT::dataTableOutput in the UI definition:

# UI Setup for interactive tables ui  fluidPage( titlePanel("Data Exploration App"), sidebarLayout( sidebarPanel( # Your existing input controls ), mainPanel( DT::dataTableOutput("dataDisplay") # Updated to use DT's dataTableOutput ) ) ) 

Server

On the server side, we adapt our logic to render the data as an interactive DataTable . This integration enhances the data presentation, making it more adaptable to user interactions and providing a richer experience in data exploration.

# Server Logic for interactive tables server  function(input, output)  # Existing data processing logic # Rendering the data as an interactive DataTable output$dataDisplay  DT::renderDataTable( datatable(dataset(), options = list( pageLength = 5, # Set rows per page autoWidth = TRUE, # Auto-adjust column widths searching = TRUE, # Enable data filtering ordering = TRUE, # Enable column sorting colReorder = TRUE)) # Allow column reorderingpagelength = 5, >) > 

These modifications transform your Shiny app’s tables into interactive platforms. Users can now engage more deeply with the data by sorting, filtering, and paging, improving data exploration efficiency and intuitiveness.

Summary Statistics Display

Summary statistics are the backbone in EDA , offering a snapshot of data characteristics like the distribution or correlation. Integrating these statistics into a Shiny application using DT enriches the data explanatory power, providing clear and concise data insights. Here’s how to embed these summary statistics into a shiny app.

User Interface

The user interface (UI) can be designed to facilitate an intuitive and interactive experience with summary statistics. Here are some examples to consider: