【问题标题】:Issue with plotting decimal list with F#'s R Type Provider使用 F# 的 R 类型提供程序绘制十进制列表的问题
【发布时间】:2023-04-05 06:05:03
【问题描述】:

我正在使用 F# 的 CsvProvider 解析 CSV 并成功创建两个列表:

let times = [ for row in airinfo.Rows -> row.Time ]

let passengers = [ for row in airinfo.Rows -> row.AirPassengers ]

times 是一个十进制列表,passengers 是一个整数列表。

最终我试图运行: R.plot (times, passengers)

对于初学者来说,虽然 R.plot passengers 有效,但 R.plot times 无效,这让我相信问题在于绘制十进制列表。

我得到以下异常:

System.Exception: 没有为类型注册转换器 Microsoft.FSharp.Collections.FSharpList`1[[System.Decimal, mscorlib, 版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089]] 或 它的任何基本类型

在 RProvider.RInteropInternal+convertToR@164.Invoke (System.String 消息)[0x00001] 在 :0 中 Microsoft.FSharp.Core.PrintfImpl+StringPrintfEnv1[TResult].Finalize () [0x00012] in <5893d081904cf4daa745038381d09358>:0   at Microsoft.FSharp.Core.PrintfImpl+Final1@224[TState,TResidue,TResult,A].Invoke (Microsoft.FSharp.Core.FSharpFunc2[T,TResult] env, A a) [0x00038] in :0 在 Microsoft.FSharp.Core.OptimizedClosures+Invoke@3253[T2,TResult,T1].Invoke (T2 u) [0x00001] 在 :0 处 RProvider.RInteropInternal.convertToR[inType](RDotNet.REngine 引擎, inType 值)[0x00061] 在 :0 处 RProvider.RInteropInternal.REngine.SetValue(RDotNet.REngine 这个, System.Object 值,Microsoft.FSharp.Core.FSharpOption1[T] symbolName) [0x00001] in <57161c90b86b2a10a7450383901c1657>:0   at RProvider.RInteropInternal.toR (System.Object value) [0x00019] in <57161c90b86b2a10a7450383901c1657>:0   at RProvider.RInterop.passArg@447 (System.Collections.Generic.List1[T] tempSymbols,System.Object arg) [0x00123] 在 :0 在 RProvider.RInterop+argList@468-1.GenerateNext (System.Collections.Generic.IEnumerable1[System.String]& next) [0x000d8] in <57161c90b86b2a10a7450383901c1657>:0   at Microsoft.FSharp.Core.CompilerServices.GeneratedSequenceBase1[T].MoveNextImpl () [0x00017] 在 :0 处 Microsoft.FSharp.Core.CompilerServices.GeneratedSequenceBase1[T].System-Collections-IEnumerator-MoveNext () [0x00001] in <5893d081904cf4daa745038381d09358>:0   at System.Collections.Generic.List1[T]..ctor (System.Collections.Generic.IEnumerable1[T] collection) [0x00077] in <48b95f3df5804531818f80e28ec60191>:0   at Microsoft.FSharp.Collections.SeqModule.ToArray[T] (System.Collections.Generic.IEnumerable1[T] 源)[0x0006a] 在 :0 在 RProvider.RInterop.callFunc (System.String packageName, System.String funcName,System.Collections.Generic.IEnumerable`1[T] argsByName, System.Object[] varArgs) [0x0001d] 在 :0 在 .$FSI_0012.main@ () [0x0002f] 在 :0 在(包装 托管到本机)System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&) 位于 System.Reflection.MonoMethod.Invoke(System.Object 对象, System.Reflection.BindingFlags 调用Attr,System.Reflection.Binder 活页夹,System.Object[] 参数,System.Globalization.CultureInfo 文化)[0x00032] 在 :0

非常感谢任何帮助。

【问题讨论】:

  • 将异常文本重新格式化为块引用
  • R 提供程序相当低级,它不知道某些 F#(如选项)和 .NET 类型。只需将您的小数转换为浮点数或其他可以被它消化的数字格式。您还需要将其装箱。您可以添加您正在调用的完整代码吗?这个例子不能复制。

标签: r f# data-conversion type-providers


【解决方案1】:

您需要将十进制列表转换为 RProvider 知道的数字类型。这是一个适合我的示例:

#load @"..\packages\RProvider\RProvider.fsx"

open System
open RDotNet
open RProvider
open RProvider.graphics

let floatList = [1. ..10.]
let decList = [1M .. 10M ]

R.plot(floatList) // this works
R.plot(decList) // Syystem.exception

System.Exception: 没有为类型注册转换器 Microsoft.FSharp.Collections.FSharpList`1[[System.Decimal, mscorlib, 版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089]] 或其任何基本类型

但是你可以将你的列表转换成另一种类型:

decList 
|> List.map float 
|> R.plot

另外,如果你想使用一个列表作为标签,也许你应该转换成字符串,而不是像我上面做的那样浮动。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多