When Connecting R with RdotNet It says Set Environment Variable 'PATH' - c#

When I Run the Warning
RDotNet.NativeLibrary.UnmanagedDll.SetDllDirectory(string) is obsolete Set environment variable 'PATH' instead.
I don't know how to do that. how to set variable path please help quickly please.
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 WindowsFormsApplication7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
string dlldir = #"C:\Program Files\R\R-3.1.1\bin\x64";
REngine.SetDllDirectory(dlldir);
REngine.CreateInstance("RDotNet");
}
private void button1_Click(object sender, EventArgs e)
{
REngine engine = REngine.GetInstanceFromID("RDotNet");
try
{
// import csv file
engine.Evaluate("dataset<-read.table(file.choose(), header=TRUE, sep = ',')");
// retrieve the data frame
DataFrame dataset = engine.Evaluate("dataset").AsDataFrame();
for (int i = 0; i < dataset.ColumnCount; ++i)
{
dataGridView1.ColumnCount++;
dataGridView1.Columns[i].Name = dataset.ColumnNames[i];
}
for (int i = 0; i < dataset.RowCount; ++i)
{
dataGridView1.RowCount++;
dataGridView1.Rows[i].HeaderCell.Value = dataset.RowNames[i];
for (int k = 0; k < dataset.ColumnCount; ++k)
{
dataGridView1[k, i].Value = dataset[i,k];
}
}
}
catch
{
MessageBox.Show(#"Equation error.");
}
}
}
}

If you want to know how can you set or edit environment variables in windows, you can use this link:
Set Environment Variable
PATH includes several addresses, and maybe you should add your dlldir address to this variable.

You are using an outdated version of R.NET; see the online documentation for instruction and sample code on how to install and use the current API (version 1.5.15 or above)

Related

Edge.js- -Access to DLL file doesn't work

I want to access the OpenHardwareMonitorLib.dll file with node.js using the edge.js module and monitor the computer's CPU and GPU temperatures. I created a Node.js file in Visual Studio Code and added the DLL file to the references.
var edge = require('edge-js');
var add7 = edge.func({
source: function() {/*
using System;
using System.Data;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenHardwareMonitor.Hardware;
public class Startup
{
public class UpdateVisitor : IVisitor
{
public void VisitComputer(IComputer computer)
{
computer.Traverse(this);
}
public void VisitHardware(IHardware hardware)
{
hardware.Update();
foreach (IHardware subHardware in hardware.SubHardware) subHardware.Accept(this);
}
public void VisitSensor(ISensor sensor) { }
public void VisitParameter(IParameter parameter) { }
}
public async Task Invoke()
{
while (true)
{
await Task.Run(() => GetSystemInfo());
}
}
static async void GetSystemInfo()
{
UpdateVisitor updateVisitor = new UpdateVisitor();
Computer computer = new Computer();
computer.Open();
computer.CPUEnabled = true;
computer.Accept(updateVisitor);
for (int i = 0; i < computer.Hardware.Length; i++)
{
if (computer.Hardware[i].HardwareType == HardwareType.CPU)
{
for (int j = 0; j < computer.Hardware[i].Sensors.Length; j++)
{
if (computer.Hardware[i].Sensors[j].SensorType == SensorType.Temperature)
Console.WriteLine(computer.Hardware[i].Sensors[j].Name + ":" + computer.Hardware[i].Sensors[j].Value.ToString() + "\r");
}
}
}
computer.Close();
}
}
*/},
references: [ 'System.Data.dll','OpenHardwareMonitorLib.dll']
});
I'm pretty sure the C# code works. I created an empty C# console application in Visual Studio and added the DLL file to the references. When I ran this C# code, I reached CPU temperatures without any problems. But in Visual Studio when I run it in my Nodejs project using edge-js, I get the following output;
'app.js' exited with code -1 (0xffffffff)
When I run it in Visual Studio Code, I can't get any output. I don't fully understand why it occurs. I would be glad if you help.
Visual Studio Code Output;

c#: Random Choose from web and ignore blank lines

