install.packages(
  c(
    "tidyverse", "tidytext", "textdata", "jsonlite",
    "wordcloud", "RColorBrewer", "lubridate",
    "scales", "knitr", "kableExtra"
  ),
  repos = "https://cloud.r-project.org"
)
library(tidyverse)
library(tidytext)
library(textdata)
library(jsonlite)
library(wordcloud)
library(RColorBrewer)
library(lubridate)
library(scales)
library(knitr)
library(kableExtra)
api_key <- "2ef854825593400e89bdaf15edcd44f2"
fetch_news <- function(query, api_key, page_size = 20) {
  url <- paste0(
    "https://newsapi.org/v2/everything?",
    "q=", URLencode(query, reserved = TRUE),
    "&language=en",
    "&sortBy=publishedAt",
    "&pageSize=", page_size,
    "&apiKey=", api_key
  )

  response <- fromJSON(url, flatten = TRUE)

  if (!identical(response$status, "ok")) {
    stop("NewsAPI request failed for ", query, ": ", response$message)
  }

  as_tibble(response$articles) %>%
    rename_with(~ str_replace_all(.x, "\\.", "_")) %>%
    mutate(query = query)
}

news_raw <- bind_rows(
  fetch_news("SpaceX", api_key),
  fetch_news("Anthropic", api_key),
  fetch_news("Klarna", api_key),
  fetch_news("CoreWeave", api_key)
)

glimpse(news_raw)
## Rows: 80
## Columns: 10
## $ author      <chr> "bbc.com", NA, NA, NA, "DIGITIMES", "benzinga.com", "Edito…
## $ title       <chr> "Musk's SpaceX overtakes Amazon to become world's fifth mo…
## $ description <chr> "Musk's SpaceX overtakes Amazon to become world's fifth mo…
## $ url         <chr> "https://biztoc.com/x/b80e7ffcc06a6a35", "https://www.amer…
## $ urlToImage  <chr> "https://biztoc.com/cdn/b80e7ffcc06a6a35_s.webp", "https:/…
## $ publishedAt <chr> "2026-06-17T04:07:51Z", "2026-06-17T04:00:00Z", "2026-06-1…
## $ content     <chr> "Musk's SpaceX overtakes Amazon to become world's fifth mo…
## $ source_id   <chr> NA, NA, NA, NA, NA, NA, NA, "the-times-of-india", NA, NA, …
## $ source_name <chr> "Biztoc.com", "Americanthinker.com", "Americanthinker.com"…
## $ query       <chr> "SpaceX", "SpaceX", "SpaceX", "SpaceX", "SpaceX", "SpaceX"…
news_clean <- news_raw %>%
  filter(!is.na(.data$title)) %>%
  mutate(
    pub_date = ymd_hms(.data$publishedAt, quiet = TRUE),
    pub_day = as.Date(pub_date),
    title_clean = str_remove(.data$title, "\\s*-\\s*[^-]+$"),
    title_clean = str_squish(str_replace_all(title_clean, "[^[:alnum:][:space:]]", " ")),
    title_clean = str_to_lower(title_clean)
  ) %>%
  distinct(title_clean, .keep_all = TRUE)

cat("Total unique headlines:", nrow(news_clean), "\n")
## Total unique headlines: 76
news_clean %>%
  select(query, title_clean, any_of(c("source_name", "source", "sourceName")), pub_day) %>%
  head(10) %>%
  kable(caption = "Sample Cleaned Headlines") %>%
  kable_styling(bootstrap_options = c("striped", "hover"), full_width = FALSE)
Sample Cleaned Headlines
query title_clean source_name pub_day
SpaceX musk s spacex overtakes amazon to become world s fifth most valuable firm Biztoc.com 2026-06-17
SpaceX musk must die Americanthinker.com 2026-06-17
SpaceX albanian exuberance Americanthinker.com 2026-06-17
SpaceX everyone should celebrate the joys of capitalism and the ipo on spacex Americanthinker.com 2026-06-17
SpaceX spacex to acquire cursor in us 60 billion stock deal after blockbuster ipo Digitimes 2026-06-17
SpaceX elon musk is weighing the sun s power for ai while crypto bettors weigh how many spacex starships will succeed in 2026 here are the odds Biztoc.com 2026-06-17
SpaceX dow hits record close as nasdaq and s p 500 slip on tech rotation Crypto Briefing 2026-06-17
SpaceX 2 75 trillion mcap spacex rockets past amazon with 66 stock surge in 3 days The Times of India 2026-06-17
SpaceX hyperliquid open interest surges 32 in a week is 80 hype next Cointelegraph 2026-06-17
SpaceX in boost to musk trump admin seeks to dismiss air pollution lawsuit against xai data centre BusinessLine 2026-06-17
news_tokens <- news_clean %>%
  select(query, title_clean) %>%
  unnest_tokens(word, title_clean) %>%
  anti_join(stop_words, by = "word") %>%
  filter(!str_detect(word, "^\\d+$"), nchar(word) > 2)

