Learn how to develop a chart generator for the stock price generator, by using the F# R type provider to generate ggplot2 charts.
- [Instructor] And returning to Visual Studio, I'm going to add a new project. This is going to be once again a Library. It's going to be called Visualisation. Again delete the scaffold. And I've pasted some code there. You'll see we've got a bunch of red squigglies around the opens. So we are going to install some references via NuGet. And we want something that's sponsored by BlueMountain Capital called Rprovider.
Highest dependencies. Install. And we just rename the library. And we're also going to install something called Deedle, which gives us a way of kind of mirroring the way that R likes to stall and manipulate data sets. Again that's via NuGet. And then go for Deedle.RPlugin. And you'll see I'm getting an error around System.Data. That's because I need to manually add a reference to System.Data.
Not via NuGet but just via the built-in Visual Studio stuff. And also references to StockChartBot.Data and ROP. Those are project references. Okay that's the donkey work done. I'll just talk you very briefly through this helper function here, because we're going to need R to or ggplot2 to render to a file. We're going to need to have a way of making temporary files and that is this. It just uses the built-in Windows functionality for making a temporary file.
It generates a suitable name for the file because we're going to attach PNG files to our Twitter replies because that's the kind of graphics which Twitter likes to attach. And we're going to rename the generated temporary file with that extension. And then we're going to return the renamed fileName. Okay a little more pasted code. This code, by the way, comes from the blog of someone called Evelina Gabasova. She's a very expert data scientist who is very active in the F# and ggplot2 and R community.
I will place the link to her blog in the source code examples. And I very much urge you, once you finish watching this course, to go and look at the stuff she's done, if you're interested in visualisation. So just to reiterate, lots of this code is adapted from stuff that Evelina's done. So here, we're making ourself a little operator that can take two SymbolicExpression objects, which are part of the R ecosystem, and combine them using the R plus operator. R has this concept, or ggplot2 has this concept, of combining elements of a chart by adding them together.
And this gives us an operator to do that. And then this function here, is a way of standardizing R fontSettings, again, by doing R calls and adding them together using the operator we just defined. And a third piece of pasted code. This is going to take a Prices object, which you're familiar with from the previous section, and it's going to generate a visualisation of those Prices and return, actually, the fileName where that visualisation has been rendered to.
And the way we do that is in the try with block. So that we can handle failures using R Choice.fail model. Here, we're instantiating a Deedle data frame. And Deedle has a way of taking a high numerable ofRecords types and producing a dataFrame with all the kind of named columns, which things like R like to have. So that's kind of built in using ofRecords. Then here's where the type provider stuff comes in. Because we've got a name space called R, and within R, you've got access to all sorts of things, which R provides.
Basically all of R's functions. So there's a massive list of all R's built-in, and, indeed, extension libraries and that kind of thing. Among those are ggplot2. So it can call ggplot. And because R has a very different way of providing function parameters, we need to insert a reference to this helper object namedParams, which is basically producing a dictionary of parameter names to parameter values.
So we're saying here that the data I want you to plot is in this dataFrame we generated here. Because of the slight difference in the way types are handled, we need to box, or produce really, a pointer to that dataFrame. So all the values on the right-hand side of these pairs, you'll see, have all had box applied to them. There's a mapping parameter, which is going to do things to allow us to label our axes Date and Close. And then we're going to call an R function called geom_line, which let's us set the size, again using box, of the line, the color, and forestgreen is a built-in kind of default color name.
And we're also going to plus in a call to these standard font settings, which are going to give us sensible font sizes for the chart we're going to attach to our tweet. And then, because we're actually, we don't need to preserve the results for all those calls because R has a kind of state of its own, so we don't need to keep track of it, we're actually going to ignore the results of all that. Then we're going to do a makeFile, using that function I outlined earlier. And we're going to do an R.ggsave using that fileName, setting some size parameters in centimeters.
And, again, ignoring the result, because R is keeping track of its own state. And then we're returning that fileName wrapped in a succeed instance.
Released
12/20/2016- Defining values and calling functions in F#
- Defining and identifying discriminated unions
- Working with if-else expressions
- Writing unit test
- Using type providers to access data
- Analyzing data with collection functions
- Plotting data using the R type provider
- Using railway-oriented programming to handle errors
- Integrating with Twitter
- Deploying an F# application to Azure
Share this video
Embed this video
Video: Develop the chart generator