# install.packages("wbstats")
library(wbstats)

World Bank API

Get the metadata

meta <- wb_cache()
names(meta)
## [1] "countries"     "indicators"    "sources"       "topics"       
## [5] "regions"       "income_levels" "lending_types" "languages"

Write to a csv

write.csv(meta$countries, "countries.csv")
write.csv(meta$topics, "topics.csv")

Search indicators using patterns

wb_search("population")
## # A tibble: 3,967 Ă— 3
##    indicator_id         indicator                               indicator_desc  
##    <chr>                <chr>                                   <chr>           
##  1 1.0.HCount.1.90usd   Poverty Headcount ($1.90 a day)         The poverty hea…
##  2 1.0.HCount.2.5usd    Poverty Headcount ($2.50 a day)         The poverty hea…
##  3 1.0.HCount.Mid10to50 Middle Class ($10-50 a day) Headcount   The poverty hea…
##  4 1.0.HCount.Ofcl      Official Moderate Poverty Rate-National The poverty hea…
##  5 1.0.HCount.Poor4uds  Poverty Headcount ($4 a day)            The poverty hea…
##  6 1.0.HCount.Vul4to10  Vulnerable ($4-10 a day) Headcount      The poverty hea…
##  7 1.0.PGap.1.90usd     Poverty Gap ($1.90 a day)               The poverty gap…
##  8 1.0.PGap.2.5usd      Poverty Gap ($2.50 a day)               The poverty gap…
##  9 1.0.PGap.Poor4uds    Poverty Gap ($4 a day)                  The poverty gap…
## 10 1.1.HCount.1.90usd   Poverty Headcount ($1.90 a day)-Rural   The poverty hea…
## # ℹ 3,957 more rows

Download data using indicator id

pop <- wb_data("SP.POP.TOTL")

write.csv(pop, "population.csv")

Quantod API

# install.packages("quantmod")
library(quantmod)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo

Get stock price data

getSymbols(c("MSFT", "BAC", "HOOD", "TSLA"), src = "yahoo")
## [1] "MSFT" "BAC"  "HOOD" "TSLA"

Plot the data

plot(MSFT$MSFT.Close)

plot(HOOD$HOOD.Close)

plot(BAC$BAC.Close)

plot(TSLA$TSLA.Close)

Save the stock data as csv

write.zoo(TSLA, "TSLA.csv", sep=",")

#Cryptocurrency API

# install.packages("jsonlite")
library(jsonlite)
btc_200 <- fromJSON("https://min-api.cryptocompare.com/data/v2/histoday?fsym=BTC&tsym=USD&limit=200")

# Find the dataframe within the obtained list
df_btc <- btc_200$Data$Data
tail(df_btc)
##           time     high      low     open volumefrom   volumeto    close
## 196 1758153600 117981.1 116134.8 116488.2   19195.15 2253362090 117126.5
## 197 1758240000 117511.0 115150.9 117126.5   16753.48 1947524484 115702.0
## 198 1758326400 116202.3 115488.4 115702.0    5995.45  694697640 115756.1
## 199 1758412800 115901.0 115262.9 115756.1    5422.99  626782372 115299.8
## 200 1758499200 115439.0 112027.4 115299.8   28253.02 3197786130 112747.9
## 201 1758585600 113372.6 111579.3 112747.9   11457.03 1291814729 112589.4
##     conversionType conversionSymbol
## 196         direct                 
## 197         direct                 
## 198         direct                 
## 199         direct                 
## 200         direct                 
## 201         direct
# install.packages("devtools")

Wikipedia hits

Obtain the wikipedia hits data

library(devtools)
## Loading required package: usethis
# devtools::install_github("ironholds/pageviews")
# install.packages("pageviews")
library(pageviews)
wiki_hits <- article_pageviews(project = "en.wikipedia",
                               article = "AI",
                               start = as.Date("2025-09-01"),
                               end = as.Date("2025-09-19"),
                               user_type = "user")

Plot the wikipedia hits over time using ggplot2 package

# install.packages("ggplot2")
library(ggplot2)

ggplot(wiki_hits, aes(x = date, y = views)) +
  geom_line(color = "blue", size =1) +
  labs(title = "Wikipedia Pageviews: AI (Sep 25)",
       x = "Date", y = "Views") +
  theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.