I'm fresh in c# forms i have an important question.
I want to make randomly text writer one line from url "list"
Let Me explain, i wanna to get a random line from pastebin, and rename a label with the random line chosed, and ignore blank lines.
This is a error:
Severity Code Description Project File Line Suppression State
Error CS0103 The name 'randomline' does not exist in the current context SNPCorp C:\Users\GhostStru\Desktop\SNP\Tester\Generator.cs 45 Active
i have that 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.Net.Sockets;
using System.Net.Security;
using System.Threading;
using System.Net.Http;
using System.Web;
using System.Net.WebSockets;
using System.Net;
using System.IO;
namespace Tester
{
public partial class Generator : Form
{
public Generator()
{
InitializeComponent();
}
private void Generator_Load(object sender, EventArgs e)
{
WebClient web = new WebClient();
string text = web.DownloadString("https://pastebin.com/raw/test");
var lines = text.Split(new string[] { "." }, StringSplitOptions.None);
Random rnd = new Random();
int randomLineIndex = rnd.Next(0, lines.Count());
var randomline = lines[randomLineIndex];
}
private void button3_Click(object sender, EventArgs e)
{
label11 = (randomline);
}
}
}
Your question has some things that aren't clear but I'll try with it. First, I define a method to get a random line:
private static string GetRandomLine()
{
using (var web = new WebClient())
{
var html = web.DownloadString("https://pastebin.com/raw/test");
var lines = html.Split(new string[] { "." }, StringSplitOptions.None);
var rnd = new Random();
int index = rnd.Next(0, lines.Length);
return lines[index];
}
}
Then, you can use that method to set Text property of your label. You must set the Text property, not the label variable
private void button3_Click(object sender, EventArgs e)
{
label11.Text = GetRandomLine();
}
The DownloadString is not working for me. Is that the Url? In any case, if you use a lot this method, is interesting save into a variable the HTML and use it some time (one hour... depending of your needs) as a cache instead of make a request each time. WebClient implements IDisposable so you must invoke Dispose after use it or allow to "using" do it automatically.
I'll try to answer your question even though you haven't explained some points clearly.
Create a function to return the random line
In button3_Click event, call a function that will return a random line, then set the text value of button3 to that returned value.
public static string GetRanLine()
{
using (var WC = new WebClient())
{
var Response = WC.DownloadString("https://pastebin.com/raw/test");
var AllLines = Response.Split(new string[] { "."}, StringSplitOptions.None);
var RanLine = AllLines[new Random().Next(0, AllLines.Length - 1)];
return RanLine;
}
}
private void button3_Click(object sender, EventArgs e)
{
label11 = GetRanLine();
}

Beginner Kinect Programming - "'args' does not exsist in the current context"?

Ok so I started writing an app following the how-to-kinect series from Microsoft and everything was going well until I wrote this:
using (InfraredFrame IRFrame = args.FrameReference.AcquiredFrame())
{
}
For some reason it keeps saying args does not exist in the current context and I have no idea why..here is my full code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Media.Imaging;
using WindowsPreview.Kinect;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace KinectApp1
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
this.Loaded += MainPage_Loaded;
}
/*Getting the Kinect Sensor*/
KinectSensor Sensor;
/*Getting the Infared Reader*/
InfraredFrameReader IRReader;
/*This is IR Data Form*/
ushort[] IRData;
/*Converting the Data (Buffer) */
byte[] IRDataConverted;
/*Writing the Bitmap Image Described in XAML*/
WriteableBitmap IRBitmap;
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
/*Get the sensor in the loaded frame*/
Sensor = KinectSensor.GetDefault();
/*Get the reader from the Source on the Sensor*/
IRReader = Sensor.InfraredFrameSource.OpenReader();
/*Frame Description for the Infrared and see how big they are*/
FrameDescription FD = Sensor.InfraredFrameSource.FrameDescription;
IRData = new ushort[FD.LengthInPixels];
IRDataConverted = new byte[FD.LengthInPixels * 4];
IRBitmap = new WriteableBitmap(FD.Width, FD.Height);
Image.Source = IRBitmap;
/*Start Sensor*/
Sensor.Open();
/*Subscribe to the event off the Reader*/
IRReader.FrameArrived += IRReader_FrameArrived;
}
void IRReader_FrameArrived(InfraredFrameReader sender, InfraredFrameArrivedEventArgs e)
{
using (InfraredFrame IRFrame = args.FrameReference.AcquiredFrame())
{
if (IRFrame != null)
{
IRFrame.CopyFrameDataToArray(IRData);
for (int i = 0; i < IRData.Length; i++)
{
byte intensity = (byte)(IRData[i]>>8);
IRDataConverted[i*4] = intensity;
IRDataConverted[i*4 + 1] = intensity;
IRDataConverted[i*4 + 2] = intensity;
IRDataConverted[i*4 + 3] = 255;
}
IRDataConverted.CopyTo(IRBitmap.PixelBuffer);
IRBitmap.Invalidate();
}
}
}
}
}
Can anyone kindly explain why this has happened? I am pretty confused,
Thanks in Advance.
P.S this is the video I was following:
http://www.microsoft.com/en-us/kinectforwindows/develop/how-to-videos.aspx
Well it's been answered so I'm not sure what else to do apart from answer it:
As Preston Guillot said in the comments:
"There is no variable named args in scope in the IRReader_FrameArrived method. You have a parameter of type InfraredFrameArrivedEventArgs named e that I'm guessing you meant to use"

