unable to connect to R from c# - c#

I am trying to connect to R from c# using the following code. It looks like C# is not reading the R dll files. My R installation directory is this:
C:\Users\R-2-13\R-2.13.0\bin\i386
and I also downloaded and put the R.NET.dll in the same directory. In Visual Studio, I set the reference to R.NET.dll file. When I run the following code, the code goes the the catch section "unable to find the R Installation". Any ideas? Has anybody got this working?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RDotNet;
namespace RNet_Calculator
{
public partial class Form1 : Form
{
// set up basics and create RDotNet instance
// if anticipated install of R is not found, ask the user to find it.
public Form1()
{
InitializeComponent();
bool r_located = false;
while (r_located == false)
{
try
{
REngine.SetDllDirectory(#"C:\Users\R-2-13\R-2.13.0\bin\i386");
REngine.CreateInstance("RDotNet");
r_located = true;
}
catch { MessageBox.Show(#"Unable to find R installation's \bin\i386 folder. Press OK to attempt to locate it.");
MessageBox.Show("error");
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}

This is http://rdotnet.codeplex.com/ (RDotNet) to develop Winform applications. While I know Shiny and all the other Web-like R-tools quite well, the combination of c# and R still is my preferred end-user combinations. Try simple things like disabling buttons with Shiny...
Too bad rdotnet is quite buggy; in the current version, it crashes on R exeptions, even in try-catched ones.
This said: please make absolutely sure that you use version 1.5, not the stupidly called "stable" (=early beta) version on the page. Best download it via NuGet. Also check if you did not mix 32bit R with 64 bit c#.
Using the Helper-functions of 1.5, initialization is:
Helper.SetEnvironmentVariables();
engine = REngine.CreateInstance(EngineName);
engine.Initialize();
# Assuming you want to catch the graphic window, use my RGraphAppHook
# on the rdotnet site http://rdotnet.codeplex.com/workitem/7
cbt = new RGraphAppHook { GraphControl = GraphPanelControl };

Related

File.Exists and SpreadsheetDocument.Open returning File Not Found Exception in UWP Application

Im having an issue with using DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open is not opening a spreadsheet, it returns a file not found exception. The class i'm using has worked many times before, but i've never used it in a UWP project.
I've created a simple example and found that I get the same issue when using File.Exists i've include all the using statements i use if that helps.
Does anyone know why the File.Exists cannot detect the file?
and yes i've triple checked the file does exist on D:!
C# UWP Project created using UWP Template Studio [MainPage.xaml.cs]
using System;
using System.IO;
using System.Data;
using System.Linq;
using System.Diagnostics;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Windows.UI.Xaml;
using UWP_APP.ViewModels;
using Windows.UI.Xaml.Controls;
namespace UWP_APP.Views
{
public sealed partial class MainPage : Page
{
public MainViewModel ViewModel { get; } = new MainViewModel();
public MainPage()
{
InitializeComponent();
string filePath = #"D:\example.xlsm";
if (File.Exists(filePath))
{
int a = 1;
}
else
{
int a = 0;
}
}
Does anyone know why the File.Exists cannot detect the file?
UWP app is running in sandbox, because File.Exists is System.IO api. So it could not work for accessing file except ApplicationData.Current.LocalFolder. If you do want to check if the file exist in the specific path, we suggest you add broadFileSystemAccess capability and enable in the system file access setting. This capability works for APIs in the Windows.Storage namespace.
And using the flolowing method to check if the file exist.
try
{
var file = StorageFile.GetFileFromPathAsync(#"C:\Users\Karan\OneDrive\Desktop\2010.pdf");
if (file != null)
{
isExist = true;
}
}
catch (Exception)
{
isExist = false;
}

Running Python Script/application in Windows Phone 8

I want to run the Skeinforge slicer program written in Python inside my Windows Phone 8 C# application. I have determined that I should probably use IronPython to do this, I have already determined that I can run Skeinforge inside the ipy.exe terminal I got when I installed IronPython. My problem though is that I am struggling to figure out how to host and run a Python script with IronPython inside Windows Phone 8. I have also already managed to get a simple hello world script running inside a Desktop Windows Forms application that transfers the applications console output to the Debug console with the following code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
DebugWriter debugW = new DebugWriter();
Console.SetOut(debugW);
}
private void button1_Click(object sender, EventArgs e)
{
TextWriter tw = new StreamWriter("Test.py");
tw.Write(scriptBox.Text);
tw.Close();
try
{
var ipy = Python.CreateRuntime();
dynamic test = ipy.UseFile("Test.py");
}
catch (Exception ex)
{
Debug.WriteLine("Error: " + ex.Message);
}
}
}
}
And this is the DebugWriter:
class DebugWriter : TextWriter
{
private StringBuilder content = new StringBuilder();
public DebugWriter()
{
Debug.WriteLine("Writing console to debug");
}
public override void Write(char value)
{
base.Write(value);
if (value == '\n')
{
Debug.WriteLine(content.ToString());
content = new StringBuilder();
}
else
{
content.Append(value);
}
}
public override Encoding Encoding
{
get { return System.Text.Encoding.UTF8; }
}
}
I have no idea how to even add the IronPython libraries to my Windows Phone 8 application though as the standard libraries won't import. I have though tried compiling the apparently now defunct Windows Phone 7 libraries with the master source code and I can import these libraries, but I get absolutely no response on the debug terminal when I try to run my hello world script.
Do any of you have any idea how to get this woring in Windows Phone 8, if you know how to do this in Windows 8/Metro/RT then that would also probably work for WP8.
UPDATE:
I have looked at the debug output again and I seem to get this error when trying to use the WP7 libraries to run a hello world script:
A first chance exception of type 'System.NotImplementedException' occurred in Microsoft.Scripting.DLL
Error: The method or operation is not implemented.
I managed to get Skeinforge running on a modified version of IPY. You can get the source for my application here: http://skeinforgewp8.codeplex.com/

The name 'myClassInstance' does not exist in the current context

Environment: c#.net VS 2010
Solution has the following two projects:
A dll with several tested methods I've added.
A test project
The only thing in the test project is a form with following code: (names changed for readability)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using DLL_PROJECT; //Yes I remembered to include the dll project
namespace DLL_PROJECT_Test
{
public partial class frmTest : Form
{
private Class_1 myClass_1; //this comes from the dll - no errors here
private Class_2 myClass_2 = new Class_2(); // no errors here either
public frmTest()
{
InitializeComponent();
//TransparencyKey = BackColor;
this.SetStyle(System.Windows.Forms.ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = System.Drawing.Color.FromArgb(0, System.Drawing.Color.Black);
myDebouncer = new Debouncer(this);
this.SetDragging(true); //THIS EXTENSION COMES FROM THE DLL AND WORKS FINE
this.RoundCorners(40, 80); //AS DOES THIS ONE
myClass_2 = new Class_2();
myClass_2.HoldStartEvent += new Class_2EventHandler(myClass_2_HoldStartEvent);
myClass_2.DragStartEvent += new Class_2EventHandler(myClass_2_DragStartEvent);
}
private void myClass_2_DragStartEvent(Class_2 sender)
{
myClass_2("DragStart") += 1; //THE ONLY ERROR IS HERE AS FOLLOWS
//ERROR: "The name 'myClass_2' does not exist in the current context"
// - Yes, the DLL is included
// - Yes, the project is .Net 4 (not client profile)
// - Yes xxx WRONG xxx, this exact syntax has been tested before on an instance of
// this class, it's just a default parameter.
// xxx should be [] instead of () for the indexer in c#. #VB_Fails
}
void myClass_2_HoldStartEvent(Class_2 sender)
{
this.Close();
}
}
}
This code:
myClass_2("DragStart") += 1;
... is using myClass_2 as if it were either the name of a method or a delegate instance.
Did you actually mean to use the indexer? That would be:
myClass_2["DragStart"] += 1;
What does "DragStart" mean here? Is it actually a property name? Perhaps you want:
myClass_2.DragStart += 1;
I very much doubt that "this exact syntax has been tested before on an instance of this class".
Admittedly the error message doesn't make much sense in this case. I think it's actually more likely that you've got a typo in your real code - a typo which isn't propagated here because you've changed the names. If you could reproduce this in a short but complete program, it would make life a lot simpler.

Im getting exception Typeload what the exception can be?

I added as reference 3 dll's: Google.Apis , Google.Apis.Translate.v2 , System.Runtime.Serialization
In Form1 i have one line:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Translator.translate(new TranslateInput());
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Now the error the exception is on the first line in the class Translator:
The line that throw the error is: var service = new TranslateService { Key = GetApiKey() };
The class code is:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Web;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using Google.Apis.Util;
using Google.Apis.Translate.v2;
using Google.Apis.Translate.v2.Data;
using TranslationsResource = Google.Apis.Translate.v2.Data.TranslationsResource;
public class Translator
{
public static string translate(TranslateInput input)
{
// Create the service.
var service = new TranslateService { Key = GetApiKey() };
string translationResult = "";
// Execute the first translation request.
Console.WriteLine("Translating to '" + input.TargetLanguage + "' ...");
TranslationsListResponse response = service.Translations.List(input.SourceText, input.TargetLanguage).Fetch();
var translations = new List<string>();
foreach (TranslationsResource translation in response.Translations)
{
translationResult = translation.TranslatedText;
}
return translationResult;
}
private static string GetApiKey()
{
return "AIzaSyCjxMe6RKHZzd7xSfSh2pEsBqUdXYm5tA8"; // Enter Your Key
}
}
/// <summary>
/// User input for this example.
/// </summary>
[Description("input")]
public class TranslateInput
{
[Description("text to translate")]
public string SourceText = "Who ate my candy?";
[Description("target language")]
public string TargetLanguage = "fr";
}
The error is:
Could not load type 'Google.Apis.Discovery.FactoryParameterV1_0' from assembly 'Google.Apis, Version=1.1.4497.35846, Culture=neutral, PublicKeyToken=null'.
Tried to google for help and also tried to change the project type to x64 platform but it didnt help. So i put it back on x86
I have windows 7 64bit visual studio c# 2010 pro .net 4.0 profile client.
Cant figure out what is the error ?
This error as reported in the above-posted messages is due to a local copy in the bin\Debug folder of your solution or project. Even though you attempt to clean your solution, such copies will persist to exist.
In order to avoid this to happen, you have to force Visual Studio to refer to the correct DLL by adding reference paths within a project properties. Unfortunately, if you got several projects within your solutions, you will have to set the reference paths for the projects one after another until completed.
Should you wish to know how to setup reference paths follow these simple instructions:
1.Select your project, right-click, then click "Properties";
2.In the project properties, click "Reference Paths";
3.Folder, type or browse to the right location of your DLL, click [Add Folder].
You will need to perform these steps for as many different locations you may have for each of your DLLs. Consider setting an output path under the Build tab of the same project properties, so that you may output your DLLs in the same directory for each of them, thus assuring you to find all the latest builds under the same location, simplifying forward your referencing.
Note this can only be one reason for this error. But it is sure that is has to do something with a wrong copy of the mentioned assembly.

Difficulty with DisqusSharp

I am playing around with DisqusSharp:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WWB.DisqusSharp.Model.DisqusService;
using WWB.DisqusSharp.Infrastructure.Hammock;
namespace DisqusSharpTest
{
class Program
{
static void Main(string[] args)
{
Console.Out.WriteLine("foo");
Console.In.ReadLine();
IDisqusService disqus = new HammockDisqusService("myKey");
IEnumerable<string> names = disqus.GetThreadList("myForumId", new StartLimitArgs { Start = 0, Limit = 5 })
.Payload.Select(disqusThread => disqusThread.Title);
foreach (string name in names)
{
Console.WriteLine(name);
}
}
}
}
After I add references to WWB.DisqusSharp.dll, as well as Hammock.dll and Newtonsoft.Json.dll, this appears to work fine. I use Visual Studio's autocorrect feature and it adds using statements for the WWB classes.
However, when I click "start debugging" or "start without debugging", it complains of a build error, saying that the namespace WWB cannot be found. I then re-add the reference, and it works again, until I hit "start debugging" again.
What am I doing wrong?
I'd be WWB, guess I should add disqussharp to the watch list. Anyhow, I'm not sure the problem you are having, but I'd suggest using the nuget package as a test to make sure things get wired up correctly.

Categories

Resources