top_words <- news_tokens %>%
  count(word, sort = TRUE) %>%
  slice_head(n = 20)

top_words %>%
  kable(caption = "Top 20 Words Across All Headlines") %>%
  kable_styling(bootstrap_options = "striped", full_width = FALSE)
Top 20 Words Across All Headlines
word n
spacex 11
billion 7
musk 6
stock 6
app 5
cash 5
coreweave 5
ipo 5
trump 5
xbox 5
anthropic 4
close 4
buy 3
cursor 3
data 3
deal 3
fans 3
iran 3
klarna 3
nvidia 3
top_words %>%
  mutate(word = fct_reorder(word, n)) %>%
  ggplot(aes(x = n, y = word, fill = n)) +
  geom_col(show.legend = FALSE) +
  scale_fill_gradient(low = "#a8d8ea", high = "#0077b6") +
  labs(
    title = "Top 20 Words in News Headlines",
    subtitle = "SpaceX, Anthropic, Klarna, CoreWeave",
    x = "Count",
    y = NULL,
    caption = "Source: NewsAPI | Jimmy Zhenning Xu, Ph.D. | github.com/utjimmyx"
  ) +
  theme_minimal(base_size = 13)

word_freq <- news_tokens %>%
  count(word, sort = TRUE) %>%
  filter(n >= 2)

set.seed(42)
wordcloud(
  words = word_freq$word,
  freq = word_freq$n,
  min.freq = 1,
  max.words = 80,
  random.order = FALSE,
  colors = brewer.pal(8, "Dark2"),
  scale = c(3.5, 0.5)
)
title("News Headline Word Cloud - Trending Tickers")

afinn <- get_sentiments("afinn")

sentiment_afinn <- news_tokens %>%
  inner_join(afinn, by = "word") %>%
  group_by(query) %>%
  summarise(
    total_words = n(),
    mean_sentiment = round(mean(value), 3),
    sum_sentiment = sum(value),
    .groups = "drop"
  ) %>%
  arrange(desc(mean_sentiment))

sentiment_afinn %>%
  kable(caption = "AFINN Sentiment Score by Topic") %>%
  kable_styling(bootstrap_options = c("striped", "hover"), full_width = FALSE)
AFINN Sentiment Score by Topic
query total_words mean_sentiment sum_sentiment
CoreWeave 5 2.000 10
Anthropic 6 -0.167 -1
SpaceX 11 -0.455 -5
Klarna 12 -1.167 -14
sentiment_afinn %>%
  mutate(
    query = fct_reorder(query, mean_sentiment),
    sentiment_dir = ifelse(mean_sentiment >= 0, "Positive", "Negative")
  ) %>%
  ggplot(aes(x = mean_sentiment, y = query, fill = sentiment_dir)) +
  geom_col(width = 0.6) +
  scale_fill_manual(values = c("Positive" = "#2ecc71", "Negative" = "#e74c3c")) +
  geom_vline(xintercept = 0, linetype = "dashed", color = "gray40") +
  labs(
    title = "Mean AFINN Sentiment Score by Topic",
    x = "Mean Sentiment Score",
    y = NULL,
    fill = NULL,
    caption = "Source: NewsAPI | Jimmy Zhenning Xu, Ph.D. | github.com/utjimmyx"
  ) +
  theme_minimal(base_size = 13) +
  theme(legend.position = "top")

bing <- get_sentiments("bing")

sentiment_bing <- news_tokens %>%
  inner_join(bing, by = "word") %>%
  count(query, sentiment) %>%
  pivot_wider(
    names_from = sentiment,
    values_from = n,
    values_fill = list(n = 0)
  ) %>%
  mutate(
    positive = coalesce(positive, 0L),
    negative = coalesce(negative, 0L),
    net_sentiment = positive - negative
  )

