This project is read-only.

how to call function from r script

May 14, 2015 at 2:52 PM
Edited May 14, 2015 at 2:54 PM
to call a script in r we will execute below code
source("D:\something.R")
but when i try to do in vs2013 with following 2 samples, it did not work, anyone can guide me?
first try
REngine.SetEnvironmentVariables();
            REngine engine = REngine.GetInstance();
            engine.Initialize();
var sentiment_R = engine.Evaluate("source('D:\something.R')");
above will return sentiment_R into list as shown below copy from immediate windows
RObjectType = List
    base: RObjectType = List
    Engine: {RDotNet.REngine}
    IsInvalid: false
    IsProtected: true
    Type: List
instead of return a function it return be a list and i cannot assign value to the function from something.R script

second try is copy out everything from something.R to vs2013 the purpose of source code is to analysis text from twitter source as shown in link here
engine.Evaluate("score.sentiment = function(sentences, pos.words, neg.words, .progress='none')" +
 " { require(plyr) require(stringr)" +

  "scores = laply(sentences, function(sentence, pos.words, neg.words)  { " +
    
    "sentence = gsub('[[:punct:]]', '', sentence)" +
    "sentence = gsub('[[:cntrl:]]', '', sentence)" +
    "sentence = gsub('\\d+', '', sentence)" +
    "sentence = tolower(sentence)" +
    
    "word.list = str_split(sentence, '\\s+')" +
    "words = unlist(word.list)" +
    
    "pos.matches = match(words, pos.words) neg.matches = match(words, neg.words)" + 
    "pos.matches = !is.na(pos.matches) neg.matches = !is.na(neg.matches) score = sum(pos.matches) - sum(neg.matches)" +
    "return(score)}, pos.words, neg.words, .progress=.progress )scores.df = data.frame(score=scores, text=sentences)return(scores.df ) } " ) ;
it return me
 : unexpected symbol
hope someone can provide link or solution to me thank in advance :)
May 31, 2015 at 1:37 AM
First:
source("D:\something.R")
cannot work. You have a single backslash character. Use:
source(@"D:\something.R")
Probably the same thing with the following, as an aside:
"sentence = gsub('\\d+', '', sentence)" +
besides the whole string you build is not a valid function declaration: you have no semi-colon or newline separator between statements.
source(@"D:\something.R")
var sentiment = engine.GetSymbol("score.sentiment").AsFunction();
You'll find sample code on how to invoke functions for instance in https://github.com/jmp75/rdotnet/blob/master/RDotNet.Tests/RFunctionsTest.cs