WithOpenCaseStream {RNetica} | R Documentation |
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.
WithOpenCaseStream(stream, expr)
stream |
A |
expr |
An arbitrary R expression to be executed. |
Either the result of evaluating expr unless executing
expr results in an error in which case it
returns a try-error
.
Russell Almond
## 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)