RDotNet function to add two user defined inputs giving errors - asp.net

I wrote the following controller function in an ASP.NET MVC5 application to return the sum of 2 user defined inputs using the R function sum(). This compiles successfully but does not give any output.
It is giving 500 (Internal Server Error) since the ajax function is failing.
Can anyone please tell me where I am making mistake?
public JsonResult Sum(int? Text1,int? Text2)
{
REngine.SetEnvironmentVariables(); //
REngine engine = REngine.GetInstance();
return Json(engine.Evaluate("function(Text1,Text2)
{sum(Text1,Text2)}").AsFunction(), JsonRequestBehavior.AllowGet);
}

I have solved the problem myself. The correct code snippet for the controller function is as follows:
var mySum = engine.Evaluate("function(x,y){return (x+y)}").AsFunction();
var v = engine.CreateInteger(Text1);
var u = engine.CreateInteger(Text2);
var sum1 = mySum.Invoke(new SymbolicExpression[] { u, v }).AsInteger();
return Json(sum1, JsonRequestBehavior.AllowGet); }
Since many of you messaged me asking for a sample code for the rdotnet application, I have published an example code with explanation on my blog:
wordpress.com/post/pheonix116s.wordpress.com/169
I hope you all may find it useful.

Related

R.Net Displaying list of objects returned by R code in an MVC application

I have an R code which returns a list of numbers using
output <- return(list(add,diff,prod,div))
I need to display these results in an MVC application using R.Net. I am able to display this in case of console application, but not in MVC. The application is failing to display the result, creating the following error:Failed to load resource: the server responded with a status of 500 (Internal Server Error). Can anyone please tell me what is wrong with my following controller code:
public JsonResult OperatoR(int Text1, int Text2)
{
REngine.SetEnvironmentVariables();
REngine engine = REngine.GetInstance();
var v = engine.CreateInteger(Text1);
var u = engine.CreateInteger(Text2);
engine.SetSymbol("u", u);
engine.SetSymbol("v", v);
engine.Evaluate("setwd(\"D:/Users/h224086/Desktop\")");
engine.Evaluate("file<-dget(\"operators.r\")");
engine.Evaluate("exec<-file(u,v)");
var sum = engine.Evaluate("exec").AsIntegerMatrix();
return Json(sum, JsonRequestBehavior.AllowGet);
}
I have solved this issue. You need to modify
var sum = engine.Evaluate("exec").AsIntegerMatrix();
as
IntegerVector group1 = engine.Evaluate("exec").AsInteger();
engine.SetSymbol("group1", group1);

Dart encodeUriComponent Map.keys()

There is an example that I have seen in a number of places for encoding a Map as follows:
#import('dart:uri');
String encodeMap(Map data) {
return Strings.join(data.getKeys().map((k) {
return "${encodeUriComponent(k)}=${encodeUriComponent(data[k])}";
}), "&");
}
I'm running what appears to be the latest Dart editor (version 0.2.9_r 16323)
in the above example, for Dart M2, I believe that data.getKeys() has been changed to data.keys() which I have altered.
However, I get an error when running it in the Editor:
Exception: NoSuchMethodError : method not found: 'call'"
I have 2 questions:
I'm wondering if this above code should still work in M2 with the change indicated (Map.keys())?
I'm wondering if this above code does something different to: JSON.stringify(data);
Any other pointers are welcome.
TIA.
Two changes to do :
import syntax has changed.
getKeys() method became a getter called keys.
A working version :
import 'dart:uri';
String encodeMap(Map data) {
return Strings.join(data.keys.map((k) {
return "${encodeUriComponent(k)}=${encodeUriComponent(data[k])}";
}), "&");
}
The String generated by this encodeMap is quite different from the one generated by JSON.stringify as you can see in the bellow snippet :
main() {
final map = {"a":"b", "c":"d"};
assert(encodeMap(map) == "a=b&c=d");
assert(JSON.stringify(map) == '{"a":"b","c":"d"}');
}

Flex Async Madness - Any way to wait for a rpc.Responder to get a response before going to the next statement?

I feel like I must be doing this wrong. I've got a function that is supposed to construct a query based on an API's description of available fields. Here's what I've got:
var query_fields = getQueryFieldsFor(sobject_name);
// Need query fields for the next statement, which actually does the query
public function getQueryFieldsFor(sObject:String):String{
//helper function to get queryfields for given sobject
var queryFields:String = '';
app.connection.describeSObject(sObject,
new mx.rpc.Responder(
function(result:DescribeSObjectResult):void{
var returnFields:String = '';
for ( var field:Field in result.fields ){
if(field.active){
returnFields.concat(field.name+',')
}
}
returnFields.slice(0, returnFields.length-1); //remove last comma
queryFields = returnFields;
}, function(error):void{
Alert.show('error in getQueryFieldsFor function');
})
);
return queryFields;
}
I know this doesn't work, and I think I understand why. However, I keep running into this type of issue and I believe I'm just thinking about it/designing it wrong. So what's a better pattern here? Would really appreciate any insight on this. Many thanks in advance.
It would be better to externalize your functions and execute your next line of code after the fact:
public function getQueryFieldsFor(sObject:String):String
{
var responder:Responder = new Responder( onResult, onFault);
app.connection.describeSObject(sObject, responder);
}
private function onResult(result:DescribeSObjectResult):void
{
var returnFields:String = '';
for ( var field:Field in result.fields ){
if(field.active){
returnFields.concat(field.name+',')
}
}
returnFields.slice(0, returnFields.length-1); //remove last comma
queryFields = returnFields;
}
Your main problem though is not the code, but a lack of thinking asynchronously. You cannot have a function called "getQueryFields" that will return it instantly. What you want to do is think in the request/response way. You're trying to get some data, a request is made to a service, gets the data back, updates a property which is then binded to a view which gets redrawn. This is the proper way to do any webapp.
It might be beneficial for you to also look at application frameworks like RobotLegs and Parsley since it helps you manage these situations. Parsley also has a task library which lets you perform several asynchronous task one after another.

Is it possible to find the function and/or line number that caused an error in ActionScript 3.0 without using debug mode?

I'm currently trying to implement an automated bug reporter for a Flex application, and would like to return error messages to a server along with the function/line number that caused the error. Essentially, I'm trying to get the getStackTrace() information without going into debug mode, because most users of the app aren't likely to have the debug version of flash player.
My current method is using the UncaughtErrorEvent handler to catch errors that occur within the app, but the error message only returns the type of error that has occurred, and not the location (which means it's useless). I have tried implementing getStackTrace() myself using a function name-grabber such as
private function getFunctionName (callee:Function, parent:Object):String {
for each ( var m:XML in describeType(parent)..method) {
if ( this[m.#name] == callee) return m.#name;
}
return "private function!";
}
but that will only work because of arguments.callee, and so won't go through multiple levels of function calls (it would never get above my error event listener).
So! Anyone have any ideas on how to get informative error messages through the global
error event handler?
EDIT: There seems to be some misunderstanding. I'm explicitly avoiding getStackTrace() because it returns 'null' when not in debug mode. Any solution that uses this function is what I'm specifically trying to avoid.
Just noticed the part about "I don't want to use debug." Well, that's not an option, as the non-debug version of Flash does not have any concept of a stack trace at all. Sucks, don't it?
Not relevant but still cool.
The rest is just for with the debug player.
This is part of my personal debug class (strangely enough, it is added to every single project I work on). It returns a String which represents the index in the stack passed -- class and method name. Once you have those, line number is trivial.
/**
* Returns the function name of whatever called this function (and whatever called that)...
*/
public static function getCaller( index:int = 0 ):String
{
try
{
throw new Error('pass');
}
catch (e:Error)
{
var arr:Array = String(e.getStackTrace()).split("\t");
var value:String = arr[3 + index];
// This pattern matches a standard function.
var re:RegExp = /^at (.*?)\/(.*?)\(\)/ ;
var owner:Array = re.exec(value);
try
{
var cref:Array = owner[1].split('::');
return cref[ 1 ] + "." + owner[2];
}
catch( e:Error )
{
try
{
re = /^at (.*?)\(\)/; // constructor.
owner = re.exec(value);
var tmp:Array = owner[1].split('::');
var cName:String = tmp.join('.');
return cName;
}
catch( error:Error )
{
}
}
}
return "No caller could be found.";
}
As a side note: this is not set up properly to handle an event model -- sometimes events present themselves as either not having callers or as some very weird alternate syntax.
You don't have to throw an error to get the stack trace.
var myError:Error = new Error();
var theStack:String = myError.getStackTrace();
good reference on the Error class
[EDIT]
Nope after reading my own reference getStackTrace() is only available in debug versions of the flash player.
So it looks like you are stuck with what you are doing now.

How to handle calls to multiple functions in the same web service?

This is a super newbie question on Flex. While I am a seasoned programmer, this is my first ever Flex application; please bear with me for my Flex codes.
I have a web service written in ColdFusion. In this web service, there are two functions; one is to return all the questions in a quiz and the other one is to return all the answer selections to the questions in a quiz.
[Bindable]
private var questionArray:ArrayCollection;
private var cfquiz:RemoteObject;
private function loadQuestions():void {
currentQuestionCounter = 0;
btnPrev.enabled = false;
btnNext.enabled = false;
cfquiz = new RemoteObject("ColdFusion");
cfquiz.source = "CFCertExam.cfquiz";
cfquiz.addEventListener(ResultEvent.RESULT, resultHandler);
}
private function resultHandler(event:ResultEvent):void {
questionArray = event.result as ArrayCollection;
txt1Questions.htmlText = questionArray.getItemAt(currentQuestionCounter).Question_Text;
btnNext.enabled = true;
}
I have the codes above. loadQuestions is called at creationComplete to retrieve the questions. Things are working fine. What I want to do is to call another function within the same web service, returnAnswers, to return the answer options for a question. Since I have cfquiz associated to the web service already, I was using cfquiz to call returnAnswers. However, there is an event listener associated to cfquiz already, resultHandler is being called when returnAnswers comes back with the results.
My questions are, first, is it possible to check which function returns the results within resultHandler? If so, how? And second, what is the best way to handle calls to multiple functions within the same web service?
Thanks in advance,
Monte
Yes you can. You need to specify a handler function for each method which in turn calls a different webservice.
The better way to do this is using AsyncToken and AsyncResponder instead of addEventListener, as following code.
tokenA = cfquiz.methodA();
tokenA.addResponder(new AsyncResponder(onResultForMethodA, onFaultMethodA));
tokenB = cfquiz.methodA();
tokenB.addResponder(new AsyncResponder(onResultForMethodB, onFaultMethodB));
tokenC = cfquiz.methodA();
tokenC.addResponder(new AsyncResponder(onResultForMethodC, onFaultMethodC));
or
tokenA = cfquiz.methodA();
var responderA:IResponder = new AsyncResponder(onResult, onFault, "methodA");
tokenB = cfquiz.methodB();
var responderB:IResponder = new AsyncResponder(onResult, onFault, "methodB");
tokenA.addResponder(responderA);
tokenB.addResponder(responderB);
private function onResult(evt:ResultEvent, token:Object):void {
if(token == "methodA" ) {
//logic for methodA
}
if(token == "methodB" ) {
//logic for methodB
}
}
I'm a little confused as I can't see where you are calling the actual web service function, for example from these examples, I am expecting to see:
cfquiz = new RemoteObject("ColdFusion");
cfquiz.source = "CFCertExam.cfquiz";
cfquiz.addEventListener(ResultEvent.RESULT, resultHandler);
cfquiz.myCFCFunctionCall(); /* where is this? */
Anyway, AFAIK you can create a new instance of the remote object and set that up to have it's own event listener.
Hope that helps.

Resources