This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.0.4
## ── 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(rvest)
##
## Attaching package: 'rvest'
##
## The following object is masked from 'package:readr':
##
## guess_encoding
library(ggplot2)
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
##
##
## ######################### Warning from 'xts' package ##########################
## # #
## # The dplyr lag() function breaks how base R's lag() function is supposed to #
## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or #
## # source() into this session won't work correctly. #
## # #
## # Use stats::lag() to make sure you're not using dplyr::lag(), or you can add #
## # conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop #
## # dplyr from breaking base R's lag() function. #
## # #
## # Code in packages is not affected. It's protected by R's namespace mechanism #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning. #
## # #
## ###############################################################################
##
## Attaching package: 'xts'
##
## The following objects are masked from 'package:dplyr':
##
## first, last
##
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(kableExtra)
##
## Attaching package: 'kableExtra'
##
## The following object is masked from 'package:dplyr':
##
## group_rows
ticker <- "AAPL" # Apple Inc.
finviz_url <- paste0("https://finviz.com/quote.ashx?t=", ticker)
webpage <- read_html(finviz_url)
data <- webpage %>%
html_nodes("table.snapshot-table2") %>%
html_table(fill = TRUE)
if (length(data) > 0) {
financials <- data[[1]]
colnames(financials) <- c("Metric", "Value")
} else {
financials <- data.frame(Metric = NA, Value = NA)
}
# Display Financials as a Table
kable(financials, caption = paste("Key Financials for", ticker))
| Metric | Value | NA | NA | NA | NA | NA | NA | NA | NA | NA | NA |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Index | DJIA, NDX, S&P 500 | P/E | 36.00 | EPS (ttm) | 6.29 | Insider Own | 0.09% | Shs Outstand | 15.04B | Perf Week | -4.86% |
| Market Cap | 3401.91B | Forward P/E | 27.64 | EPS next Y | 8.19 | Insider Trans | -5.39% | Shs Float | 15.01B | Perf Month | -2.90% |
| Income | 96.15B | PEG | 3.66 | EPS next Q | 1.61 | Inst Own | 63.75% | Short Float | 0.85% | Perf Quarter | -6.81% |
| Sales | 395.76B | P/S | 8.60 | EPS this Y | 8.40% | Inst Trans | 1.75% | Short Ratio | 2.50 | Perf Half Y | 1.83% |
| Book/sh | 4.44 | P/B | 51.02 | EPS next Y | 11.97% | ROA | 27.57% | Short Interest | 127.89M | Perf Year | 33.91% |
| Cash/sh | 3.58 | P/C | 63.26 | EPS next 5Y | 9.83% | ROE | 136.52% | 52W Range | 164.07 - 260.10 | Perf YTD | -9.57% |
| Dividend Est. | 1.03 (0.45%) | P/FCF | 34.61 | EPS past 5Y | 15.41% | ROI | 63.80% | 52W High | -12.93% | Beta | 1.24 |
| Dividend TTM | 1.00 (0.44%) | Quick Ratio | 0.88 | Sales past 5Y | 9.18% | Gross Margin | 46.52% | 52W Low | 38.02% | ATR (14) | 6.58 |
| Dividend Ex-Date | Feb 10, 2025 | Current Ratio | 0.92 | EPS Y/Y TTM | -2.14% | Oper. Margin | 31.76% | RSI (14) | 36.83 | Volatility | 2.73% 2.33% |
| Employees | 164000 | Debt/Eq | 1.45 | Sales Y/Y TTM | 2.61% | Profit Margin | 24.30% | Recom | 2.14 | Target Price | 254.02 |
| Option/Short | Yes / Yes | LT Debt/Eq | 1.26 | EPS Q/Q | 10.13% | Payout | 16.11% | Rel Volume | 1.58 | Prev Close | 239.07 |
| Sales Surprise | 0.03% | EPS Surprise | 2.23% | Sales Q/Q | 3.95% | Earnings | Jan 30 AMC | Avg Volume | 51.20M | Price | 226.46 |
| SMA20 | -5.41% | SMA50 | -4.92% | SMA200 | -0.35% | Trades | Volume | 26,586,786 | Change | -5.27% |
# Get Stock Data from Yahoo Finance
getSymbols(ticker, src = "yahoo", from = Sys.Date() - 365, to = Sys.Date())
## [1] "AAPL"
# Plot Stock Price
chartSeries(get(ticker), theme = chartTheme("white"), name = paste("Stock Price of", ticker))
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.