如何使用R.Net从.Net调用R函数时传递参数

时间:2015-09-29 05:46:31

标签: .net r r.net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using RDotNet;
using RDotNet.NativeLibrary;

namespace OutlookAddinSendMailToWebService
{
    public partial class ThisAddIn
{
    #region Variables
    private Outlook.Explorer activeExplorer = null;
    #endregion

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        activeExplorer = Application.ActiveExplorer();
    }

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
    }

    //Send Message to Webservice
    public void SendMailToWebservice()
    {
        string mailTo = String.Empty;
        string mailCC = String.Empty;
        string mailSubject = String.Empty;
        string mailBody = String.Empty;
        string mail = String.Empty;

        REngine.SetEnvironmentVariables();
        // There are several options to initialize the engine, but by default the following suffice:
        REngine engine = REngine.GetInstance();

        if (activeExplorer.Selection.Count > 0)
        {
            foreach (Object selectedItem in activeExplorer.Selection)
            {
                if (selectedItem is Outlook.MailItem)
                {
                    Outlook.MailItem mailItem = (selectedItem as Outlook.MailItem);
                    if (mailItem != null)
                    {
                        mailCC = "CC: " + mailItem.CC;
                        mailTo = "To: " + mailItem.To;
                        mailSubject = "Subject: " + mailItem.Subject;
                        mailBody = "Body: " + mailItem.Body;
                        mail = mailTo + '\n' + mailCC + '\n' + mailSubject + '\n' + mailBody;
                        //System.Windows.Forms.MessageBox.Show(mail);

                        engine.Evaluate("load('~/key.RData')");
                        var key = engine.Evaluate("key(mail)").AsCharacter()[0];

                        engine.Evaluate("load('~/add.RData')");
                        var add = engine.Evaluate("trim(key)").AsCharacter()[0];

                        engine.Evaluate("load('~/match.RData')");
                        var mat = engine.Evaluate("match(add,key)").AsCharacter()[0];

                        System.Windows.Forms.MessageBox.Show(mat);
                    }
                }
            }
        }
    }

我在以下代码行中出错。当iam尝试调用关键函数时,找不到错误作为参数邮件。

engine.Evaluate("load('~/key.RData')");
var key=engine.Evaluate("key(mail)").AsCharacter()[0];
engine.Evaluate("load('~/add.RData')");
var add = engine.Evaluate("trim(key)").AsCharacter()[0];
engine.Evaluate("load('~/match.RData')");
var mat = engine.Evaluate("match(add,key)").AsCharacter()[0];

1 个答案:

答案 0 :(得分:1)

R确实不知道你的变量" mail",它是一个.NET变量。

CharacterVector mailVec = engine.CreateCharacterVector(new[] { mail });
engine.SetSymbol("mail", mailVec);

请参阅文档

中的教程basic types with R.NET