RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 760021
Accepted
Andrey K.
Andrey K.
Asked:2020-12-18 21:32:41 +0000 UTC2020-12-18 21:32:41 +0000 UTC 2020-12-18 21:32:41 +0000 UTC

R 与 C# 和 WPF 的集成

  • 772

我对R编程语言不是很熟悉,有没有可能做一个这样的系统?

假设有一个用 R 编写的模块。对于它,应该沿途形成一组输入数据。在什么情况下,这些数据是通过 C# 形成的。接下来,调用这个 R 模块,它会生成一个报告,例如一些数据。此数据显示在 WPF GUI 上,即 WPF 在其某些控件中显示 R 标记或某些 R 图形。那么有可能吗?

第二个问题,同时。是否可以使用 C# 使 R 输入动态更新,以便 WPF 界面上的图形动态更新?

谢谢。


PS:我会尝试开始比赛。理想情况下,我会对解决方案的简短或详细描述感到满意,而不仅仅是解释为什么是或为什么不是。

Upd:我在很久以后添加了我自己关于闪亮的答案,所以纯粹通过投票来判断是不客观的。你甚至可以看看我的回答。

c#
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. Danny
    2020-12-25T22:45:06Z2020-12-25T22:45:06Z

    我建议使用此处概述的想法,即:

    • 安装 R.NET
    • 例如,使用评估在 C# 代码中使用 R,如上面的链接所示:

      using System;
      using RDotNet;
      
      namespace RCalculator
      {
          class Program
          {
          static void Main(string[] args)
              {
                  string result;
                  string input;
                  REngine engine;
      
                  //запустить R engine            
                  REngine.SetEnvironmentVariables();
                  engine = REngine.GetInstance();
                  engine.Initialize();
      
                  //ввод
                  Console.WriteLine("Введите выражение");
                  input = Console.ReadLine();
      
                  //вычисление
                  CharacterVector vector = engine.Evaluate(input).AsCharacter();
                  result = vector[0];
      
                  //закрыть R
                  engine.Dispose();
      
                  //вывод
                  Console.WriteLine("");
                  Console.WriteLine("Result: '{0}'", result);
                  Console.WriteLine("Нажмите любую клавишу...");
                  Console.ReadKey();
              }
          }
      }
      

    您收到 C# 中的输出,也就是说,您不受其进一步交付方式的限制。因此,您需要像在 C# 中通常那样访问 WPF。

    • 4
  2. Best Answer
    Andrey K.
    2020-09-16T01:40:07Z2020-09-16T01:40:07Z

    最后,对我有用的解决方案是:

    可接受的解决方案:闪亮


    对不起,我将仅简要描述解决方案(很快):

    1) R 便携式

    2)运行应用程序:

    .libPaths( c( .libPaths(), "C:\\Users\\Andrey\\AppData\\Local\\Temp\\zzzz\\xxxx\\Server\\Reports\\pckgs") )
    
    .libPaths()
    
    if(!require(shiny)){
        install.packages("shiny", repos="http://cran.rstudio.com/", destdir="C:\\Users\\Andrey\\AppData\\Local\\Temp\\zzzz\\xxxx\\Server\\Reports\\pckgs")
        library(shiny)
    }
    
    if(!require(parallel)){
        install.packages("parallel", repos="http://cran.rstudio.com/", destdir="C:\\Users\\Andrey\\AppData\\Local\\Temp\\zzzz\\xxxx\\Server\\Reports\\pckgs")
        library(parallel)
    }
    
    options(echo=TRUE) # if you want see commands in output file
    args <- commandArgs(trailingOnly = TRUE)
    print(args)
    
    folder_address <- args[1]
    port <- as.numeric(args[2])
    
    x <- system("ipconfig", intern=TRUE)
    z <- x[grep("IPv4", x)]
    ip <- gsub(".*? ([[:digit:]])", "\\1", z) 
    # print(paste0("the Shiny Web application runs on: http://", ip, ":1234"))
    
    runApp(folder_address, launch.browser=FALSE, port = port, host = ip)
    

    3)要理解这个概念,一个批处理文件就足够了thebatnik.bat:

    C:\Users\Andrey\Desktop\RPlay\Shiny\R-Portable\App\R-Portable\bin\RScript.exe "C:\Users\Andrey\AppData\Local\Temp\zzzz\xxxx\Server\Reports\runapp.r" "C:\Users\Andrey\AppData\Local\Temp\zzzz\xxxx\Server\Reports\rep1"  1234 
    PAUSE
    

    这里 1234 是端口号(端口号)

    4) 之前

    library(shiny)
    

    4)报告的第一个组件是服务器组件。(服务器部分)

    # Define server logic required to draw a histogram ----
    server <- function(input, output) {
    
    # Histogram of the Old Faithful Geyser Data ----
    # with requested number of bins
    # This expression that generates a histogram is wrapped in a call
    # to renderPlot to indicate that:
    #
    # 1. It is "reactive" and therefore should be automatically
    #    re-executed when inputs (input$bins) change
    # 2. Its output type is a plot
    output$distPlot <- renderPlot({
    
    x    <- faithful$waiting
    bins <- seq(min(x), max(x), length.out = input$bins + 1)
    
    hist(x, breaks = bins, col = "#75AADB", border = "white",
         xlab = "Waiting time to next eruption (in mins)",
         main = "Histogram of waiting times")
    
    })
    
    }
    

    5) UI部分R+Shiny(UI部分):

    # Define UI for app that draws a histogram ----
    ui <- fluidPage(
    
    # App title ----
    titlePanel("Dynaic report example"),
    
    # Sidebar layout with input and output definitions ----
    sidebarLayout(
    
    # Sidebar panel for inputs ----
    sidebarPanel(
    
      # Input: Slider for the number of bins ----
      sliderInput(inputId = "bins",
                  label = "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30)
    
    ),
    
    # Main panel for displaying outputs ----
    mainPanel(
    
      # Output: Histogram ----
      plotOutput(outputId = "distPlot")
    
    )
    

    ) )

    5) 之后

    # Create Shiny app ----
    shinyApp(ui = ui, server = server)
    

    项目 4-5 可以在 rep1 文件夹中的一个或多个文件中。

    (4-5是rep1文件夹中的文件)

    6)在这个例子中托管在

    IPv4-адрес. . . . . . . . . . . . : 192.168.2.128
    

    ,并且要在一个端口上进行多个报告,您可能需要(如果我没记错的话)(如果我是对的,您可以使用以下工具在一个端口上运行多个报告):

    nginx

    7)在这个阶段,一个报告已经准备好了,在运行批处理文件之后,可以从浏览器(在浏览器中)打开它:

    如果在浏览器中,结果是这样的

    8)但是有一个wpf控件(顺便说一句,还有一个winform的)(wpf控件):

    <WebBrowser Source="http://192.168.2.128:1234"></WebBrowser>
    

    用更好的浏览器(如 Chrome)替换 .NET WebBrowser 控件?

    //winforms - на самостоятельную работу, homework
    

    还有:

    Process.Start("thebatnik.bat");
    

    9)可以使用 R 从数据库中读取数据。此外,R 可以接受其他格式的数据,例如,来自文件或可能来自其他来源的数据。例如,可以使用 c# 准备数据文件。关于更新数据源是一个有趣的问题,但我没有解决这个问题,我在最初的阶段就停止了,在概念验证阶段。我认为更新数据源的问题是可以解决的。但这就是现在的全部内容,因为不幸的是,我现在不必考虑这个计划的复杂性。(抱歉,不能说更新源,但可能有可能;可以通过 c# 准备带有数据的文件;或其他读取数据的解决方案,如 R 工具以使用数据库)

    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    Python 3.6 - 安装 MySQL (Windows)

    • 1 个回答
  • Marko Smith

    C++ 编写程序“计算单个岛屿”。填充一个二维数组 12x12 0 和 1

    • 2 个回答
  • Marko Smith

    返回指针的函数

    • 1 个回答
  • Marko Smith

    我使用 django 管理面板添加图像,但它没有显示

    • 1 个回答
  • Marko Smith

    这些条目是什么意思,它们的完整等效项是什么样的

    • 2 个回答
  • Marko Smith

    浏览器仍然缓存文件数据

    • 1 个回答
  • Marko Smith

    在 Excel VBA 中激活工作表的问题

    • 3 个回答
  • Marko Smith

    为什么内置类型中包含复数而小数不包含?

    • 2 个回答
  • Marko Smith

    获得唯一途径

    • 3 个回答
  • Marko Smith

    告诉我一个像幻灯片一样创建滚动的库

    • 1 个回答
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Алексей Шиманский 如何以及通过什么方式来查找 Javascript 代码中的错误? 2020-08-03 00:21:37 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5