Posts tagged c#

Running R in an Azure C# WebJob

I had the task to run R in an C# azure web job, I could of used the Azure Machine Learning Studio in order to run the R script, and executed this process via a web call. But I wanted the ability to potentially host this app outside of Azure, an a simple C# web app would allow me that with simple modifications.

This was my first time looking at R, so I had to spend quite a bit of time in R Studio to get to grips with the basics of running the scripts and handling packages. The next step was to decide on how to run the script, after a bit of searching I found the RDotNet nuget package which promised to provide a simple way of running pieces of R code within .net.

The simplest example I got running to prove the concept:

This expects a standard installation of R on your windows machine, I believe it can handle installations on unix and mac if running in mono. This script ran perfectly, I was able to get this running nicely in Linqpad as well. The next issue faced was how to get this running if you dont have a standard installation of R, as would be the case with the Azure web app.

The SetEnviromentVariables call accepts to parameters, rPath and rHome, so if your installation was else where, or even running from a usb stick or network drive you could still use RDotNet. With some lots of searching on StackOverflow, I finally found what paths the SetEnvironmentVariables call was expecting. I copied my installation of R to a new dir and updated my linqpad script with the following:

The script continued to run successfully, so with the concept proved I could move onto creating the Azure web job. Using the Kudu tools website (https://<WEB_APP_NAME>.scm.azurewebsites.net/DebugConsole), I copied the R-3.3.2 folder and its contents to /site/wwwroot/,

Web Job Folder Structure

Web Job Folder Structure

and updated the rPath and rHome variables to these new locations:Pushing the new web app and letting it run, provided the same output ’10′ in the azure streaming logs for the app. Success!

Packages

Soon after getting the basic R script running I ran into issues adding new packages, the basic error boiled down to not having rights access to download or install new packages. (There may be a simple way round this, such as changing where R is located on the web app, or some config settings).

The work around involved downloading the package I was looking to use, in this case RODBC, and uploading the zip to the web app, you can see my packages folder in the screenshot above. Next you have to install this package in your R script, I achieved this with the following:

Torrent Automation

One of the main reasons for buying a server was for the ability to leave a torrent client running 24/7, now I can. With uTorrent ( and all of its advanced options ) I can now download torrents all of the time, one of the drawbacks to this is the drain of bandwidth on the rest of the network. So I’ve set uTorrent to only download at full speed during the night, and then very limited during the daytime, this was I shouldn’t notice any impact on my network.

uTorrent Schedule

Another nice feature of uTorrent is the auto download of torrents when placed in a specific directory, so this means I can quickly add a torrent file to my NAS drive and within a few seconds the torrent is downloading. This and with addition of the RSS feed feature allows me to quickly download TV and films without much effort.

File are then automatically moved to the correct directory, based on the torrents labels, so that my Boxee Box will pick up the media without me intervening.

Another idea I had (well @Bonxy suggested) was to get twitter updates when downloads have finished. I had already dealt with twitter messages when creating my home automation system for my dissertation. So it was just a quick job of copy a bit of code and creating a simple console app that took the tweet params.

Twitter Code

I then edited my uTorrent options to ‘run a program’ when a download completes, I just simply pass the twitter message with the torrent name, and within a few seconds I get a tweet (plus email and free SMS) with my download details. I then added the same call to the status changed option in uTorrent.

tweet Image

Obviously I cancelled the above download as downloading content that isn’t freely available isn’t legal. :)

Resize An Image

For the imgUp tool I had to resize an image to various different sizes, after searching on the web for a while I came accross this nice little function.

public void ResizeImage(string OriginalFile, string NewFile, int NewWidth, int MaxHeight, bool OnlyResizeIfWider)
        {
            System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(OriginalFile);

            // Prevent using images internal thumbnail
            FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
            FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

            if (OnlyResizeIfWider)
            {
                if (FullsizeImage.Width <= NewWidth)
                {
                    NewWidth = FullsizeImage.Width;
                }
            }

            int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width;
            if (NewHeight > MaxHeight)
            {
                // Resize with height instead
                NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height;
                NewHeight = MaxHeight;
            }

            System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);

            // Clear handle to original file so that we can overwrite it if necessary
            FullsizeImage.Dispose();

            // Save resized picture
            NewImage.Save(NewFile);
        }

The method take several parameters, first the file location of the original file, then the location for the altered image to be saved. Then the width and height of the new image, finally you can set whether the image will be resized only if the width is larger than the resized size, this prevents the image from being stretched.

ResizeImage("c:\img1.jpg", "c:\img1-altered.jpg" , 640, 480, true);

I found this snippet here and here.