Use of drc library, instability ?

Jul 4, 2013 at 9:18 AM
Hello,
Sorry for asking help again but I can't solve my problem. When I use R in my code, a function will works one time but when I call it again, even if I don't change any settings, even if I kill the variable first, it don't want to work.
Do

            '.EagerEvaluate("dataR=(head(dataR, 15))")
            If asymptote Then
                asymptote = False
                engine.EagerEvaluate("d1=drm(X1 ~ t, data = dataR, fct = L.3(fixed=c(NA,max(dataR),NA)))")
            ElseIf tronc Then
                tronc = False
                'engine.EagerEvaluate("dataR2=(head(dataR, " & My.Settings.temps & "))")
                engine.EagerEvaluate("d1=drm(X1 ~ t, data = dataR, fct = L.3(fixed=c(NA,NA,NA)))")
            ElseIf point Then
                point = False
                'engine.EagerEvaluate("graphics.off()")
                'engine.EagerEvaluate("dataR=dataR[-" & My.Settings.Point & ",]")
                engine.EagerEvaluate("d1=drm(X1 ~ t, data = dataR, fct = L.3(fixed=c(NA,NA,NA)))")
            Else
                engine.EagerEvaluate("d1=drm(X1 ~ t, data = dataR, fct = L.3(fixed=c(NA,NA,NA)))")
            End If
            With engine
                .EagerEvaluate("plot(d1,log='')")
            End With

            Dim r As New retard
            Dim res = r.ShowDialog
            If res = System.Windows.Forms.DialogResult.OK Then
                continuer = True
            ElseIf res = System.Windows.Forms.DialogResult.Cancel Then
               
                asymptote = True
            ElseIf res = System.Windows.Forms.DialogResult.No Then
                
                tronc = True
            ElseIf res = System.Windows.Forms.DialogResult.Abort Then
                
                point = True
            End If
        Loop While (continuer = False)
In my code, in the first loop, it will do the Else of the first If bloc, then ask what we need to do, but if I try to shorten the data (if tronc), the drm function send an error.

Thank you for your help
Developer
Jul 26, 2013 at 7:09 AM
Hi,

To look at this in more details and with a hope to reproduce and isolate the issue, I need a self contained small program, without user interface code, and with the data included (if possible legally of course). As small as possible a reproducible program with its data.

Regards
Sep 4, 2013 at 4:08 PM
hi
Sorry for responding only now.

here is a version like you wanted. Thank you for your help

Imports RDotNet
Imports System
Imports System.Collections
Imports Microsoft.Win32
Imports Microsoft.VisualBasic
Imports System.IO

Public Class Form1
    Dim continuer As Boolean = False
    Dim asymptote As Boolean = False
    Dim tronc As Boolean = False
    Dim point As Boolean = False

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'openning R
        Dim res As Integer
        Dim envPath = System.Environment.GetEnvironmentVariable("PATH")
        Dim rBinPath = "C:\Program Files\R\R-3.0.1\bin\i386"
        System.Environment.SetEnvironmentVariable("PATH", envPath + Path.PathSeparator + rBinPath)
        REngine.SetDllDirectory("C:\Program Files\R\R-3.0.1\bin\i386")
        Dim engine = REngine.CreateInstance("RDotNet")
        engine.EagerEvaluate("library(drc)")
        engine.Evaluate("?drc::actimL")

        'creating data
        engine.EagerEvaluate("DO1 <- 400:450")
        engine.EagerEvaluate("DO2 <- 400:450")
        engine.EagerEvaluate("DO3 <- 400:450")
        engine.EagerEvaluate("t <- 1:51")
        engine.EagerEvaluate("dataR <- data.frame(DO1,DO2,DO3,t)")
        'Modifying data
        Do
            'doing again with a fixed asymptote
            If asymptote Then
                asymptote = False
                engine.EagerEvaluate("d1=drm(X1 ~ t, data = dataR, fct = L.3(fixed=c(NA,max(dataR),NA)))")
                'doing again only on a part of the datas
            ElseIf tronc Then
                tronc = False
                engine.EagerEvaluate("dataR2=(head(dataR, 15))") '15 is an exemple
                engine.EagerEvaluate("d1=drm(X1 ~ t, data = dataR, fct = L.3(fixed=c(NA,NA,NA)))")
                'doing again after deleted one point
            ElseIf Point Then
                Point = False
                engine.EagerEvaluate("dataR=dataR[-6,]") '6 is an exemple
                engine.EagerEvaluate("d1=drm(X1 ~ t, data = dataR, fct = L.3(fixed=c(NA,NA,NA)))")
                'first action
            Else
                engine.EagerEvaluate("d1=drm(X1 ~ t, data = dataR, fct = L.3(fixed=c(NA,NA,NA)))")
            End If
            With engine
                .EagerEvaluate("plot(d1,log='')")
            End With
            engine.EagerEvaluate("plot(t,mp)")
            Dim dataRR = engine.EagerEvaluate("dataRR=data.frame(cbind(t,mp))")
            Dim d1 = engine.EagerEvaluate("d1<-drm(mp ~ t, data = dataRR, fct = L.3(fixed=c(NA,NA,NA)))")

            res = 0 'with res=0 we go out of the loop but using 1, 2 or 3 we can ask for a treatment

            If res = 0 Then
                continuer = True
            ElseIf res = 1 Then
                asymptote = True
            ElseIf res = 2 Then
                tronc = True
            ElseIf res = 3 Then
                point = True
            End If
        Loop While (continuer = False)
        engine.Close()

    End Sub
End Class
Sep 4, 2013 at 4:21 PM

Hello Uranne,

This is a common mistake which is covered in several location on the web site.

The R.NET instance can only be create once for the life of an application.

Move your "opening R" code section to an initialization method that only gets executed once and make the "engine" variable a class level field.

Also move the engine.close() to the FormClosed event handler.

Best Regards,

Rob

Sep 5, 2013 at 1:46 PM
Hello, thank you for helping me.

This mistake is repaired. Now during the first test, my application works, but if I test it a second time just after, it doesn't works, stopping at "library(drc)" step. I think it is a problem linked with the closing of R, but nothing can prove it, not in task manager neither in my code.
But after several minutes, the application works again ...
Sep 19, 2013 at 8:50 AM
Hello,
My application still doesn't want to work. I have tried to unload my dataR variable or unload the plot before reusing it, but i haven't seen any difference. Do you have any other idea ?
Developer
Sep 21, 2013 at 2:11 PM
AFAIK The engine should be initialized once, and closed (equivalent to disposed of, using the Dispose method), per process life. Reading between the lines of the previous posts I am not sure this is the case.
Sep 23, 2013 at 8:14 AM
Hello,
This is the part of my code I added after you explain me how to initialize and close :
 Public Sub New()
        InitializeComponent()
        Dim envPath = System.Environment.GetEnvironmentVariable("PATH")
        Dim rBinPath = "C:\Program Files\R\R-3.0.0\bin\i386"
        System.Environment.SetEnvironmentVariable("PATH", envPath + Path.PathSeparator + rBinPath)
        REngine.SetDllDirectory("C:\Program Files\R\R-3.0.0\bin\i386")
        engine = REngine.CreateInstance("RDotNet")
        engine.EagerEvaluate("library(drc)")
    End Sub

    Public Sub close_app() Handles Me.FormClosed
        engine.Close()
    End Sub
But it still doesn't work, is there a mistake ?