WithOpenCaseStream {RNetica}R Documentation

Evaluate an expression and then close the Netica Case Stream.

Description

This function evaluates expr in a context where the CaseStream is open. The stream is closed when the evaluation is complete. The evaluation of expr is surrounded with a tryCatch so that the stream is closed whether or not the expression is successfully executed.

Usage

WithOpenCaseStream(stream, expr)

Arguments

stream

A CaseStream object. This can be open or closed. If closed it is reopened.

expr

An arbitrary R expression to be executed.

Value

Either the result of evaluating expr unless executing expr results in an error in which case it returns a try-error.

Author(s)

Russell Almond

See Also

CaseStream,ReadFindings

Examples


## This function reads findings from a stream until it finds one
## matching a certain case ID.
ReadCase <- function (stream,nodes,caseID) {
  WithOpenCaseStream(stream,
    {stream <- ReadFindings(nodes,stream,"FIRST")
     while(!is.na(getCaseStreamPos(stream)) &&
           getCaseStreamLastId(stream) != caseID) {
       ReadFindings(nodes,stream,"NEXT")
     }
     if (is.na(getCaseStreamPos(stream))) {
       warning("Case ID:",caseID," not found in stream.")
     }
     stream
   })
}

sess <- NeticaSession()
startSession(sess)


## Test it.
abc <- CreateNetwork("ABC", session=sess)
A <- NewDiscreteNode(abc,"A",c("A1","A2","A3","A4"))
B <- NewDiscreteNode(abc,"B",c("B1","B2","B3"))
C <- NewDiscreteNode(abc,"C",c("C1","C2"))

AddLink(A,B)
AddLink(A,C)
AddLink(B,C)

## Input filename
## Note, this is a cached copy of the file written in the WriteFindings
## documentation. 
casefile <- file.path(library(help="RNetica")$path,
                           "testData","abctestcases.cas")


filestream <- ReadCase(CaseFileStream(casefile, session=sess),list(A,B,C),1002)
stopifnot( !isCaseStreamOpen(filestream),
           NodeFinding(A) == "A2",
           NodeFinding(B) == "B2",
           NodeFinding(C) == "C2",
           getCaseStreamLastId(filestream)==1002,
           abs(getCaseStreamLastFreq(filestream)-2.0) < .0001)

##Clean Up
DeleteNetwork(abc)
stopSession(sess)


[Package RNetica version 0.8-4 Index]