Calling R-UDF's from Excel - r

I want to have functions in Excel (Taking Excel-Arguments) that call R-Functions and return the results back to Excel. RExcel does not seem to be on CRAN (anymore) and I have not yet found an alternative. Does anyone know of a way to achieve this?
One constraint is that the solution should work even without administrator privileges on Windows Vista.
Edit:
Thanks for the comment. We have tried R.Net, but with little success. We went for:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ExcelDna.Integration;
using System.Windows.Forms;
using RDotNet;
using RDotNet.NativeLibrary;
public static class MyFunctions
{
[ExcelFunction(Description = "Test R connection")]
public static double MyRTest(double x)
{
var envPath = Environment.GetEnvironmentVariable("PATH");
var rBinPath = #"D:\Programme\R\R-2.15.1\bin\i386";
Environment.SetEnvironmentVariable("PATH", envPath + Path.PathSeparator + rBinPath);
// using (REngine engine = REngine.CreateInstance("RDotNet"))
// {
// }
// REngine rengine = REngine.CreateInstance("RDotNet");
return (2*x);
} // End Function
[ExcelFunction(Description="Multiplies two numbers", Category="Useful functions")]
public static double MultiplyThem(double x, double y)
{
return x * y;
}
}
The function MultiplyThem() can be Called in Excel and it works. As is, so does MyRTest. But as soon as I try to instanciate R using either of the methods commented out, it returns the #Value error in Excel. Trying to make sense of it, we went for a Command Line application and just copied the example from here (v1.5). However at the line engine.Initialize(); the debugger just terminated, without error or exception. We use R12.15.1 (32 Bit), Windows Vista (32 Bit), Visual Studio 2010 Professional and RdotNet 1.5.0. What could have gone wrong?

Related

Process to find camera pointer in a game using cheat engine?

I am trying to find out how to get a mouse working inside RPCS3 emulator for a game (Ghost Recon Future Soldier with patch 1.05)
There is a library that supports injecting the mouse but doesn't support the game I am trying to play. After a lot of digging, I found a library that actually implements mouse injection in a few games.
Sample implementation for the KillZone3 game to support mouse injection looks like this in C#
using KAMI.Core.Cameras;
using KAMI.Core.Utilities;
using System;
namespace KAMI.Core.Games
{
public class Killzone2PS3 : Game<HVecVACamera>
{
DerefChain m_hor;
DerefChain m_vert;
public Killzone2PS3(IntPtr ipc, string version) : base(ipc)
{
uint baseAddress = version switch
{
"01.01" => 0x117e740 + 0x234,
"01.29" => 0x11B0540 + 0x234,
_ => throw new NotImplementedException($"{nameof(Killzone2PS3)} [v'{version}'] is not implemented"),
};
var baseChain = DerefChain.CreateDerefChain(ipc, baseAddress, 0x0);
m_vert = baseChain.Chain(0x80).Chain(0x5c).Chain(0x11c).Chain(0x78);
m_hor = baseChain.Chain(0x78).Chain(0x0).Chain(0x68).Chain(0xc).Chain(0x90);
}
public override void UpdateCamera(int diffX, int diffY)
{
if (DerefChain.VerifyChains(m_hor, m_vert))
{
m_camera.HorY = IPCUtils.ReadFloat(m_ipc, (uint)m_hor.Value);
m_camera.HorX = IPCUtils.ReadFloat(m_ipc, (uint)(m_hor.Value + 4));
m_camera.Vert = IPCUtils.ReadFloat(m_ipc, (uint)m_vert.Value);
m_camera.Update(diffX * SensModifier, -diffY * SensModifier);
IPCUtils.WriteFloat(m_ipc, (uint)m_hor.Value, m_camera.HorY);
IPCUtils.WriteFloat(m_ipc, (uint)(m_hor.Value + 4), m_camera.HorX);
IPCUtils.WriteFloat(m_ipc, (uint)m_vert.Value, m_camera.Vert);
}
}
}
}
Main lines in the above program are those addresses which I believe are associated with the camera pointer stored in memory obtained mostly with Cheat Engine.
What is the process required to find these pointers for my game. I am aware that is may be different for each game but I could really use some direction here. Where do I start? How do I narrow down till I arrive at this pointer
What a coincidence, I am also doing the exact same thing on RPCS3 right now. After digging around I've found some videos that discuss into how to use Cheat Engine to find where a player's position and camera would be stored. It involves making a lot of separate scans in Cheat Engine to search for unknown values that are increasing/decreasing.
This video seems to be the closest thing to what you are looking for:
https://www.youtube.com/watch?v=yAl_6qg6ZnA
You should also make sure you set up Cheat Engine so it works with RPCS3 correctly as shown here:
https://exvsfbce.home.blog/2019/08/24/basic-cheat-engine-setup-on-rpcs3/
After you've found the correct pointer for the camera it should be fairly easy to implement it into the library by making your own class in the Games folder.