sentiment_bing %>%
  kable(caption = "Bing Sentiment Count by Topic") %>%
  kable_styling(bootstrap_options = "striped", full_width = FALSE)
Bing Sentiment Count by Topic
query negative positive net_sentiment
Anthropic 5 2 -3
CoreWeave 3 9 6
Klarna 10 4 -6
SpaceX 5 11 6
news_tokens %>%
  inner_join(bing, by = "word") %>%
  count(word, sentiment, sort = TRUE) %>%
  group_by(sentiment) %>%
  slice_head(n = 10) %>%
  ungroup() %>%
  mutate(word = reorder_within(word, n, sentiment)) %>%
  ggplot(aes(x = n, y = word, fill = sentiment)) +
  geom_col(show.legend = FALSE) +
  facet_wrap(~ sentiment, scales = "free_y") +
  scale_y_reordered() +
  scale_fill_manual(values = c("positive" = "#2ecc71", "negative" = "#e74c3c")) +
  labs(
    title = "Top Positive & Negative Words in Headlines",
    x = "Count",
    y = NULL,
    caption = "Source: NewsAPI | Jimmy Zhenning Xu, Ph.D. | github.com/utjimmyx"
  ) +
  theme_minimal(base_size = 12)

nrc <- get_sentiments("nrc")

emotion_nrc <- news_tokens %>%
  inner_join(nrc, by = "word") %>%
  filter(!sentiment %in% c("positive", "negative")) %>%
  count(query, sentiment) %>%
  group_by(query) %>%
  mutate(prop = n / sum(n))

ggplot(emotion_nrc, aes(x = sentiment, y = prop, fill = query)) +
  geom_col(position = "dodge") +
  scale_y_continuous(labels = percent_format()) +
  scale_fill_brewer(palette = "Set2") +
  labs(
    title = "NRC Emotion Proportions by Topic",
    x = "Emotion",
    y = "Proportion of Emotional Words",
    fill = "Topic",
    caption = "Source: NewsAPI | Jimmy Zhenning Xu, Ph.D. | github.com/utjimmyx"
  ) +
  theme_minimal(base_size = 12) +
  theme(
    axis.text.x = element_text(angle = 30, hjust = 1),
    legend.position = "top"
  )

tfidf_words <- news_tokens %>%
  count(query, word) %>%
  bind_tf_idf(word, query, n) %>%
  group_by(query) %>%
  slice_max(tf_idf, n = 6) %>%
  ungroup()

tfidf_words %>%
  mutate(word = reorder_within(word, tf_idf, query)) %>%
  ggplot(aes(x = tf_idf, y = word, fill = query)) +
  geom_col(show.legend = FALSE) +
  facet_wrap(~ query, scales = "free_y", ncol = 2) +
  scale_y_reordered() +
  scale_fill_brewer(palette = "Set1") +
  labs(
    title = "Top TF-IDF Terms by Topic",
    subtitle = "Words most distinctive to each news topic",
    x = "TF-IDF Score",
    y = NULL,
    caption = "Source: NewsAPI | Jimmy Zhenning Xu, Ph.D. | github.com/utjimmyx"
  ) +
  theme_minimal(base_size = 12)

summary_tbl <- sentiment_afinn %>%
  left_join(
    sentiment_bing %>% select(query, positive, negative, net_sentiment),
    by = "query"
  ) %>%
  rename(
    Topic = query,
    `Words Matched` = total_words,
    `Mean AFINN` = mean_sentiment,
    `AFINN Sum` = sum_sentiment,
    Positive = positive,
    Negative = negative,
    `Net (Bing)` = net_sentiment
  )

summary_tbl %>%
  kable(caption = "Sentiment Summary: All Topics") %>%
  kable_styling(
    bootstrap_options = c("striped", "hover", "condensed"),
    full_width = FALSE
  ) %>%
  column_spec(3, color = ifelse(summary_tbl$`Mean AFINN` >= 0, "darkgreen", "red"))
Sentiment Summary: All Topics
Topic Words Matched Mean AFINN AFINN Sum Positive Negative Net (Bing)
CoreWeave 5 2.000 10 9 3 6
Anthropic 6 -0.167 -1 2 5 -3
SpaceX 11 -0.455 -5 11 5 6
Klarna 12 -1.167 -14 4 10 -6