यह प्रश्न लगभग 5 वर्ष पुराना है और इसके लिए कुछ उत्तर here जैसे उपलब्ध हैं। मैं इसे बहुत ही सरल R
स्क्रिप्ट के साथ जाउंगा।
यह इस link
इस सरल उदाहरण में साथ शुरू करने के लिए अच्छा है, मैं आर करने के लिए 3 से पारित, 5 के साथ जोड़ सकते हैं और परिणाम (8) वापस मिलता है।
कदम
नीचे की तरह कुछ name.r
के रूप में एक पाठ फ़ाइल बनाएँ, अपने आर कोड के साथ,। मैं rcodeTest.r
library(RODBC) # you can write the results to a database by using this library
args = commandArgs(trailingOnly = TRUE) # allows R to get parameters
cat(as.numeric(args[1])+5)# converts 3 to a number (numeric)
फिर एक सी # वर्ग बनाने के रूप में यह नाम दिया है नीचे की तरह भी here पर उपलब्ध है (यह कुछ भी कहते हैं, मैं इसे RScriptRunner
कहा जाता है),। यह एक साधारण वर्ग जो सिर्फ एक प्रक्रिया (एक exe फ़ाइल)
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for RScriptRunner
/// </summary>
public class RScriptRunner
{
public RScriptRunner()
{
//
// TODO: Add constructor logic here
//
}
// Runs an R script from a file using Rscript.exe.
///
/// Example:
///
/// RScriptRunner.RunFromCmd(curDirectory + @"\ImageClustering.r", "rscript.exe", curDirectory.Replace('\\','/'));
///
/// Getting args passed from C# using R:
///
/// args = commandArgs(trailingOnly = TRUE)
/// print(args[1]);
///
///
/// rCodeFilePath - File where your R code is located.
/// rScriptExecutablePath - Usually only requires "rscript.exe"
/// args - Multiple R args can be seperated by spaces.
/// Returns - a string with the R responses.
public static string RunFromCmd(string rCodeFilePath, string rScriptExecutablePath, string args)
{
string file = rCodeFilePath;
string result = string.Empty;
try
{
var info = new ProcessStartInfo();
info.FileName = rScriptExecutablePath;
info.WorkingDirectory = Path.GetDirectoryName(rScriptExecutablePath);
info.Arguments = rCodeFilePath + " " + args;
info.RedirectStandardInput = false;
info.RedirectStandardOutput = true;
info.UseShellExecute = false;
info.CreateNoWindow = true;
using (var proc = new Process())
{
proc.StartInfo = info;
proc.Start();
result = proc.StandardOutput.ReadToEnd();
}
return result;
}
catch (Exception ex)
{
throw new Exception("R Script failed: " + result, ex);
}
}
}
फिर फोन और तरह
result = RScriptRunner.RunFromCmd(path + @"\rcodeTest.r", @"D:\Programms\R-3.3.3\bin\rscript.exe", "3");
rscript.exe
अपने आर निर्देशिका में स्थित है पैरामीटर भेजने कहता है, और path
का स्थान है आपके आर स्क्रिप्ट (rcodeTest.r
)
अब आप नीचे दिखाए गए आउटपुट के रूप में परिणाम 8 = 5 + 3 प्राप्त कर सकते हैं।
स्रोत
2017-05-22 21:45:15
मेरा प्रतिलेख शीर्षलेख और 3 पंक्तियों के साथ डेटा.फ्रेम लौटाता है। मैं इसे कैसे पढ़ूं? –
क्या आपके पास डेटाबेस का उपयोग करने के लिए कोई प्रतिबंध है? यदि नहीं, तो तालिका बनाएं और इस तालिका (मानों) में मानों को संग्रहित करें, और तालिका का नाम अपनी स्क्रिप्ट (सी #) पर पास करें। या बस एक पूर्वनिर्धारित तालिका और स्कीमा का उपयोग करें। इसके लिए आपको आर में आरओडीबीसी पैकेज का उपयोग करने की आवश्यकता हो सकती है। –
मुझे अब पढ़ने की अनुमति है, लेकिन मुझे लिखने की अनुमति मांगनी है। लेकिन क्या आउटपुट को पढ़ने का कोई तरीका है? डीबी में इसे बचाए बिना? या पूरी तालिका को स्ट्रिंग के रूप में तैयार करें और इसे स्ट्रिंग के रूप में भेजें? मै खो गया हूँ। मैंने प्लंबर के साथ एक बाकी एपीआई भी बनाया है, यह मुझे अपेक्षित JSON परिणाम लौटा रहा है, लेकिन यह एकल धागा है, इसलिए एक समय में केवल एक प्रक्रिया की अनुमति है। –