Wealth-Lab and R statistics
Author: dynamo1126
Creation Date: 10/16/2008 3:29 PM
profile picture

dynamo1126

#1
How do you integrate 5.2 with the RDCOM from R? I've followed several scripts from the following locations. http://www.codeproject.com/KB/cs/RtoCSharp.aspx

However, I don't think I am referencing the COM correctly since the error message relates to a lack of reference for
CODE:
Please log in to see this code.


I suspect I am not referencing the COM correctly, but how is this done using code within WealthLab? My understanding for C# it is usually done with a menu in Visual C#
I'm struggling through C# so any help on this would be appreciated

Thanks.
profile picture

Eugene

#2
Google "COM Interop".
profile picture

dynamo1126

#3
Eugene

They really don't pay you enough!!! Thanks this is great.
profile picture

dynamo1126

#4
Eugene

I can compile my program without error, but when I run the package I get the following error:
Could not load file or assembly 'STATCONNECTORSRVLib, Version=1.1.0.0, Public KeyToken=null or one of its dependencies. The system cannot find the file specified.

This occurs after the system compiles and accepts my references.
profile picture

dynamo1126

#5
I'm trying to use R in a C# application (Wealth-Lab 5.0) that allows .NET assemblies to be added as a reference, but not COM items. To address this I've used the tlbimp.exe to create NET assemblies that the application can read, which I understand is what occurs in Visual.net when you add a COM reference. I've assembled the following.
StatConnectorCommonLib.dll STATCONNECTORSRVLib.dll StatConnControls.dll STATCONNECTORCLNTLib.dll StatConnTools.dll. Using these assemblies in Visual.Net as .NET references my code works fine. But when I add them to my C# application I get the an error message that "Could not load file or assembly STATCONNECTORSRVLib, version=1.1.0.0, Culture=neutral, PublicKeyToken=null or one of its dependencies". Is there something else Visual.Net does to .Net assemblies? Also I am using C# .Net 3.5, but the application was assembled in 2.0 could this be an issue? Thanks for your help

Dan

Code attached

CODE:
Please log in to see this code.
profile picture

Eugene

#6
Have you placed the assemblies to Wealth-Lab main directory ?
profile picture

dynamo1126

#7
Thanks Eugene, that did the trick. Just for my own knowledge, why didn't using the other references tab work?

Thanks again

Dan
profile picture

Eugene

#8
QUOTE:
Thanks Eugene, that did the trick. Just for my own knowledge, why didn't using the other references tab work?

Have you saved the Strategy with these references selected in the dialog window? These references should be saved with each Strategy window where you need to utilize them.
profile picture

dynamo1126

#9
I did save them with each strategy, its just the .dlls were outside of the wealthlab directory, when I reloaded they were still there. However, needed to put them in the wealthlab directory for it to work. As an aside now I don't need to reference them under the "Other assemblies to Reference" Tab, yet they do not seem to appear under the ".NET Framework" tab so I assume being part of the directory allows them to be compiled without calling on them.
profile picture

Eugene

#10
The error message "Could not load file or assembly..." is rather self-descriptive: like Wealth-Lab finds the interop DLL, compiles the code, but in runtime loses track of the COM DLL itself.

QUOTE:
As an aside now I don't need to reference them under the "Other assemblies to Reference" Tab, yet they do not seem to appear under the ".NET Framework" tab


I believe they shouldn't be listed in ".NET Framework" unless added to the GAC with gacutil.exe manually.

QUOTE:
I did save them with each strategy, its just the .dlls were outside of the wealthlab directory, when I reloaded they were still there.


Question:

If you close WL5, restart it and open the saved Strategy with those references added in "Other Assemblies to Reference - Add Reference", do you still see them on the list?
profile picture

sedelstein

#11
Hello

Is there any update for this using WL 6 ?

I'd like to access the statistics in R from WL

Thanks
profile picture

Eugene

#12
There seems to be new stuff like R.NET if you google "R statistics c#".
profile picture

sedelstein

#13
Thanks Eugene

My searches turned up DCOM servers but this is likely the best way to go

I know this is not strictly a WL question but if it something you can help with it would be appreciated. It might be helpful for other WL users who want to try this and hopefully it's just my error in missing a step

I placed the RDotNet.dll in the main WL directory
Went to references -> Other assemblies to reference and tried to add this .dll file

I thought this would do the trick but I got an error message about "sandboxing" this assembly in a previous .NET Framework

Did I add the reference correctly?

Thanks
profile picture

Eugene

#14
Have you cleared the "downloaded from internet" attribute in File Properties?

.net local assembly load failed with CAS policy



Don't forget to Apply after unblocking.
profile picture

sedelstein

#15
Hello Eugene

I am try to add the RDotNet.dll reference to some new strategy

Despite repeated attempts. e.g. unblocking and reading what I could about it, When I try to add this .NET reference I get the error message

Could not add this Reference: An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous. If this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch. See http://go.micorsoft.com/fwlink/?LinkId=155569 for more information

I've followed the links but still a bit flumoxed. Does this message make sense to you and if possible, can you offer a suggestion as to what to try next?

Thanks again
profile picture

