Quantcast
Channel: R.NET
Viewing all articles
Browse latest Browse all 1634

New Post: how to call function from r script

$
0
0
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 :)

Viewing all articles
Browse latest Browse all 1634

Trending Articles