How can I use open hardware monitor source code in c# ? I tried anything doesn't work

I have this code in Form1:
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 OpenHardwareMonitor.Hardware.HDD;
using OpenHardwareMonitor;
namespace OpenHardwareMonitor
{
public partial class Form1 : Form
{
OpenHardwareMonitor.Hardware.SensorValue sv;
OpenHardwareMonitor.Hardware.ISensor ii;
public Form1()
{
InitializeComponent();
string y = ii.Name;
sv = new Hardware.SensorValue();
DateTime dt = sv.Time;
float t = sv.Value;
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
ii variable is null I don't know how to make an instance for it.
The other two variables in the constructor return 0 nothing. If I'm not using the ii variable the other two don't throw an error but don't return any values.
I'm using the openhardwaremonitor dll from http://code.google.com/p/open-hardware-monitor/downloads/detail?name=openhardwaremonitor-v0.4.0-beta.zip&can=2&q=
The c# dll is coming with the program it self.
So I added as reference the dll but I don't know how to make the code.
Could someone build for me just an example of the code according to my code here ?
I tried to look in the openhwardwaremonitor site and source code there and didn't understand how to use it.
What else can I do ?
Thanks.
I've tested this code:
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 OpenHardwareMonitor;
using OpenHardwareMonitor.Hardware;
namespace CPUTemperatureMonitor
{
public partial class Form1 : Form
{
Computer thisComputer;
public Form1()
{
InitializeComponent();
thisComputer = new Computer() { CPUEnabled = true };
thisComputer.Open();
}
private void timer1_Tick(object sender, EventArgs e)
{
String temp = "";
foreach (var hardwareItem in thisComputer.Hardware)
{
if (hardwareItem.HardwareType == HardwareType.CPU)
{
hardwareItem.Update();
foreach (IHardware subHardware in hardwareItem.SubHardware)
subHardware.Update();
foreach (var sensor in hardwareItem.Sensors)
{
if (sensor.SensorType == SensorType.Temperature)
{
temp += String.Format("{0} Temperature = {1}\r\n", sensor.Name, sensor.Value.HasValue ? sensor.Value.Value.ToString() : "no value");
}
}
}
}
textBox1.Text = temp;
}
}
}
The form has a multiline text control and a timer. Add a reference to OpenHardwareMonitorLib.dll.
You also need to request a higher execution level in the application, i.e. right click on the project, add a new manifest file item and declare
requestedExecutionLevel level="highestAvailable" uiAccess="false"

Get ONLY word count from PDF document

I was hoping to get just the word count from a pdf document programmatically.
I've looked at PDFSharp, but it's awefully bulky for what I want to do. I don't have access to the server, so I can't install acrobat to get to their api's or anything. I'd be willing to do it in iTextSharp or another tool.
iTextSharp has a wonderful PdfTextExtractor object that will get you all of the text (assumming as #Rob A pointed out that its actually stored as text and not images or pure vector). Once you've got all of the text a simple RegEx will give you the word count.
The code below should do it for you. (Tested on iText 5.1.1.0)
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.IO;
using iTextSharp.text.pdf.parser;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string InputFile = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Input.pdf");
//Get all the text
string T = ExtractAllTextFromPdf(InputFile);
//Count the words
int I = GetWordCountFromString(T);
}
public static string ExtractAllTextFromPdf(string inputFile)
{
//Sanity checks
if (string.IsNullOrEmpty(inputFile))
throw new ArgumentNullException("inputFile");
if (!System.IO.File.Exists(inputFile))
throw new System.IO.FileNotFoundException("Cannot find inputFile", inputFile);
//Create a stream reader (not necessary but I like to control locks and permissions)
using (FileStream SR = new FileStream(inputFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
//Create a reader to read the PDF
iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(SR);
//Create a buffer to store text
StringBuilder Buf = new StringBuilder();
//Use the PdfTextExtractor to get all of the text on a page-by-page basis
for (int i = 1; i <= reader.NumberOfPages; i++)
{
Buf.AppendLine(PdfTextExtractor.GetTextFromPage(reader, i));
}
return Buf.ToString();
}
}
public static int GetWordCountFromString(string text)
{
//Sanity check
if (string.IsNullOrEmpty(text))
return 0;
//Count the words
return System.Text.RegularExpressions.Regex.Matches(text, "\\S+").Count;
}
}
}
You can use a pdf2text tool and then count the words:
tools pdf2text

Categories

Resources