library(tidyverse)

library(bea.R)
# Get the data from API

beaKey <- "6D5F9FCA-743C-4CE2-ADB0-7A2850F8508D"

beaSets(beaKey = beaKey)
## $Dataset
##                DatasetName                   DatasetDescription
## 1                     NIPA                 Standard NIPA tables
## 2       NIUnderlyingDetail Standard NI underlying detail tables
## 3                      MNE            Multinational Enterprises
## 4              FixedAssets         Standard Fixed Assets tables
## 5                      ITA  International Transactions Accounts
## 6                      IIP    International Investment Position
## 7              InputOutput                    Input-Output Data
## 8            IntlServTrade         International Services Trade
## 9            GDPbyIndustry                      GDP by Industry
## 10                Regional                   Regional data sets
## 11 UnderlyingGDPbyIndustry           Underlying GDP by Industry
## 12      APIDatasetMetaData    Metadata about other API datasets
## 
## attr(,"params")
##   ParameterName                       ParameterValue
## 1        USERID 6D5F9FCA-743C-4CE2-ADB0-7A2850F8508D
## 2        METHOD                       GETDATASETLIST
## 3  RESULTFORMAT                                 JSON
beaParams(beaKey = beaKey, "GDPbyIndustry")
## $Parameter
##   ParameterName ParameterDataType
## 1     Frequency            string
## 2      Industry            string
## 3       TableID           integer
## 4          Year           integer
##                                        ParameterDescription
## 1                                   A - Annual, Q-Quarterly
## 2              List of industries to retrieve (ALL for All)
## 3 The unique GDP by Industry table identifier (ALL for All)
## 4         List of year(s) of data to retrieve (ALL for All)
##   ParameterIsRequiredFlag ParameterDefaultValue MultipleAcceptedFlag AllValue
## 1                       1                                          1      ALL
## 2                       1                                          1      ALL
## 3                       1                                          1      ALL
## 4                       1                                          1      ALL
## 
## attr(,"params")
##   ParameterName                       ParameterValue
## 1        USERID 6D5F9FCA-743C-4CE2-ADB0-7A2850F8508D
## 2        METHOD                     GETPARAMETERLIST
## 3   DATASETNAME                        GDPBYINDUSTRY
## 4  RESULTFORMAT                                 JSON
"Method" = "GetData"

"datasetname" = "GDP"
head(beaSearch("GDP", beaKey = beaKey))
## Warning in beaSearch("GDP", beaKey = beaKey): Regional metadata is missing from
## /Library/Frameworks/R.framework/Versions/4.2/Resources/library/beaR/data and
## may be locked for updating on the BEA API; searching national metadata only.
##    SeriesCode RowNumber                         LineDescription LineNumber
## 1:     A191RO        10            Gross domestic product (GDP)          1
## 2:   PE000009       330                  Average of GDP and GDI         32
## 3:     A191RL        10            Gross domestic product (GDP)          1
## 4:   PB000009       130                  Average of GDP and GDI         12
## 5:   PA000009       200 Average of GDP and GDI, current dollars         19
## 6:     A191RC        10            Gross domestic product (GDP)          1
##    ParentLineNumber Tier Path TableID DatasetName
## 1:                     0    1  T10111        NIPA
## 2:                     0   32  T10111        NIPA
## 3:                     0    1  T10701        NIPA
## 4:                     0   12  T10701        NIPA
## 5:                     0   19  T10701        NIPA
## 6:                     0    1  T10705        NIPA
##                                                                                                                                       TableName
## 1:                                                          Table 1.1.11. Real Gross Domestic Product: Percent Change From Quarter One Year Ago
## 2:                                                          Table 1.1.11. Real Gross Domestic Product: Percent Change From Quarter One Year Ago
## 3: Table 1.7.1. Percent Change From Preceding Period in Real Gross Domestic Product, Real Gross National Product, and Real Net National Product
## 4: Table 1.7.1. Percent Change From Preceding Period in Real Gross Domestic Product, Real Gross National Product, and Real Net National Product
## 5: Table 1.7.1. Percent Change From Preceding Period in Real Gross Domestic Product, Real Gross National Product, and Real Net National Product
## 6:          Table 1.7.5. Relation of Gross Domestic Product, Gross National Product, Net National Product, National Income, and Personal Income
##            ReleaseDate     NextReleaseDate         MetaDataUpdated  Account
## 1: Feb 28 2019  8:30AM Mar 28 2019  8:30AM 2019-03-06T10:13:29.923 National
## 2: Feb 28 2019  8:30AM Mar 28 2019  8:30AM 2019-03-06T10:13:29.923 National
## 3: Feb 28 2019  8:30AM Mar 28 2019  8:30AM 2019-03-06T10:13:29.923 National
## 4: Feb 28 2019  8:30AM Mar 28 2019  8:30AM 2019-03-06T10:13:29.923 National
## 5: Feb 28 2019  8:30AM Mar 28 2019  8:30AM 2019-03-06T10:13:29.923 National
## 6: Feb 28 2019  8:30AM Mar 28 2019  8:30AM 2019-03-06T10:13:29.923 National
##                                                                                                             apiCall
## 1: beaGet(list('UserID' = '[your_key]', 'Method' = 'GetData', 'DatasetName' = 'NIPA', 'TableName' = 'T10111', ...))
## 2: beaGet(list('UserID' = '[your_key]', 'Method' = 'GetData', 'DatasetName' = 'NIPA', 'TableName' = 'T10111', ...))
## 3: beaGet(list('UserID' = '[your_key]', 'Method' = 'GetData', 'DatasetName' = 'NIPA', 'TableName' = 'T10701', ...))
## 4: beaGet(list('UserID' = '[your_key]', 'Method' = 'GetData', 'DatasetName' = 'NIPA', 'TableName' = 'T10701', ...))
## 5: beaGet(list('UserID' = '[your_key]', 'Method' = 'GetData', 'DatasetName' = 'NIPA', 'TableName' = 'T10701', ...))
## 6: beaGet(list('UserID' = '[your_key]', 'Method' = 'GetData', 'DatasetName' = 'NIPA', 'TableName' = 'T10705', ...))
beaGet(list( "UserID" = beaKey,
            "Method" = "GetData",
            "DatasetName" = "NIPA",
            "TableName" = "T10705",
            "Frequency" = "A",
            "Year" = "2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021",
            "ResultFormat" = 'json')) -> gdp
# Filter data
library("dplyr")
gdp %>% filter(SeriesCode == "A191RC") -> gdp2
gdp2 %>% select(LineDescription, DataValue_2004:DataValue_2021) -> gdp3
# Reshape data
pivot_longer(gdp3, 
             cols = starts_with("DataValue"), 
             names_to = "year",values_to = "prod", values_drop_na = FALSE)-> gdp4
gdp5 = select(gdp4,-1 )
gdp5 %>% mutate(year = 2004:2021) -> gdp6
# Plot
p <- ggplot(gdp6,aes(x=year, y=prod))+geom_line()
p + ggtitle("GDP vs Year") +
  xlab("Year") + ylab("GDP")