Skip to content

Commit

Permalink
Fix tutorial (#260)
Browse files Browse the repository at this point in the history
* Make CI use .net6

* Test adding in dynamicinterop dll to docs build

* Fix errors in tutorial pages
  • Loading branch information
AndrewIOM authored Aug 10, 2022
1 parent 84928e3 commit 326ae70
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
2 changes: 1 addition & 1 deletion docs/operators.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ x.Engine.Evaluate("setClass('testclass', representation(foo='character', bar='in

let test = x.Engine.Evaluate("new('testclass', foo='s4', bar=1:4)")

s4?foo
test?foo
40 changes: 24 additions & 16 deletions docs/tutorial.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ open RProvider.tseries
open RProvider.zoo

open System
open System.Net
open System.Net.Http
(**
If either of the namespaces above are unrecognized, you need to install the package in R
using `install.packages("stats")`.
Expand All @@ -70,24 +70,32 @@ In this tutorial, we use [F# Data](http://fsharp.github.io/FSharp.Data/) to acce
prices from the Yahoo Finance portal. For more information, see the documentation for the
[CSV type provider](http://fsharp.github.io/FSharp.Data/library/CsvProvider.html).
The following snippet uses the CSV type provider to generate a type `Stocks` that can be
used for parsing CSV data from Yahoo. Then it defines a function `getStockPrices` that returns
array with prices for the specified stock and a specified number of days:
The following snippet defines a function `getStockPrices` that returns
array with prices for the specified stock and a specified number of days from a stocks API:
*)
#r "nuget:FSharp.Data"
open FSharp.Data

type Stocks = CsvProvider<"http://ichart.finance.yahoo.com/table.csv?s=SPX">

/// Returns prices of a given stock for a specified number
/// of days (starting from the most recent)
let getStockPrices stock count =
let url = "http://ichart.finance.yahoo.com/table.csv?s="
[| for r in Stocks.Load(url + stock).Take(count).Rows -> float r.Open |]
|> Array.rev
// NB The 'demo' key has very limited usage.
let apiKey = "demo"

// URL of a service that generates price data
let url stock = sprintf "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=%s&apikey=%s&datatype=csv" stock apiKey

/// Get opening prices for MSFT for the last 255 days
let msftOpens = getStockPrices "MSFT" 255
/// Returns prices (as tuple) of a given stock
let getStockPrices stock count =
// Download the data and split it into lines
use wc = new HttpClient()
let data = wc.GetStringAsync(url stock) |> Async.AwaitTask |> Async.RunSynchronously
let dataLines = data.Split([| '\n' |], StringSplitOptions.RemoveEmptyEntries)

// Parse lines of the CSV file and take specified
// number of days using in the oldest to newest order
seq { for line in dataLines |> Seq.skip 1 do
let infos = line.Split(',')
yield float infos.[4] }
|> Seq.truncate count |> Array.ofSeq |> Array.rev

/// Get opening prices for MSFT for the last 100 days
let msftOpens: float[] = getStockPrices "MSFT" 100

(**
## Calling R functions
Expand Down

0 comments on commit 326ae70

Please sign in to comment.