Building Interactive CPI Analysis Tool with Shiny

Idrissa Tankari

Setting Up the Environment

Link to Download the dataset:
https://www.dropbox.com/scl/fi/m9rur5jlewnxrv2282xby/cpi-1.csv?rlkey=bei5ka2x2k98tcfq6ic4teu7x&dl=0

To begin our journey into creating an interactive Shiny application, we first ensure we have all the necessary tools at our disposal. This includes loading essential libraries such as shiny, tidyverse, readr, and lubridate. The shiny package forms the backbone of our application, providing the framework for creating dynamic web interfaces. The tidyverse package offers a suite of data manipulation and visualization tools, enhancing our ability to work with data efficiently. It also assists in reading CSV files while lubridate simplifying the handling of date and time objects.

library(shiny)
library(tidyverse)
library(readr)
library(lubridate)

# Load CPI dataset
cpi <- read.csv("cpi.csv")
cpi$period <- mdy(cpi$period)

Building User Interface

Designing Interactive Components

In this step, we design our Shiny application's user interface (UI) using the fluidPage() function. Within the tabsetPanel(), we create two distinct tabs to organize the application's functionalities effectively. The first tab, titled "Evolution of CPI," allows users to explore the historical evolution of the Consumer Price Index (CPI) through interactive select inputs for specifying start and end dates. The second tab, named "CPI Inflation Calculator," empowers users to calculate the inflation-adjusted value of a specified amount over time by selecting base and current periods and entering a dollar amount.

ui <- fluidPage(

tabsetPanel(
tabPanel("Evolution of CPI",
selectInput(inputId = "month1", label="Please select the start month", choices=cpi$month, selected=1),
selectInput(inputId = "year1", label="Please select the start year", choices=cpi$year, selected=2000),
selectInput(inputId = "month2", label="Please select the end month", choices=cpi$month, selected=1),
selectInput(inputId = "year2", label="Please select the current year", choices=cpi$year),
plotOutput(outputId = "plot")
),

tabPanel("CPI Inflation Calculator",
numericInput(inputId = "dollar", label="Please enter an amount", value=0,min=0),
selectInput(inputId = "month1b", label="Please select the base month", choices=cpi$month, selected = 1),
selectInput(inputId = "year1b", label="Please select the base year", choices=cpi$year, selected=2000),
selectInput(inputId = "month2b", label="Please select the current month", choices=cpi$month, selected = 1),
selectInput(inputId = "year2b", label="Please select the current year", choices=cpi$year),
actionButton(inputId="update", label="Calculate"),
textOutput(outputId = "text")
)
)
)

This structured and intuitive UI design ensures that users can seamlessly interact with the application and easily access its functionalities.

Defining Server Logic

In the server function, we define the logic for generating outputs based on user inputs. We render a dynamic plot using the function for the "Evolution of CPI" tab. This plot showcases the historical trend of the CPI based on the selected start and end dates. On the other hand, the "CPI Inflation Calculator" tab computes the inflation-adjusted value of a user-entered dollar amount using the renderText() function. This calculation is performed based on the selected base and current periods, providing users with valuable insights into the purchasing power of their money over time.

server <- function(input, output, session) {

output$plot <- renderPlot(
{
cpi.2 <- subset(cpi, (year>=input$year1 & month>=input$month1) & (year<=input$year2 & month<=input$month2))

ggplot(data=cpi.2, aes(x=period, y=cpi)) +
geom_line(color="darkblue", linewidth=0.8) +
labs(x="Period", y="CPI") +
theme_classic()
}
)

output$text <- renderText(
{
dollar <- eventReactive(input$update, input$dollar)

month1 <- eventReactive(input$update, input$month1b)
year1 <- eventReactive(input$update, input$year1b)

cpi.base <- subset(cpi, year==year1() & month==month1())
index1 <- cpi.base$cpi

month2 <- eventReactive(input$update, input$month2b)
year2 <- eventReactive(input$update, input$year2b)

cpi.current <- subset(cpi, year==year2() & month==month2())
index2 <- cpi.current$cpi

new.dollar <- round(dollar()*(index2/index1), 2)

month.name.1 <- month.name[as.numeric(month1())]
month.name.2 <- month.name[as.numeric(month2())]

paste("$", dollar(), "in", month.name.1, year1(), "has the same buying power as",
"$", new.dollar, "in", month.name.2, year2(), "!")
}
)
}

Through meticulous handling of user inputs and thoughtful generation of outputs, we ensure that our Shiny application delivers a seamless and interactive user experience. This enables users to explore CPI trends and perform inflation calculations confidently and easily, empowering them with valuable insights into economic dynamics over time.

Conclusion: Empowering Data Exploration with Shiny

In this comprehensive guide, we've embarked on a journey to create an interactive Shiny application for exploring Consumer Price Index (CPI) data. By meticulously explaining each step of the code, we've provided readers with a thorough understanding of how to build a dynamic and user-friendly application for analyzing economic trends.

From setting up the environment by loading essential libraries and datasets to designing an intuitive user interface with interactive components, we've laid the foundation for a seamless user experience. Users can effortlessly navigate between tabs to visualize the historical evolution of the CPI and calculate inflation-adjusted values of monetary amounts over time.

Through the server logic, we've demonstrated how to handle user inputs and generate dynamic outputs, ensuring that the application responds in real-time to user interactions. Whether users explore CPI trends or perform inflation calculations, they can trust that the application delivers accurate and insightful results.

In conclusion, this guide is a valuable resource for anyone looking to harness the power of Shiny for data exploration and analysis. By following the step-by-step instructions and understanding the underlying logic, users can create their own interactive applications to unlock the hidden insights within their data. With Shiny, the possibilities for data exploration are endless, empowering users to make informed decisions and drive meaningful change.

No responses yet

Write a response