Converting ImageMoniker to WPF BitmapSource in VS2022

I'm developing an extension for Visual Studio that includes a XAML view inside a VS window. I want the extension to look and feel like the native UI. The extension is currently running and working fine in VS2017 and VS2019 using the following code to transform a moniker to a WPF BitmapSource that can be used directly from XAML:
public static BitmapSource GetIconForImageMoniker(ImageMoniker? imageMoniker, int sizeX, int sizeY)
{
if (imageMoniker == null)
{
return null;
}
IVsImageService2 vsIconService = ServiceProvider.GlobalProvider.GetService(typeof(SVsImageService)) as IVsImageService2;
if (vsIconService == null)
{
return null;
}
ImageAttributes imageAttributes = new ImageAttributes
{
Flags = (uint)_ImageAttributesFlags.IAF_RequiredFlags,
ImageType = (uint)_UIImageType.IT_Bitmap,
Format = (uint)_UIDataFormat.DF_WPF,
LogicalHeight = sizeY,
LogicalWidth = sizeX,
StructSize = Marshal.SizeOf(typeof(ImageAttributes))
};
IVsUIObject result = vsIconService.GetImage(imageMoniker.Value, imageAttributes);
object data;
result.get_Data(out data);
BitmapSource glyph = data as BitmapSource;
if (glyph != null)
{
glyph.Freeze();
}
return glyph;
}
This method is a direct copy-paste from the WpfUtil class available in multiple of Mads Kristensen's extensions.
As already mentioned, this works fine in VS2017 and VS2019. Now I want this running in VS2022 as well. The extension shows up inside VS2022 but the icons are no longer shown. The problem is that this returns null in VS2022 but not in the previous versions:
ServiceProvider.GlobalProvider.GetService(typeof(SVsImageService)) as IVsImageService2;
Does anyone know how to make this work in VS2022 as well?
This is caused by changes to the interop libraries in VS2022. Namely, they've all been consolidated to a single library (you can see the details here).
This does break compatibility with targeting prior versions of Visual Studio. There is a guide for migrating extensions to VS2022, but to summarize, the guidance is to:
Refactor the source code into a Shared Project.
Create a new VSIX projects targeting VS2022 and the VSIX project you have now would remain for targeting prior versions.

ASP.Net is too slow

Name: http://localhost:50692/bfea4cb4df42428dac17db20239d7d53/arterySignalR/poll?transport=longPolling&connectionToken=AQAAANCMnd8BFdERjHoAwE%2FCl%2BsBAAAAY8r9H53SS0mPVZi%2BJaxhmgAAAAACAAAAAAADZgAAwAAAABAAAACxV1Yze0hqYW74IbMt%2F3ccAAAAAASAAACgAAAAEAAAANgm3EjRWVa2PG4Y0yKe170oAAAA3HRUEcQdeyzIzSRY1%2B88s1AWq3zAs3dVYKoE8nrk0XpbjrLFXenhzRQAAACtcSj%2FmTPLei2BFkjrPAqbbTphxA%3D%3D&messageId=d-90D2E0DD-B%2C0%7CC%2C9%7CD%2C0&requestUrl=http%3A%2F%2Flocalhost%3A53648%2F&browserName=Chrome&userAgent=Mozilla%2F5.0+(Windows+NT+6.1%3B+WOW64)+AppleWebKit%2F537.36+(KHTML%2C+like+Gecko)+Chrome%2F43.0.2357.132+Safari%2F537.36&tid=8&_=1436780235916
Status: 200
Type: xhr
Initiator: browserLink:37
Size: 336 B
Time: 6.0 seconds
From the Network tab in Inspect Element in Chrome.
It just does this over and over again without getting anywhere. I have no idea what parts of my code to give you, and I'd rather not swarm you with the whole project. Tell me what you need to see and I'll give you it.
When I click "copy as HAR" I get this: http://pastebin.com/tqqNGYpW
It's interesting that the 'log' at the top shows version 1.2, and I just uninstalled log4net version 1.2 and installed log4net version 2.x instead to fix another problem. The problem was related to some 32-bit 64-bit problems between LinqToExcel (it uses log4net).
This is almost certainly LinqToExcel-related. It only hangs sometimes. It continues doing the above even after I've stopped the loading process.
Further info:
The page that hangs displays Excel files from LinqToExcel. The page that does not use LinqToExcel does not hang. It doesn't matter if the page has Excel files to display, or not.
The hang occurs when I call the Index() method.
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using steer.Models;
using LinqToExcel;
namespace steer.Controllers
{
public class FilesController : Controller
{
private UpFile.UpFileDBContext db = new UpFile.UpFileDBContext();
// GET: Upload
public ActionResult Index()
{
return View(db.UpFiles.ToList());
}
// GET: Upload/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
UpFile upFile = db.UpFiles.Find(id);
if (upFile == null)
{
return HttpNotFound();
}
var excel = new ExcelQueryFactory(AppDomain.CurrentDomain.BaseDirectory+"Uploaded\\"+upFile.Name);
var firstSheet = excel.GetWorksheetNames().First();
var excelRows = from c in excel.Worksheet(firstSheet)
select c;
ViewBag.excelRows = excelRows;
ViewBag.excelColumns = excel.GetColumnNames(firstSheet); ;
ViewBag.numberOfColumns = ViewBag.excelColumns.Count;
return View(upFile);
}
}
}
This is just a guess, but I'm assuming you have files you've uploaded in a database table and the page that is slow is the index which just shows the list of files.
If you have all the file details and the file contents in the same table then I suspect you're loading all those file contents for every single file every time you do an index.
Assuming this is the case you can either tell the linq to just select the info you want, or split out the data across 2 tables to make it so that you only end up with the data if you explicitly link it in.

