2 people like it.

Accessing list results by name with RProvider

R functions often return a list of results, indexed by names. This is a helper function which allows directly accessing the results using their names.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
open RDotNet
open RProvider
open RProvider.``base``
open RProvider.stats

let (?) (x:SymbolicExpression) name = 
  let nameLookup = 
    x.GetAttribute("names").AsList() 
    |> Seq.mapi (fun i n -> n.GetValue<string>(), i)  |> dict
  x.AsList().[nameLookup.[name]]

// call R function
let x = 0.1
let f = R.eval(R.parse(text="function(a){(a - 2)^2}"))
let minX = R.optim(x, f)

// access results
minX?counts
minX?value
namespace RDotNet
val x : SymbolicExpression
type SymbolicExpression =
  inherit SafeHandle
  member Engine : REngine
  member Equals : other:SymbolicExpression -> bool + 1 overload
  member GetAttribute : attributeName:string -> SymbolicExpression
  member GetAttributeNames : unit -> string[]
  member GetHashCode : unit -> int
  member GetMetaObject : parameter:Expression -> DynamicMetaObject
  member IsInvalid : bool
  member IsProtected : bool
  member Preserve : unit -> unit
  member SetAttribute : attributeName:string * value:SymbolicExpression -> unit
  ...

Full name: RDotNet.SymbolicExpression
val name : 'a (requires equality)
val nameLookup : System.Collections.Generic.IDictionary<'a,int> (requires equality)
SymbolicExpression.GetAttribute(attributeName: string) : SymbolicExpression
module Seq

from Microsoft.FSharp.Collections
val mapi : mapping:(int -> 'T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.mapi
val i : int
val n : SymbolicExpression
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
val dict : keyValuePairs:seq<'Key * 'Value> -> System.Collections.Generic.IDictionary<'Key,'Value> (requires equality)

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.dict
(extension) SymbolicExpression.AsList() : GenericVector
val x : float

Full name: Script.x
val f : obj

Full name: Script.f
val minX : SymbolicExpression

Full name: Script.minX
Raw view Test code New version

More information

Link:http://fssnip.net/qW
Posted:9 years ago
Author:Evelina Gabasova
Tags: rprovider , r