fslaborg / rprovider Goto Github PK
View Code? Open in Web Editor NEWAccess R packages from F#
Home Page: http://fslab.org/RProvider/
License: Other
Access R packages from F#
Home Page: http://fslab.org/RProvider/
License: Other
Seems like the call to R_setupMainLoop from R.NET hangs.
Works in the compiler and IDE, for some reason, though not in F# interactive.
Haven't tried compiled code.
I spent quite some time searching for any hints in the sources or docs for a slice.
I couldn't find it. As it is a key operation in R I would suggest adding it.
Do I miss something obvious?
An attempt to reference provider dll produces the following error from fcs:
error FS3053: The type provider 'RProvider.RProvider' reported an error: The type provider constructor has thrown an exception: The type initializer for '<StartupCode$RProvider>.$RInterop' threw an exception.
Although source code specifies the cause as absence of R installed, the correspondent exception when intercepted by fsc gets an inner exception and detailed problem cause message gets buried. To see the real cause of the problem RProvider should be started under debugger, which is detrimental for non-advanced users.
Would it be feasible to expose F# dlls as R packages? That would complement the R Provider nicely, so you could have full roundtrip, and have people more comfortable with R than F# to be able to start using F#
For example, example.fsx does not build on a vanilla R installation because zoo and tseries are not installed. Should be able to put packages in a directory in your source control so that they are available for build.
Automatic package restore in VS or command line apparently does not run the init.ps1
script, so type provider fails to load (at least it looks like that on my laptop). Not sure if there is any possible workaround for this though...
Namespace appears but no functions appear in the Intellisense. If you try to call R.qplot from the IDE, it does not produce anything.
I've obtained a 32-bit libR.dylib from the last 32-bit build of R on Mac OSX, 2.15.3. How should I set it up to load RProvider in fsharpi? So far I can open RProvider but not RProvider.base
, says namespace 'base' is not defined
A+
I just want to share experience for people who may struggle figuring out which entry in the registry is actually used to select the R install.
My experience is that R entries in windows registry are quite messy and I spent a bit of time figuring out exactly WHICH entry in the registry is used to find R by the type provider.
In my registry (win64), I can find paths to R install in :
HKEY_LOCAL_MACHINE\Software\R-core\R\InstallPath
HKEY_LOCAL_MACHINE\Software\R-core\R[version]\InstallPath
HKEY_LOCAL_MACHINE\Software\R-core\R64\InstallPath
HKEY_LOCAL_MACHINE\Software\R-core\R64[version]\InstallPath
HKEY_LOCAL_MACHINE\Software\Wow6432Node\R-core\R\InstallPath
HKEY_LOCAL_MACHINE\Software\Wow6432Node\R-core\R[version]\InstallPath
HKEY_LOCAL_MACHINE\Software\Wow6432Node\R-core\R32\InstallPath
HKEY_LOCAL_MACHINE\Software\Wow6432Node\R-core\R32[version]\InstallPath
In Visual Studio and for x86 process, the key actually used, on my machine, is
HKEY_LOCAL_MACHINE\Software\Wow6432Node\R-core\R\InstallPath
For a x64 process, it is :
HKEY_LOCAL_MACHINE\Software\R-core\R64\InstallPath
The InstallPath under [version] does not seem be used.
Hope this may help someone.
Currently, bindings are set in R forever and never released
I am using the example.fsx file in the RProvider. When I run it from home, it works like a champ. When I run it from work, I get a:
System.Net.WebException: The remote server returned an error: (407) Proxy Authentication Required.
at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
at System.Net.WebClient.DownloadString(Uri address)
at System.Net.WebClient.DownloadString(String address)
at FSI_0002.getStockPrices(String stock, Int32 count) in C:\temp\Open Source R Type Provider 1.0.3\FSharpRProvider-master\src\RProvider\example.fsx:line 24
at <StartupCode$FSI_0002>.$FSI_0002.main@() in C:\temp\Open Source R Type Provider 1.0.3\FSharpRProvider-master\src\RProvider\example.fsx:line 35
Stopped due to error
I checked in Stackoverflow here: http://stackoverflow.com/questions/18981682/f-wsdlservice-type-provider-proxy
I see that type providers are not proxy aware at design time. What about run-time? What do I need to do to get to the service?
This issue may be connected with
[Win x64] R installation & Registry
#28
Code (script1.fsx):
open RDotNet
open RProvider
open RProvider.base
Error:
The namespace 'base' is not defined.
One more hint:
RProvider is visible in References.
I am using Rx64 3.0.2 only. 32-bit version is not installed.
I have tried adding \bin to path without effect.
Thanks in advance for help.
Visual Studio crashes and restarts intermittently when running the simple program below.
Additionally the type provider occasionally crashes and intellisense is lost. This is restored only by restarting VS. Exception is non-specific, e.g. "an exception was thrown"
RProvider 1.0.3
R.NET 1.5.5
RDotNet.FSharp 0.1.2.1
R 3.0.2
Win 7 Pro x64
module main
open RProvider
open RProvider.base
let x = R.c(1,2,3)
I want to try R.sapply functionality which requires me to specify a function that is to apply to a vector, just as I do in R. But I don't know how to construct a R function in a proper way.
In many situations, we may need "apply" functions. I wonder whether I can boost my computation by parallel library, but I'm again stuck by the clusterApply function which also needs a function parameter.
Anybody has an idea?
Currently we expose them as a vararg function because we cannot get their formals.
To actually get some strong-typing.
Hi all.
I've got a problem I don't understand...
Until a couple of days ago everything (F#-R-Excel Add-Ins) was working fine. Now I get only #value errors in Excel when using R functions.
A trivial example that generates am Excel #value error is attached... Please, note that when I substitute R.pi.GetValue() with a regular float the Excel function works fine.
namespace R_modeling
[]
module linear_model =
open RDotNet
open RProvider
open RProvider.``base``
open RProvider.stats
open ExcelDna.Integration
open System
[<ExcelFunction(Name="r_CpOver", Description="A copy using R", Category="RFunctions")>]
let r_CopyOver([<ExcelArgument(Name="adds Pi in R", Description = "Rpi addition")>] anumber:float ) =
let pi_plus = R.pi.GetValue<float>() + anumber //2.0 + anumber //works!
pi_plus
Any help will be greatly appreciated.
Thanks in advance.
I make some unit tests and run them with NUnit Test Adapter 1.0.0.0 on VS 2012. If I run tests with R.NET without RProvider, Dispose
method works fine. If I run tests with RProvider, Dispose
method stops at R_CleanTempDir
.
Intensive calling for R functions like lm() is highly probable to cause crash. The code may look like this:
open RProvider
open RProvider.``base``
open RProvider.stats
open RProvider.fUnitRoots
let s =
[|1..2000|]
|> Array.map (fun i ->
let x = R.rnorm(100)
let y = R.rnorm(100)
R.assign("x",x) |> ignore
R.assign("y",y) |> ignore
let m = R.lm("y~x") // It almost always crashes at this line.
let res = m.AsList().["residuals"].AsNumeric()
let test1 = R.adfTest(res)
let p:double = test1.GetAttribute("test").AsList().["p.value"].AsNumeric().[0]
p
)
When the program is running, it almost always crashes for the following exception:
An unhandled exception of type 'System.AccessViolationException' occurred in RDotNet.dll
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
However, when I directly use RDotNet to implement exactly the same thing, no error occurs at all. Actually, I "solved" the problem by adding a GC.Collect() in each iteration, and the probability of error is hugely reduced. I wonder what's the root of the error?
If the zoo package is not installed, we should not fail until we actually need it (e.g. when you just use data frames)
RProvider v1.0.4
tries to load assemblies from RProvider.1.0.3
folder. Script RProvider.fsx - Lines 7-9
are incorrect and latest NuGet package is broken.
Please update RProvider.fsx
, increase version and release new package.
RDotNet provides SymbolicExpression object to get access to R objects. Most R objects are constructed as Lists.
For example, a t-test result is a list of statistics and other things, a linear model (lm) is a list of coefficients, residuals and other useful list members. However, it is quite inconvenient to extract the members from a list without chaining several methods and convert the value to appropriate types.
In some packages, the R functions returns S4 object. For example, fUnitRoots package provides a unitrootTest function which returns S4 object that stores much richer information than offered by official R stats packages. But it's a nightmare to extract wanted information from these Lists or the slots of S4 objects. R methods package provide slot() function to extract slots from S4 objects.
As a user of C# and F#, I just want to conveniently get access to the inner information of the R objects without caring too much about the nature of the R object.
Here I developed a very rough RDotNet.Extensions project using F# and RProvider to extend SymbolicExpression:
module RDotNet.Extensions
open RDotNet
open RDotNet.Internals
open RProvider
open RProvider.``base``
open RProvider.methods
type SymbolicExpression with
/// Get the member symbolic expression of List or S4 object.
member this.Member (name: string) =
match this.Type with
| SymbolicExpressionType.List -> this.AsList().[name]
| SymbolicExpressionType.S4 -> R.slot(this,name)
| _ -> invalidOp "Unsupported operation on R object"
/// Convert the symbolic expression to a typed array.
member this.ToArray<'a> () = this.Value :?> 'a[]
/// Get the value from the typed vector by name.
member this.ValueOf<'a> (name: string) =
match this.Type with
| SymbolicExpressionType.NumericVector -> box(this.AsNumeric().[name]) :?> 'a
| SymbolicExpressionType.CharacterVector -> box(this.AsCharacter().[name]) :?> 'a
| SymbolicExpressionType.LogicalVector -> box(this.AsLogical().[name]) :?> 'a
| SymbolicExpressionType.IntegerVector -> box(this.AsInteger().[name]) :?> 'a
| SymbolicExpressionType.ComplexVector -> box(this.AsComplex().[name]) :?> 'a
| SymbolicExpressionType.RawVector -> box(this.AsRaw().[name]) :?> 'a
| _ -> invalidOp "Unsupported operation on R object"
/// Get the value from the typed vector by index.
member this.ValueAt<'a> (index: int) = box(this.ToArray<'a>().[index]) :?> 'a
/// Get the first value from the typed vector.
member this.First<'a> () = this.ValueAt<'a>(0)
/// Assign this symbolic expression to a string symbolic identifier in current R environment.
member this.AssignTo(name:string) = R.assign(name,this) |> ignore
This extension currently allows me to write code in a fluent style like this:
let dic = dict [("ma",R.c(0.6,0.4,0.1).Value)]
let list = R.list(dic)
let data1 = R.arima_sim(model=list,n=500)
let model = R.arima(x=data1,order=R.c(0,0,5))
let coef:double[] = model.Member("coef").ToArray()
let data = R.rnorm(1000)
let test = R.unitrootTest(data)
let pvalue:double = test.Member("test").Member("p.value").ValueOf("n") // test is S4 object
I suggest that RDotNet or RProvider adopt some extensions to allow such fluent-style of accessing the data members of R objects.
This issue is raised in RDotNet community, I hope RProvider can also do something about it.
When I install the NuGet package, and add references in a FSI file:
... the last line shows an error:
"The type provider ... reported an error: Assembly Attribute TypeProviderAssemblyAttribute' refers to a designer assembly 'RProvider' which cannot be loaded or doesn't exist. Could not load file or assembly 'RDotNet, Version=1.5.4.0' ..."
I'm currently passing .NET NaN's to R via RDotNet (without RProvider) then convert them to NA's using a series of RDotNet engine.evaluate statements.
This isn't exactly elegant, so what I'd like to do is streamline the process by taking advantage of RProvider's syntax, e.g.
define a function
let nan2na (arr: RDotNet.SymbolicExpression) =
engine.SetSymbol("arr",arr)
engine.Evaluate("arr[is.nan(arr)] <- NA") |> ignore
engine.GetSymbol("arr")
// then we can do the following
let result = data |> R.data_matrix |> nan2na |> R.fancystuff_etc
However, when I try to do this I get a bunch of nulls back... along with
A first chance exception of type 'System.ArgumentException' occurred in RDotNet.dll
What is the best way of dealing with this?
Hi all.
I've got a problem I don't understand...
Until a couple of days ago everything (F#-R-Excel Add-Ins) was working fine. Now I get only #value errors in Excel when using R functions.
A trivial example that generates am Excel #value error follows (sorry, I don't know how to paste code since this is my first post...)... Please, note that when I substitute R.pi.GetValue() with a regular float the Excel function works fine.
Any help will be greatly appreciated. Thanks in advance.
namespace R_modeling
[]
module linear_model =
open RDotNet
open RProvider
open RProvider.``base``
open RProvider.stats
open ExcelDna.Integration
open System
[<ExcelFunction(Name="r_CpOver", Description="A copy using R", Category="RFunctions")>]
let r_CopyOver([<ExcelArgument(Name="adds Pi in R", Description = "Rpi addition")>] anumber:float ) =
let pi_plus = R.pi.GetValue<float>() + anumber //2.0 + anumber //works!
pi_plus
In many cases, I use R objects which is a R List and contains some other objects. If we want to access these elements, we need expr.AsList().["member"].AsNumeric() for example:
open RProvider.tseries
let x = R.rnorm(100)
let test = R.adf_test(x)
let p = test.AsList().["p.value"].AsNumeric().[0]
If we load libraries like fUnitRoots which returns S4 objects, we need to use other ways to extract the member, for example:
open RProvider.fUnitRoots
let x = R.rnorm(100)
let test = R.adfTest(x) // which returns a S4 object rather than a List object.
let p = test.GetAttribute("test").AsList().["p.value"].AsNumeric().[0]
If I define:
type SymbolicExpression with
/// Get the member symbolic expression of given name.
member this.Member(name: string) =
match this.Type with
| SymbolicExpressionType.List -> this.AsList().[name]
| SymbolicExpressionType.S4 -> this.GetAttribute(name)
| _ -> invalidOp "Unsupported operation on R object"
/// Get the value from a named vector by name.
member this.ValueOf<'a>(name: string) = this.AsVector().[name] :?> 'a
/// Get the value from an indexed vector by index.
member this.ValueAt<'a>(index: int) = this.AsVector().[index] :?> 'a
/// Get the first value of a vector.
member this.First<'a>() = this.ValueAt<'a>(0)
let inline (?) (expr:SymbolicExpression) (mem:string) =
expr.Member(mem)
This will allow me to write like this:
let x = R.rnorm(100)
let test = R.adf_test(x)
let stat:double = test?statistic.First()
let p:double = test?``p.value``.First()
for another,
let test = R.adfTest(x)
let stat:double = test?test?statistic.First()
let p:double = test?test?``p.value``.First()
which looks much cleaner if such a ? operator is defined.
My question is: Is there a better way to use more RProvider-way to access R object members (which does not bother too much about internal workings and operations of R objects but easy to operate and manipulate) rather than in a R.NET way?
I managed to get this working on linux with mono.
Would you be interested in a pull request with the changes?
Currently just supports arrays.
eg for the function rq (quantile regression, in the quantreg package), the first parameter is the regression model, passed as a formula.
I don't manage to find a way to build that kind of object.
Thx
(Just recording an idea that we discussed offline)
You can use save
and load
in R to export and import environments (e.g. the state of your research). It would be nice if the R provider was able to load the RData
files in a type-safe way and gave you access to all the variables defined in the file.
Something like (as a bonus point, the variables could be automatically typed with the appropriate type after conversion to F#):
type m = REnvironment<"MyFile.RData">
m.some
m.another
m.more
See load function.
This means we reflect on the function definition at runtime. We should probably generate a quotation at compile time that does the right thing, instead. Performance would be better.
The SymbolicExpression
object has the Print
method that formats the results nicely, so it looks useful to add something like:
fsi.AddPrinter(fun (se:RDotNet.SymbolicExpression) ->
se.Type.ToString() + "\n" + se.Print())
I suppose this has some disadvantages - for example, you might not want to always copy the data to F# Interactive.
But I think that having this by default (with the option to turn it off somehow) would make learning/using R provider a lot easier. For example:
> R.sum [ 1 .. 10 ];;
val it : RDotNet.SymbolicExpression = IntegerVector
[1] 55
> R.data_frame(namedParams [ "a", [20 .. 25 ]; "b", [25 .. 30]]);;
val it : RDotNet.SymbolicExpression =
List
a b
1 20 25
2 21 26
3 22 27
4 23 28
5 24 29
6 25 30
Maybe allow a string * 'a pair to be passed as one of the ParamArray arguments, to allow names to be passed. This would be fine as long as there is no other natural R representation for a pair.
Unless I am mistaken, there is no NuGet package for the R TypeProvider. This would make it easier to install - any plans to do that, or reasons why this would be problematic?
Would this be possible to do? Has anyone tried yet?
I see the following error when I try to script using RProvider
(VS2012 Update 3).
All required assemblies are located in the same folder with RProvider.dll
.
error FS3053: The type provider 'RProvider.RProvider' reported an error: The type provider constructor has thrown an exception: Could not load file or assembly 'RDotNet.NativeLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5f72f6e23c6920d4' or one of its dependencies. The system cannot find the file specified.
The script is following
// Do not work in VS and FSI
//#r @"D:\Research&Development\PRroviderApps\packages\RProvider.1.0.3\lib\RProvider.dll"
// Do not work in VS but work in FSI
#I @"D:\Research&Development\PRroviderApps\packages\RProvider.1.0.3\lib\"
#r "RProvider.dll"
open RProvider
I see from the readme that R 2.15 isn't supported, and that R.Net changes would be required. I also see from the RDotNet page that 2.15 is now supported from that end (http://rdotnet.codeplex.com/). Does this mean 2.15 is now working with this type provider?
Currently, RInteropInternal.convertToR searches through its various conversion methods even when passed a value which is already a SymbolicExpression (or a subclass of SymbolicExpression). It seems like it might be a useful optimization to check for SymbolicExpressions first, and skip the search if one is found.
It appears that the current behaviour is that the R provider will throw an exception if R is not installed and you reference it using #r "RProvider.dll
.
Would it make sense to handle this in some "nicer" way?
(One reason for the question is that if we include R provider in "FsLab", then we'll have a load script that could reference the R provider - even if R is not installed. We could include two load scripts, but it feels that having just one way of loading this would be nicer.)
One possible way would be to generate R
type with <summary>
saying that R is not installed (or we could generate R.<Note>
member similarly to what F# does when you need to add a reference - or used to do earlier (?))
See also fslaborg/zzarchive-FsLab#1
Just installed the NuGet package and received the same error as #38.
The init.ps1 does not seem to run, and I solved it by manually copying the file into the package directory.
Is this expected behaviour?
Executing the code below in fsi results in the following exception:
System.Exception: No converter registered for type Deedle.Frame2[[System.Tuple
2[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] or any of its base types
at [email protected](String message) in c:\dev\git\RProvider\src\RProvider\RInterop.fs:line 102
at RProvider.RInteropInternal.REngine.SetValue(REngine this, Object value, FSharpOption1 symbolName) in c:\dev\git\RProvider\src\RProvider\RInterop.fs:line 212 at RProvider.RInteropInternal.toR(Object value) in c:\dev\git\RProvider\src\RProvider\RInterop.fs:line 225 at RProvider.RInterop.passArg@312(List
1 tempSymbols, Object arg) in c:\dev\git\RProvider\src\RProvider\RInterop.fs:line 326
at [email protected](IEnumerable1& next) in c:\dev\git\RProvider\src\RProvider\RInterop.fs:line 334 at Microsoft.FSharp.Core.CompilerServices.GeneratedSequenceBase
1.MoveNextImpl()
at Microsoft.FSharp.Core.CompilerServices.GeneratedSequenceBase1.System-Collections-IEnumerator-MoveNext() at Microsoft.FSharp.Collections.SeqModule.ToArray[T](IEnumerable
1 source)
at RProvider.RInterop.callFunc(String packageName, String funcName, IEnumerable`1 argsByName, Object[] varArgs) in c:\dev\git\RProvider\src\RProvider\RInterop.fs:line 331
at RProvider.RInterop.call(String packageName, String funcName, String serializedRVal, Object[] namedArgs, Object[] varArgs) in c:\dev\git\RProvider\src\RProvider\RInterop.fs:line 375
at <StartupCode$FSI_0004>.$FSI_0004.main@() in c:\dev\ModelShift\CleanModel\Script4.fsx:line 25
open RProvider
open RDotNet
open Deedle
open RProvider.dplyr
open System
let periodMembers =[(1,1);(2,1);(3,1);(1,2);(2,2);(3,2);]
let premia =[125;135;169;231;876;24;]
let firstSeries = Series(periodMembers,premia)
let firstFrame = Frame.ofColumns["Premia"=>firstSeries]
let projectedYears = series([1=>2014;2=>2014;3=>2014;])
let projectedMonths = series([1=>"Apr";2=>"May";3=>"Jun"])
let secondFrame = Frame(["year";"month"],[projectedYears;projectedMonths;]) |> Frame.mapRowKeys (fun k -> k,0)
let fullFrame = dplyr.R.left__join (firstFrame, secondFrame, by="NULL")
Currently uses the SOFTWARE\R-core registry key, if it exists, and the PATH. R_HOME would allow users to switch between multiple versions.
I downloaded BlueMountainCapital-FSharpRProvider-v0.5-22-g090ddc1 and was trying to build the solution with VS2012.
There are the following compile errors:
3.Error 3 Lookup on object of indeterminate type based on information prior to this program point. A type annotation may be needed prior to this program point to constrain the type of the object. This may allow the lookup to be resolved. C:\FSharpProvider\BlueMountainCapital-FSharpRProvider-090ddc1\RInterop.fs 418 13 RProvider
Could You please help? I am new in F# and with VS 2012
I was just using a Debug build of the latest master (with the change made to address issue 29:
In my build, the code to initialize the engine (in RInterop) was executing before the code (in RProvider) which changes the PATH environment variable. As a consequence, the provider was failing to load during a program execution with a DllNotFound error (i.e. not finding R.dll).
I moved the PATH setting code into a function in RInterop which gets called just before the engine is initialized, and this fixed the problem.
When I execute the following code in the interactive
let d = -0.1700106725339548
let foo = R.pnorm(d, 0.0, 1.0)
let bar = foo.Value
then I get the following result:
val d : float = -0.1700106725
val foo : SymbolicExpression
val bar : obj = [|0.5|]
which is clearly wrong. If I change d to be 1 then the result is correct. What do I do wrong? Is there a problem with float arguments?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.