excel data from asp

i have made a list of folders from my directory, to show as html.
What i want is to be able to read and write in excel.
now what am strugleing to do is put this code to get it as .xlsx. i have a excel file in my computer and want all the directories to go in that file..
I have been told EPPlus is the best solution. but am not sure how to implement this in the above code so my directories comes in excel rather than html.
any ideas/direction would be really helpful
If you want to go the route of EPPlus, first thing you need to do is add the EPPlus package to your solution. This can be achieved by doing one of the following:
Opening up NuGet, searching for EPPlus in the gallery and installing it
Opening up your Package Manager Console and typing Install-Package EPPlus
Downloading the dll file and manually adding a reference to it in your project.
Then, in your code:
using OfficeOpenXml;
using System.IO;
using System.Linq;
namespace TestConsole {
class Program {
static void Main(string[] args) {
string[] directoryList = System.IO.Directory.GetDirectories(#"C:\Users\bblack\Temp\TestDirectories\");
using (Stream file = new FileStream(#"C:\Users\bblack\Temp\testexcelfile.xlsx", FileMode.Create))
using (ExcelPackage xl = new ExcelPackage(file)) {
ExcelWorksheet sheet = xl.Workbook.Worksheets.Add("Sheet1");
for (int i = 1; i < directoryList.Count(); i++)
sheet.Cells[i, 1].Value = directoryList[i];
xl.Save();
}
}
}
}
This
Gets all the directories in the address provided to it.
Creates a new FileStream where you want your Excel file to be
Creates a new ExcelPackage based off of the FileStream
Adds a new worksheet (there are zero by default) to the workbook in the ExcelPackage
Iterates through directoryList, and for each string in there puts the value in a new cell
it is important to note, that worksheet cell indexes are not zero-based, they start with a base index of 1
edit Woops, forgot to close the stream, use this updated answer.

ASP.NET MVC Reference script file with version wildcard (without bundling)

In a ASP.NET MVC 4 project, I'd like to reference a versioned script file like this:
// Just some pseudo-code:
<script src="#Latest("~/Scripts/jquery-{0}.min.js")"></script>
// Resolves to the currently referenced script file
<script src="/Scripts/jquery-1.10.2.min.js"></script>
so that when a new Script version is updated via NuGet, the reference is updated automatically. I know of the bundling-and-minification feature, but it's just to much. I just want the little part which resolves the wildcards. My files are already minified, and also I don't want the bundles.
Do you have some smart ideas how to solve this?
Even though it's a little over kill to use the Bundling in MVC, but I think that will be your best bet. It's already been done and proven so why spend more time to write some proprietary code.
That being said, if you want a simple sample of what you can do, then you can try the following.
public static class Util
{
private const string _scriptFolder = "Scripts";
public static string GetScripts(string expression)
{
var path = HttpRuntime.AppDomainAppPath;
var files = Directory.GetFiles(path + _scriptFolder).Select(x => Path.GetFileName(x)).ToList();
string script = string.Empty;
expression = expression.Replace(".", #"\.").Replace("{0}", "(\\d+\\.?)+");
Regex r = new Regex(#expression, RegexOptions.IgnoreCase);
foreach (var f in files)
{
Match m = r.Match(f);
while (m.Success)
{
script = m.Captures[0].ToString();
m = m.NextMatch();
}
}
return script;
}
}
This will return you the last match in your Scripts director or it will return empty string.
Using this call
#Html.Raw(MvcApplication1.Util.GetScripts("jquery-{0}.min.js"))
Will get you this result if 1.8.2 is the last file that matched your string.
jquery-1.8.2.min.js
Hope this will help you get started.

Resources