public async Task List() { await _session.ExecuteAsync("x <- c(1:10)"); var ol = await _session.GetListAsync <int>("x"); ol.Count.Should().Be(10); ol.Should().ContainInOrder(new int[] { 1, 2, 3, 4, 5, 6, 7, 9, 10 }); }
/// <summary> /// Creates R data frame from .NET <see cref="DataFrame"/> /// </summary> /// <param name="session">R session</param> /// <param name="name">Name of the variable to assign the R list to</param> /// <param name="df">.NET data frame</param> /// <param name="cancellationToken">Cancellation token</param> /// <exception cref="ArgumentNullException" /> /// <exception cref="ArgumentException" /> /// <exception cref="REvaluationException" /> /// <exception cref="OperationCanceledException" /> /// <exception cref="RHostDisconnectedException" /> public static async Task CreateDataFrameAsync(this IRHostSession session, string name, DataFrame df, CancellationToken cancellationToken = default(CancellationToken)) { Check.ArgumentNull(nameof(session), session); Check.ArgumentNull(nameof(name), name); Check.ArgumentNull(nameof(df), df); await session.ExecuteAsync(Invariant($"{name.ToRStringLiteral()} <- {df.ToRDataFrameConstructor()}")); if (df.RowNames != null && df.RowNames.Count > 0) { await session.ExecuteAsync(Invariant($"rownames({name}) <- {df.RowNames.ToRListConstructor()}")); } if (df.ColumnNames != null && df.ColumnNames.Count > 0) { await session.ExecuteAsync(Invariant($"colnames({name}) <- {df.ColumnNames.ToRListConstructor()}")); } }
/// <summary> /// Creates list of objects in R from list of .NET objects /// </summary> /// <typeparam name="T">.NET object type</typeparam> /// <param name="session">R session</param> /// <param name="name">Name of the variable to assign the R list to</param> /// <param name="list">List of .NET objects</param> /// <param name="cancellationToken">Cancellation token</param> /// <exception cref="ArgumentNullException" /> /// <exception cref="ArgumentException" /> /// <exception cref="REvaluationException" /> /// <exception cref="OperationCanceledException" /> /// <exception cref="RHostDisconnectedException" /> public static Task CreateListAsync <T>(this IRHostSession session, string name, IEnumerable <T> list, CancellationToken cancellationToken = default(CancellationToken)) { Check.ArgumentNull(nameof(session), session); Check.ArgumentNull(nameof(name), name); Check.ArgumentNull(nameof(list), list); var rlist = list.ToRListConstructor(); return(session.ExecuteAsync(Invariant($"{name.ToRStringLiteral()} <- {rlist}"))); }
public async Task CallMyFunction(double x, double y) { await task; if (!scriptExecuted) { await session.ExecuteAsync(File.ReadAllText(scriptPath)); // Можно попробовать так //await session.ExecuteAsync("source('full\\path\\to\\Script.R')"); scriptExecuted = true; } var res = await session.ExecuteAndOutputAsync($"my.f({x}, {y})"); Console.WriteLine(res.Output); }
public async Task ExecuteCommandAsync(string command) { await rHostSession.ExecuteAsync(command); }