sedelstein

#16
For what it's worth, I Googled "Unblock property not sticking"

and then this link http://answers.microsoft.com/en-us/windows/forum/windows_7-security/clicking-on-unblock-for-a-files-security/9a6f5cfb-d0f8-4f50-9328-8aa2cad3a352 and then these two posts.

I have found that zipping the files and then unzipping them does the trick when a simple click of Unblock doesn't stick. Not sure if it matters, but I used "Send To > Compressed Folder" to zip and WinRAR to unzip.

Awesome, this fixes this issue with little fuss. I used the 7-zip shell command to create a quick zip and then extract/overwrite, problem solved.




Very strange, but it worked. For WL users who are interested, I plan to use multiple linear regression, principal components, and co-integration techniques in the R statistics package
profile picture

Cone

#17
Right. As Eugene indicated, whenever you download and "manually install" a dll from the internet, you need to "Unblock" it before it can be used. You must perform the Properties > General > Unblock operation in a User data directory (like the Desktop or Downloads) - the action won't "stick" if you try to do in a directory requiring Admin privileges like "Program Files". (This is the problem you had before.)

You can also clear that "Downloaded from the internet" flag by moving the file to a pen drive and then back, or as it seems, by performing the zip/unzip operation as you did.
profile picture

sedelstein

#18
Thanks Cone,

I had the Admin privileges and thought I had all I needed.

I know (once again) this is likely beyond the scope of WL but I placed some very simple code taken from the RDotNet site

CODE:
Please log in to see this code.


and got the error CS0012 The type 'System.Dynamic.IDynamicMetaObjectProvider' is defined in an assembly that is not referenced. You must add a reference to assemply 'System.Core', Version=4.0.0.0, Culture=neutral. PublicKeyToken= (then sone hexadecimal)

I've been looking all over for System.Core and searching System.Dynamic....... etc.....

Any thoughts? I thought I was free and clear hours ago when I got things to compile with just
CODE:
Please log in to see this code.


Thanks again
profile picture

Eugene

#19
In WL 6.7 (later this year), adding a reference to System.Core.dll will no longer be required.

In WL 6.4-6.6, you need to add a reference to System.Core v4.0. Switch to "Other assemblies to reference...", click "Add a reference", find the file called System.Core.dll, highlight it and confirm. The file's placement depends on your OS and WLP "bitness". For 64-bit WLP, choose c:\windows\microsoft.net\framework64\v4.0.30319, for 32-bit WLP it's c:\windows\microsoft.net\framework\v4.0.30319
profile picture

sedelstein

#20
All I can say is WOW!

Thank you Gentlemen. It compiles
profile picture

sedelstein

#21
This is perhaps more of a C# question but it relates to the way WL runs

I know that the Execute() method gets executed multiple times (e.g. Long, Short, Both)
This causes a problem in my code as I can only instantiate the R Engine only once or it results in an error message and the code stops

CODE:
Please log in to see this code.


Is there a way to place this code outside the Execute() method but make the variable "engine" global in scope so that it can be used for each pass through Execute() and for multiple symbols ?

profile picture

Eugene

#22
This task is accomplished through instantiating REngine as a class-level variable, or in narrow sense through the Singleton design pattern.

CODE:
Please log in to see this code.


Class-level variables is a C# 101 matter, so you will find them covered everywhere if you google them.

UPDATE:
Code has been slightly edited to avoid syntax errors.
profile picture

sedelstein

#23
Thank you for the code update Eugene, I greatly appreciate your patience.

The code compiles without errors. Running it produces the runtime error "Value does not fall within expected range" which near as I can tell by checking C# websites such as stackoverflow.com is produced when you are trying to create multiple instances of an object which allows for only one at a time hence my initial thought that the problem is a result of the Execute() method being run multiple times. Am I barking up the wrong tree? It feels like I'm nearly there and I hope that other WL customers will derive some benefit as well. "R" is a very powerful tool.
profile picture

sedelstein

#24
Near as I can tell after debugging in Visual Studio, I'm only allowed to create once instance of RDotNet. Calling CreateInstance more than once per session throws an error.

Can I create an instance and initialize it outside of the Execute() method"

I tried putting those calls just after

CODE:
Please log in to see this code.

but when compiling, WL gives me unhandled exception errors and offers me the choice of Continuing WL or Quitting.

I am also reaching out the R Forums. It seems the issue is more that one call
profile picture

Eugene

#25
It seems to be a popular problem with that library. Although I have never used it, a very brief search have found what looks like the proper solution from a programmer's point of view:

Just make sure to not call Initialize() more than once:

CODE:
Please log in to see this code.


Basically, this is the same Singleton pattern but implemented inside the R.NET library itself. First you attempt to get an already created instance with that GetInstanceFromID call, and only then instantiate it again if it's null i.e. doesn't exist yet (first time call).

Should you have any further R-related queries, please ask them on the R forums.
profile picture

sedelstein

#26
QUOTE:
Should you have any further R-related queries, please ask them on the R forums.


Roger, will comply.



I very much appreciate your help to date and will keep working on this. If need be, I will consult with a professional C# programmer

Thanks again.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).