-
Notifications
You must be signed in to change notification settings - Fork 69
Add support for remote R sessions using svSocket. #115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dcharbon
wants to merge
4
commits into
fslaborg:master
Choose a base branch
from
dcharbon:RemoteRTypeProvider
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c4f4ab5
Add support for remote R sessions using svSocket.
dcharbon eed754e
Improve the tutorial documentation for RemoteR.
dcharbon fd5f100
Improve documentation. Fix method clashes by defining the RemoteR pro…
dcharbon 1e835e1
Refactor connection code out of RemoteSession, add utilities.
dcharbon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| (*** hide ***) | ||
| // Include the right directories so that the documentation tool tips work | ||
| #nowarn "211" // Ignore warning that a search path does not exist on #I | ||
| #I "../../packages/FSharp.Data.1.1.10/lib/net40/" | ||
| #I "../../bin/" | ||
|
|
||
| (** | ||
| # RemoteR Provider Tutorial | ||
|
|
||
| ## Introduction | ||
|
|
||
| Interaction with R sessions running in a separate process, or even a separate machine | ||
| on the network, can be performed using the RProvider.RemoteSession class. The | ||
| RemoteSession class can copy variables to the remote R session, as well as from the | ||
| remote R session, and invoke arbitrary R code in the remote session. | ||
|
|
||
| This can be very useful if you want to switch between working in Fsi and working in | ||
| RGui or RStudio, or if you want to offload some processing in R to another machine. | ||
|
|
||
| To make the experience of using RemoteSession instances more like using the R type | ||
| provided by RProvider the RemoteR type provider extends the RemoteSession class | ||
| with methods and properties corresponding to functions and properties defined by R packages | ||
| for the local R installation. R packages referenced by the module "open" syntax will have | ||
| their functions and properties automatically added to the RemoteSession class, just as they | ||
| are for the R type. It's important to remember that type discovery is performed using the | ||
| local R installation; if you are connected to an R session running on a separate machine | ||
| it may not have the package you reference installed and attempting to call a function in | ||
| the remote R session provided by a package that isn't installed on that machine will result | ||
| in a runtime error. | ||
|
|
||
| ## Pre-requisites | ||
|
|
||
| The RemoteSession class requires the svSocket package in R. This package must be installed | ||
| in R for the local R installation. If you connect to a remote machine, the R installation | ||
| for that remote machine must also have the svSocket package installed. | ||
|
|
||
| As of 8/1/2014 a patched version of the compiler is required for the type provider that adds | ||
| the extension methods to RemoteSession. The source for this version and can be downloaded from | ||
| of the compiler can be downloaded and built from here: | ||
| [Extension Methods fork on CodePlex](https://visualfsharp.codeplex.com/SourceControl/network/forks/dsyme/cleanup/contribution/6853) | ||
|
|
||
| ## Tutorial Setup | ||
|
|
||
| These instructions assume that you are running RGui or RStudio on the same machine as you | ||
| are running Fsi. | ||
|
|
||
| * Start RGui or RStudio | ||
| * Make sure that svSocket package is installed | ||
| * In R, `require(svSocket)` | ||
| * In R, `startSocketServer()` | ||
|
|
||
| ## Referencing the provider | ||
|
|
||
| In order to use the RemoteR provider, you need to reference the `RDotNet.dll` library | ||
| (which is a .NET connector for R) and the `RProvider.dll` itself. For this tutorial, | ||
| we use `open` to reference a number of packages including `stats`, `tseries` and `zoo`: | ||
| *) | ||
| #I "../packages/RProvider.1.0.3/lib" | ||
| #r "RDotNet.dll" | ||
| #r "RDotNet.FSharp.dll" | ||
| #r "RDotNet.NativeLibrary.dll" | ||
| #r "RProvider.dll" | ||
| #r "RProvider.Runtime.dll" | ||
|
|
||
| open RDotNet | ||
| open RProvider | ||
| (** | ||
| These statements import the R packages named. Discovery of the packages is performed | ||
| using the local R installation. | ||
| *) | ||
| open RProvider.``base`` | ||
| open RProvider.stats | ||
| open RProvider.tseries | ||
| open RProvider.graphics | ||
|
|
||
| open System | ||
| open System.Net | ||
|
|
||
| /// Constructs a new RemoteSession. If you've compiled with an F# compiler that supports | ||
| /// generating extension methods the RR instance will provide the same functions as the | ||
| /// R type would given the R package imports above: base, stats, tseries, and graphics | ||
| let RR = new RemoteSession() | ||
|
|
||
| (** | ||
| If either of the namespaces above are unrecognized, you need to install the package in R | ||
| using `install.packages("stats")`. | ||
|
|
||
| ## Obtaining data | ||
|
|
||
| In this tutorial, we use [F# Data](http://fsharp.github.io/FSharp.Data/) to access stock | ||
| 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: | ||
| *) | ||
| #r "FSharp.Data.dll" | ||
| 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).Data -> float r.Open |] | ||
| |> Array.rev | ||
|
|
||
| /// Get opening prices for MSFT for the last 255 days | ||
| let msftOpens = getStockPrices "MSFT" 255 | ||
|
|
||
| (** | ||
| ## Calling R functions | ||
|
|
||
| Now, we're ready to call R functions using the type provider. The following snippet takes | ||
| `msftOpens`, calculates logarithm of the values using `R.log` and then calculates the | ||
| differences of the resulting vector using `R.diff`: | ||
| *) | ||
|
|
||
| /// Retrieve stock price time series and compute returns | ||
| let msft = msftOpens |> RR.log |> RR.diff | ||
|
|
||
| /// Alternately, the RemoteR class is provided which is equivalent to the R class; however, | ||
| /// it takes a RemoteSession instance as the first argument and uses that RemoteSession instance | ||
| /// to make all R calls. | ||
| ///let msft = msftOpens |> (fun a -> RemoteR.log(RR, a) |> (fun a -> RemoteR.diff(RR, a) | ||
|
|
||
| (** | ||
| If you want to see the resulting values, you can call `msft.AsVector()` in F# Interactive. | ||
| Next, we use the `acf` function to display the atuo-correlation and call `adf_test` to | ||
| see if the `msft` returns are stationary/non-unit root: | ||
| *) | ||
|
|
||
| let a = RR.acf(msft) | ||
| let adf = RR.adf_test(msft) | ||
|
|
||
| (** | ||
| After running the first snippet, a window similar to the following should appear (note that | ||
| it might not appear as a top-most window). | ||
|
|
||
| <div style="text-align:center"> | ||
| <img src="images/acf.png" /> | ||
| </div> | ||
|
|
||
| Finally, we can obtain data for multiple different indicators and use the `R.pairs` function | ||
| to produce a matrix of scatter plots: | ||
| *) | ||
|
|
||
| // Build a list of tickers and get diff of logs of prices for each one | ||
| let tickers = | ||
| [ "MSFT"; "AAPL"; "X"; "VXX"; "SPX"; "GLD" ] | ||
| let data = | ||
| [ for t in tickers -> | ||
| printfn "got one!" | ||
| t, getStockPrices t 255 |> RR.log |> RR.diff ] | ||
|
|
||
| // Create an R data frame with the data and call 'R.pairs' | ||
| let df = RR.data_frame(namedParams data) | ||
| RR.pairs(df) | ||
|
|
||
| // Create variable in the remote session and copy a value to it | ||
| RR?foo <- "foo" | ||
| // Retrieve a remote variable as a RemoteSymbolicExpression | ||
| let remoteFoo = RR?foo | ||
| // Get a local SymbolicExpression for the remote one | ||
| let localFoo = remoteFoo.ToLocalValue() | ||
| // Get an unnamed RemoteSymbolicExpression for a value | ||
| (32).ToRemoteValue(RR) | ||
|
|
||
| (** | ||
| As a result, you should see a window showing results similar to these: | ||
|
|
||
| <div style="text-align:center"> | ||
| <img src="images/pairs.png" /> | ||
| </div> | ||
|
|
||
| *) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this example work if you don't have the compiler patched to support provided extension methods?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, the example will not work if you don't have the patched compiler or a later compiler version that includes the extension method support.