# Clear the workspace
  rm(list = ls()) # Clear environment
  gc()            # Clear unused memory
##          used (Mb) gc trigger (Mb) max used (Mb)
## Ncells 521472 27.9    1159727   62   660385 35.3
## Vcells 947744  7.3    8388608   64  1769625 13.6
  cat("\f")       # Clear the console

1) Install & Setup

# Install package
# install.package("bea.R") I already installed this package

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(bea.R)
## Warning: package 'bea.R' was built under R version 4.3.2
## Loading required package: data.table
## 
## Attaching package: 'data.table'
## 
## The following objects are masked from 'package:lubridate':
## 
##     hour, isoweek, mday, minute, month, quarter, second, wday, week,
##     yday, year
## 
## The following objects are masked from 'package:dplyr':
## 
##     between, first, last
## 
## The following object is masked from 'package:purrr':
## 
##     transpose
## 
## Note: As of February 2018, beaGet() requires 'TableName' for NIPA and NIUnderlyingDetail data instead of 'TableID.' See https://github.us-bea/bea.R for details.
# Setup API key
beaKey = "F61A8C05-17AA-465F-9119-2039DBD96157"

2)

beaSearch('Gross domestic product (GDP)', beaKey, asHtml = TRUE)
## Creating first-time local copy of metadata for all datasets - only done once.
## Datasets will be updated only if timestamps indicate metadata obsolete in future searches,
## and only obsolete metadata sets will be updated (it's faster this way).
## 
## No encoding supplied: defaulting to UTF-8.
## Warning in beaSearch("Gross domestic product (GDP)", beaKey, asHtml = TRUE):
## Regional metadata is missing from
## C:/Users/LENOVO/AppData/Local/R/win-library/4.3/beaR/data and may be locked for
## updating on the BEA API; searching national metadata only.
beaSpecs = list(UserID = beaKey, Method = "GetData", DatasetName = "NIPA", TableName = "T10705",
            Frequency = "A", Year = "X",  ResultFormat = "json")
df = beaGet(beaSpecs, asWide = FALSE);
## No encoding supplied: defaulting to UTF-8.
head(df, 3)
##    TableName SeriesCode LineNumber              LineDescription TimePeriod
## 1:    T10705     A191RC          1 Gross domestic product (GDP)       1929
## 2:    T10705     A191RC          1 Gross domestic product (GDP)       1930
## 3:    T10705     A191RC          1 Gross domestic product (GDP)       1931
##        METRIC_NAME CL_UNIT UNIT_MULT DataValue NoteRef
## 1: Current Dollars   Level         6    104556  T10705
## 2: Current Dollars   Level         6     92160  T10705
## 3: Current Dollars   Level         6     77391  T10705
4)
# Filter data
library("dplyr")
gdp = df %>% filter(SeriesCode == "A191RC")
3)
#Plot the graph

plot(gdp$TimePeriod,
     gdp$DataValue,
     col = "red",
     xlab = "Year", 
     ylab = "GDP", 
     main = "GDP&Time")

5)
   We can see the financial crisis and covid both are slow down in the GDP level. In 2008-2009 financial crisis slow down because of the unployment, economic level down and business also not well so the GDP level was slow down at that time.
   In 2019-2020, COVID-19 affected whole of the country all the industries and manufacturing were shut down.So the GDP is lower.
6) ggplot2
# Convert 'TimePeriod' column from characters to numeric
gdp$TimePeriod <- as.numeric(gdp$TimePeriod)
ggplot(gdp, aes(x = TimePeriod, y = DataValue)) +
  geom_line(color = "red") +
  labs(title = "GDP&Time", x = "Time Period", y = "GDP") +
  theme_minimal()