Skip to content

Commit fdbec0b

Browse files
authored
Add Jump Start example stock-api-plumber (#66)
* Add Jump Start example stock-api-plumber * Bump version for release * Update extension to R 4.2.3 * Update manifest using R 4.4.3, remove renv.lock * Add minimumConnectVersion * Try R 4.4.0 * Update README to reflect correct R version * Update manifest description
1 parent 914825d commit fdbec0b

File tree

7 files changed

+1018
-0
lines changed

7 files changed

+1018
-0
lines changed

.github/workflows/extensions.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ jobs:
3434
quarto-stock-report-python: extensions/quarto-stock-report-python/**
3535
portfolio-dashboard: extensions/portfolio-dashboard/**
3636
quarto-document: extensions/quarto-document/**
37+
stock-api-plumber: extensions/stock-api-plumber/**
3738
stock-api-flask: extensions/stock-api-flask/**
3839
landing-page: extensions/landing-page/**
3940
stock-api-fastapi: extensions/stock-api-fastapi/**

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# RStudio
22
.Rproj.user
33
**/renv/
4+
.Rprofile
45

56
# Quarto
67
.quarto/
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Stock Pricing Service
2+
3+
## About this example
4+
5+
An API allows you to turn your models into production services that other tools and teams can use. APIs are a great way for software engineering teams to use your models without translating them into different languages.
6+
7+
8+
## Learn more
9+
10+
* [Plumber Introduction](https://www.rplumber.io/)
11+
* [Plumber Documentation](https://www.rplumber.io/docs/)
12+
13+
## Requirements
14+
15+
* Posit Connect license allows API publishing
16+
* R version 4.4.0 or higher

extensions/stock-api-plumber/manifest.json

Lines changed: 914 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#* @apiTitle Stock Information API
2+
#* @apiVersion 1.0.0
3+
#* @apiDescription This Plumber API surfaces the ability to programmatically request price history or a number representing the relative volatility of a stock ticker
4+
5+
library(dplyr)
6+
7+
valid_tickers <- c(
8+
Apple = "AAPL",
9+
Amazon = "AMZN",
10+
Facebook = "FB",
11+
Google = "GOOG",
12+
Intel = "INTC",
13+
Microsoft = "MSFT"
14+
)
15+
16+
all_data <- readRDS("stock_data.rds") %>%
17+
as_tibble()
18+
19+
get_price_data <- function(data, ticker = "AMZN", from = "2010-01-01") {
20+
data %>%
21+
filter(
22+
ticker == {{ ticker }},
23+
date >= as.Date(from, format="%Y-%m-%d")
24+
) %>%
25+
collect()
26+
}
27+
28+
#* Protect against an invalid ticker
29+
#* @filter checkTicker
30+
function(req, res) {
31+
if (!is.null(req$args$ticker) && !req$args$ticker %in% valid_tickers) {
32+
res$status <- 400
33+
return(
34+
list(
35+
error = paste(
36+
"Invalid ticker. Please use one of",
37+
paste(
38+
"'", valid_tickers, "'",
39+
sep = "", collapse = ", "
40+
)
41+
)
42+
)
43+
)
44+
} else {
45+
plumber::forward()
46+
}
47+
}
48+
49+
#* @get /price
50+
#* @param ticker:character ticker symbol (MSFT; AMZN; AAPL; FB; GOOG)
51+
#* @response 200 Returns price for ticker
52+
#* @response 400 Bad ticker
53+
#* @response 500 Bad ticker
54+
#* @response default Returns price for ticker
55+
price <- function(ticker = "AMZN") {
56+
get_price_data(all_data, ticker, "2010-01-01")
57+
}
58+
59+
#* @get /volatility
60+
#* @param ticker:character ticker symbol (MSFT; AMZN; AAPL; FB; GOOG)
61+
#* @response 200 Returns volatility for ticker
62+
#* @response 400 Bad ticker
63+
#* @response 500 Bad ticker
64+
#* @response default Returns volatility for ticker
65+
volatility <- function(ticker = "AMZN") {
66+
price <- get_price_data(all_data, ticker, "2010-01-01") %>%
67+
select(date, adjusted) %>%
68+
mutate(returns = (log(adjusted) - log(lag(adjusted)))) %>%
69+
na.omit() %>%
70+
summarize(volatility = var(returns))
71+
72+
list(ticker = ticker, volatility = price$volatility)
73+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Version: 1.0
2+
3+
RestoreWorkspace: Default
4+
SaveWorkspace: Default
5+
AlwaysSaveHistory: Default
6+
7+
EnableCodeIndexing: Yes
8+
UseSpacesForTab: Yes
9+
NumSpacesForTab: 2
10+
Encoding: UTF-8
11+
12+
RnwWeave: Sweave
13+
LaTeX: pdfLaTeX
293 KB
Binary file not shown.

0 commit comments

Comments
 (0)