[英]attempting to connect to R from C#
我正在嘗試使用C#連接到R。 我安裝了R.Net並引用了我的項目。 這是我第一次嘗試C#。 有什么想法我做錯了嗎?
這是示例C#代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RDotNet;
namespace RNet_Calculator
{
public partial class Form1 : Form
{
// set up basics and create RDotNet instance
// if anticipated install of R is not found, ask the user to find it.
public Form1()
{
InitializeComponent();
string dlldir = @"C:\Users\R\R-2.15.2\bin\x64";
bool r_located = false;
while (r_located == false)
{
try
{
REngine.SetDllDirectory(dlldir);
REngine.CreateInstance("RDotNet");
r_located = true;
}
catch
{
MessageBox.Show(@"Unable to find R installation's \bin\i386 folder.
Press OK to attempt to locate it.");
}
}
}
}
}
這是Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace Form1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
實際上,這與R無關。您可能在一個地方而不是另一個地方覆蓋了名稱空間。 你有代碼
namespace RNet_Calculator
在您的表單代碼中。 如果您打開Form1.designer.cs
您可能會看到
namespace Form1
只需將名稱空間從Form1
更改為RNet_Calculator
,您的錯誤就會消失。
編輯
為了響應您的編輯,您應該將單個RNET_Calculator
命名空間改回Form1
或者也應該(但不必)更改Program.cs文件(以及項目中的其他文件)中的Form1
命名空間。 這樣做意味着您還應該在項目屬性中更改名稱空間。 右鍵單擊您的項目,選擇“屬性”,然后在“應用程序”選項卡(應該是第一個打開的選項卡)中,將“默認名稱空間”文本框更改為RNET_Calculator
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.