MemoryStreamContents {RNetica}R Documentation

Access the contents of a MemoryCaseStream

Description

This function returns the contents of a MemoryCaseStream's internal buffer as a data.frame. Alternatively, it sets the contents of the buffer to a given data frame.

Usage

MemoryStreamContents(stream)
MemoryStreamContents(stream) <- value

Arguments

stream

A MemoryCaseStream object whose contents is to be access.

value

Either a data frame giving the new value (see details), or else NULL.

Details

A set of cases for a Netica network corresponds to a data.frame. The columns represent nodes in the graph, and the values in that particular column correspond to findings for that node: a particular instantiation for that state, with a value of NA if the state of that node is unknown.

In addition to columns representing variables, two special columns are allowed. The column named “IDnum”, if present should contain integers which correspond to ID numbers for the cases (this correspond to the id argument of WriteFindings). The column named “NumCases” should contain number values and this allows rows to be differentially weighted (this correspond to the freq argument of WriteFindings).

A MemoryCaseStream contains an R data frame object written out in string form. This function converts between the internal string object and the data frame representation. When called as MemoryStreamContents(stream) it reads the current value of the stream and converts it to a data frame. When called as setter function, it converts the value into a string and focuses the MemoryCaseStream object on this string.

Setting the contents to NULL creates a new empty stream buffer inside of the stream object. This is useful for creating a blank buffer for writing cases.

The code MemoryCaseStream object maintains a cached copy of the data frame associated with the memory stream. Calling the function in either the setter or getter form updates that cache. Calling this function when the stream is closed, will access the cached copy. In the case of the setter form, this will updated the cached value, and if the stream is reopened, it will focus on the new cached value. Note that if the stream is closed before MemoryStreamContents is called, then the value returned will be the cached value created when MemoryStreamContents was last called, or when the stream is opened.

Value

A data frame which corresponds to the contents of the stream buffer, or NULL if the stream buffer is empty.

Note

The cached value of the stream can be accessed with the expression stream$Case_Stream_DataFrame. While it is almost certainly a mistake to set this value directly, there may be situations (e.g., avoiding duplicating the data frame when the stream is essentially open for reading only) where it is useful. On the other hand, there may be situations where it is useful to read the cached value without forcing a reread of the memory buffer.

Author(s)

Russell Almond

References

http://norsys.com/onLineAPIManual/index.html: SetStreamContents_ns(),

See Also

CaseFileDelimiter, CaseFileMissingCode, WriteFindings, ReadFindings, MemoryCaseStream, CaseStream

Examples

sess <- NeticaSession()
startSession(sess)

casefile <- file.path(library(help="RNetica")$path,
                           "testData","abctestcases.cas")
                     
CaseFileDelimiter("\t", session=sess)
CaseFileMissingCode("*", session=sess)
cases <- read.CaseFile(casefile, session=sess)

memstream <- CaseMemoryStream(cases, session=sess)

## Should be the same as cases
stopifnot(all.equal(MemoryStreamContents(memstream),cases))


MemoryStreamContents(memstream) <- cases

CloseCaseStream(memstream) ## Don't forget to read off the value
                           ## first if needed before closing.

stopifnot(!isCaseStreamOpen(memstream))
## This should return the cached value.
MemoryStreamContents(memstream)

## Will clear stream when next open
MemoryStreamContents(memstream) <- NULL

OpenCaseStream(memstream)
stopifnot(is.null(MemoryStreamContents(memstream)))

CloseCaseStream(memstream)

## Second test, do this from scratch.

casesabb <-
  data.frame(IDnum=1001:1010,NumCases=rep(1,10),
             A=c("A1","A1","A1","A1","A1","A2","A2","A2","A2","A2"),
             B1=c("B1","B1","B1","B2","B2","B2","B2","B2","B1","B1"),
             B2=c("B1","B1","B1","B1","B2","B2","B2","B2","B2","B1"))

abbstream <- CaseMemoryStream(casesabb, session=sess)
MemoryStreamContents(abbstream)
CloseCaseStream(abbstream)

abb <- CreateNetwork("ABB", session=sess)
A <- NewDiscreteNode(abb,"A",c("A1","A2"))
B1 <- NewDiscreteNode(abb,"B1",c("B1","B2"))
B2 <- NewDiscreteNode(abb,"B2",c("B1","B2"))

AddLink(A,B1)
AddLink(A,B2)

A[] <- c(.5,.5)
NodeExperience(A) <- 10

B1["A1"] <- c(.8,.2)
B1["A2"] <- c(.2,.8)
B2["A1"] <- c(.8,.2)
B2["A2"] <- c(.2,.8)
NodeExperience(B1) <- c(10,10)
NodeExperience(B2) <- c(10,10)

abbstream <- CaseMemoryStream(casesabb, session=sess)
## This does not appear to work correctly
abbstream <- ReadFindings(list(A,B1,B2),abbstream,"FIRST")
NodeFinding(A)
NodeFinding(B1)
NodeFinding(B2)

CloseCaseStream(abbstream)
DeleteNetwork(abb)
stopSession(sess)


[Package RNetica version 0.5-4 Index]