How to implement a read table function in F#? (like Vlookup) - f#

I need a function.
When I enter 0~15 and Protection, it'll return 45%.
Just like Vlookup function in Excel.
Is there a function like this one in F#?
(At website try F#, Learn -> Financial Modeling -> Using the Yahoo Finance Type Provider
It recommended us to use Samples.Csv.dll. However, I failed to install it and don't want to install that package just for a function :(.. )
I followed the tutorial (http://fsharp.github.io/FSharp.Data/library/CsvProvider.html)
and tried to run the program on my computer. But I am in trouble now
It couldn't identify the type CsvProvider (So I can't use the function Stocks.Load.)
What's the problem..?

This is how the code looks when using the CSV type provider in F# Data. To get this to work, you'll need to add reference to FSharp.Data.dll. The best way to do this is to install the package from NuGet. In Visual Studio, it will add reference for you, and in command line you can say:
nuget install FSharp.Data
Alternatively, if you are in an F# script file, then you need to install the nuget package and then add #r #"C:\path\to\FSharp.Data.dll". Then you can write the following:
open FSharp.Data
// Generate type based on a local copy with sample data
type Data = CsvProvider<"sample.csv">
// Load actual data from a file (this can be a different file with the same structure)
let loaded = Data.Load("runtime/file/name.csv")
// Find row for a specified age range & look at the properties
let row = loaded.Data |> Seq.find (fun r -> r.Age = "0~15")
row.Protection
row.Saving
row.Specified

A very simple way to do this is with a DataTable:
open System.Data
open System.IO
open LumenWorks.Framework.IO.Csv
let vlookup =
let table = new DataTable()
do
use streamReader = new StreamReader(#"C:\data.csv")
use csvReader = new CsvReader(streamReader, hasHeaders=true)
table.Load(csvReader)
table.PrimaryKey <- [|table.Columns.["Age"]|]
fun age (column: string) -> table.Rows.Find([|age|]).[column]
//Usage
vlookup "0~15" "Protection" |> printfn "%A"
There's no lack of CSV readers out there. I used this especially fast one (also available on NuGet).

Related

lines=True parameter for the Json Type Provider and Json.Net library?

I am working on this Kaggle competition. The Jupyter notebooks on Kaggle only support R and Python and I wanted to use F# locally. The problem is that the datasets are .json files and both the F# Json Type Provider and Newtonsoft libraries fail when trying to parse the files.
Here are examples of the code failing in F#:
open FSharp.Data
type Context = JsonProvider<"train.json">
let context = Context.
and
open System
open System.IO
open Newtonsoft.Json
open Newtonsoft.Json.Linq
let object = JObject.Parse(File.ReadAllText("train.json"));
object
This Python example uses these line of code to parse them correctly:
train = pd.read_json('../input/stanford-covid-vaccine/train.json', lines=True)
test = pd.read_json('../input/stanford-covid-vaccine/test.json', lines=True)
In the notebook, the author says that without the "lines=True" parameter, the read_json method fails with this trailing error.
My question: assuming tis is the same error, is there a way to apply that same kind of "lines=true" to the .NET libraries to parse the json?
I've seen a few datasets where the format was one valid JSON record per line:
{"event":"nothing 1"}
{"event":"nothing 2"}
{"event":"nothing 3"}
This is not valid JSON overall. I think you can either parse it line-by-line or you can turn it into valid JSON. For line-by-line parsing (which may be more efficient as you can do this in a streaming fashion), I would use:
open FSharp.Data
type Log = JsonProvider<"""{"event":"nothing 1"}""">
for line in File.ReadAllLines("some.json") do
let l = Log.Parse(line)
printfn "%s" l.Event

which library is GetSamples in

Referenced here
Add calculated key to collection
not in FSharp.Data 2.4.6
and no name space referenced in the Answer given by the great TP
GetSample is present.....
GetSamples can be used on your JSON data represented by a JsonProvider.
Install the FSharp.Data Nuget package and add the import declaration by using open FSharp.Data. Once you have defined a JSON file or some JSON data you should be able to process it further.
open FSharp.Data
//type Values = JsonProvider<"yourData.json">
type Values = JsonProvider<""" [{"Name":"Hello"}, {"Name":"World"}] """>
printfn "%A" (Values.GetSamples())

Can I create a conditional literal?

In order to create a Json provider I need to pass a literal with the path. There are several people working on the project from different locations, and the paths are different in each case. (Actually only the beginning of each path). I tried to create a literal with pattern matching but the compiler does not accept it. Is there another way to do this?
My failed attempt is below:
open FSharp.Data
[<Literal>]
let bitbucketRoot = // Error message: This is not a valid constant expression
let computerName = Environment.MachineName
match computerName with
| "DESKTOP-G3OF32U" -> "C:\\Users\\Fernando"
| "HPW8" -> #"H:\Dropbox\"
| _ -> failwith "Unknown computer"
[<Literal>] // Error message: This is not a valid constant expression
let projDataPath = bitbucketRoot + #"Bitbucket\VSProjects\Fractal10\Fractal10\data\"
[<Literal>] // Error message: This is not a valid constant expression
let jsonPath = projDataPath + "fractal.json"
type PathInfo = JsonProvider<Sample=jsonPath>
I would advise that you store it in source control and make it a path relative to your project root, assuming you are working out of a common source control repository.
Either that, or host the sample on a public URL. (I wouldn't actually recommend this because including it in your source repository allows versioning and doesn't publicly expose your data)
You cannot create a conditional literal as the other comments point it out. However this is a fairly frequent use case and the way to deal with it is as follows:
#r #"..\packages\FSharp.Data\lib\net40\FSharp.Data.dll"
open FSharp.Data
open System
open System.IO
[<Literal>]
let JsonSource = __SOURCE_DIRECTORY__ + #"\test.json"
type JSonType = JsonProvider<JsonSource>
let json1 = JSonType.GetSamples()
let anotherPath = #"C:\tmp"
let anotherJson = anotherPath + #"\test.json"
let json2 = JSonType.Load(anotherJson)
The __SOURCE_DIRECTORY__ directive will point to the project root (just display it in the REPL) and then you can add the filename to it and make that a literal. If you check in this file into a git repo, then everyone who checks it out can have it in a relative path, and you can refer it when generating the type. When actually using the type or referring to the full file you can just use the .Load() method to load any file, and this doesn't have to be a literal.
There is actually a second way, which could work for you depending on the circumstances, compile a sample, and distribute it as a .dll. You can refer to this and use it directly without having access to the actual file. Please see the Using the JSON Provider in a Library section at the end of the documentation.
I have not tried referring to the json in a config file, it might also be possible.

F# Excel Interop: Marshal.GetActiveObject("Excel.Application") does not work

I am trying to use F# to automate some Excel tasks. I actually got two issues:
1. If I open a workbook using an Excel.Application instance, I would miss all the add-ins that would have been automatically loaded if I had just opened the workbook in Excel. So I try to open the workbook in Excel first, with all add-ins loaded, and then hand the instance to F#. Then I got the second issue:
2. The Marshal.GetActiveObject("Excel.Application") actually open a new instance, instead of getting the existing instance. The code is below:
#r "Microsoft.Office.Interop.Excel"
#r "office"
open Microsoft.Office.Interop
open System
open System.Runtime.InteropServices
let app = Marshal.GetActiveObject("Excel.Application")
let app1 = app :?> Excel.Application
let wb = app1.ActiveWorkbook
let visible = app1.Visible
let n = app1.Workbooks.Count
and the output is:
val app : obj
val app1 : Excel.Application
val wb : Excel.Workbook = null
val visible : bool = false
val n : int = 0
But I am very sure I have an Excel instance running and it is visible. Thanks!
I have double checked my code and this is the way to use Excel from F# script (use Excel-DNA for other cases). However, very often (especially when using add-ins) Excel does not close properly and some semi-dead Excel processes hang in the task manager until they are manually killed. Probably you are getting a wrong process, not the one where you open your workbook.

#load a package in F# interactive (FSharpChart.fsx)

Hi i'm a noob and asking this newbie question, please forgive me.
I've installed successfully FSharpChart in my local directory
...
Added package 'MSDN.FSharpChart.dll.0.60.0' to folder 'C:\Users\Fagui\Documents\GitHub\Learning Fsharp\Expert in F\packages'
Added package 'MSDN.FSharpChart.dll.0.60.0' to 'packages.config'
Successfully installed 'MSDN.FSharpChart.dll 0.60.0' to Expert in F
now, if i do
#load "FSharpChart.fsx";;
^^^^^^^^^^^^^^^^^^^^^^^
stdin(4,1): error FS0078: Unable to find the file 'FSharpChart.fsx' in any of
C:\Users\Fagui\AppData\Local\Temp
additional info:
inside this folder, i see a nupkg file, and a lib directory
Inside the lib directory, there is a dll, and a pdf file,
but i don't see any .fsx file.
basically, F# has installed the package in the active folder for the current project, and F#interactive is in another folder ?? bit strange ?
should i install another time the package ? or what is the way around it ?
thanks
UPDATE:
i don't know why, but apparently when i installed the FSharpChart package, I only got the dll, no fsx file
i managed to load it doing
#I #"C:\Users\Fagui\Documents\GitHub\Learning Fsharp\Expert in F"
#r #"packages\MSDN.FSharpChart.dll.0.60\lib\MSDN.FSharpChart.dll";;
unfortunately, typing the script in F# interactive
open MSDN.FSharp.Charting
let rnd = System.Random()
let rand() = rnd.NextDouble()
let randomPoints = [for i in 0 .. 1000 -> 10.0 * rand(), 10.0 * rand()]
randomPoints |> FSharpChart.Point;;
doesn't yield any chart, but just returns a list
val rnd : Random
val rand : unit -> float
val randomPoints : (float * float) list =
[(9.765916457, 2.272289941); (0.8211438594, 1.625466995);
...
(7.783786034, 7.572208311); (6.497914692, 3.66987128); ...]
val it : ChartTypes.PointChart
this may be due to the fact that the library is not supported anymore, and that i should use a newer library like Thomas Petricek indicated.
So, i did manage to install FSharp.Charting instead
let rnd = System.Random()
let rand() = rnd.NextDouble()
let randomPoints = [for i in 0 .. 1000 -> 10.0 * rand(), 10.0 * rand()]
randomPoints |> Chart.Point;;
and it did work
There is a newer version of the FSharpChart.fsx library which is called F# Charting, so first of all, I would recommend using this newer library instead (the API is quite similar, but F# Charting has a number of improvements).
The documentation for F# Charting also has a detailed page on referencing the library.
Typically, when you reference the library using NuGet, you'll need to specify relative reference:
// On Mac OSX use packages/FSharp.Charting.Gtk.0.90.13/FSharp.Charting.Gtk.fsx
#load "packages/FSharp.Charting.0.90.13/FSharp.Charting.fsx"
Where 0.90.13 is the version of the library that you got from NuGet (you may need to check the folder name - the path references in #load are relative to the place where your script lives).
Yes, F# Interactive is independent of the current project.
Use:
#load #"C:\Users\Fagui\Documents\GitHub\Learning Fsharp\Expert in F\packages\FSharpChart.fsx";;
Also you can use the #I directive if you need to reference assemblies of a specific folder, see the reference.

Resources