• 検索結果がありません。

Rのオブジェクト指向について(R) script of

N/A
N/A
Protected

Academic year: 2018

シェア "Rのオブジェクト指向について(R) script of"

Copied!
12
0
0

読み込み中.... (全文を見る)

全文

(1)

Reference Classes

Martin Morgan

Fred Hutchinson Cancer Research Center

(2)

R

Memory and Class Systems

Memory model

◮ Everything in memory ◮ Illusion of copy-on-change Class systems

S3 Instance-based; single inheritance, single dispatch. Common use case: create once (?).

S4 Class definition; multiple inheritance, multiple dispatch. Updateable, e.g., phenoData(x)[["foo"]] <- <...>

S4 challenges

◮ Novel programming concepts. ◮ Poor memory management.

(3)

Reference Classes

◮ Introduced inR-2.12.

◮ Suitable when all code should see the same object, e.g., internet resource, GUI window, read-only data base. ◮ Single inheritance, single dispatch, fields and methods –

Java-like; methods associated with classes, rather than generics.

(4)

Example:

Stream

> setOldClass(c("file", "connection"))

> ## Define base class; .Stream is a `generator'

> .Stream <- setRefClass("Stream", + fields = list(

+ .con = "file", ## convention; no 'private' fields + fileName = "character", chunk = "integer"), + methods = list(

+ open = function() {

+ "open connection for reading"

+ ## Documentation: Stream$help("open") + .self$.con <- file(fileName, "r") + invisible(.self)

+ }, close = function() { + "close connection" + base::close(.con)

(5)

Using

Stream

> fl <- system.file("extdata", "s_1_sequence.txt", + package="Biostrings")

> strm <- .Stream$new(fileName=fl, chunk=100L) > strm$chunk

[1] 100

> strm$open() > strm$close()

> try(strm$close()) ## already closed!

(6)

Example:

ReadLinesStream

> .ReadLinesStream <- setRefClass("ReadLinesStream", + contains = "Stream",

+ methods=list(

+ stream = function() readLines(.con, chunk))) > ##

> ## Use

> strm <- .ReadLinesStream$new(fileName=fl, chunk=500L) > strm$open()

> while (length(res <- strm$stream())) + cat("length = ", length(res), "\n")

length = 500 length = 500 length = 24

(7)

ReadLinesStream

user interface I

◮ Provide user with convenient and familiar interface ◮ Document with established conventions

> ReadLinesStream <- function(fileName, chunk=256L) + {

+ .ReadLinesStream$new(fileName=fileName,

+ chunk=chunk)

+ }

> setMethod("show", "ReadLinesStream", function(object) + {

+ cat("class:", class(object), "\n")

(8)

ReadLinesStream

user interface II

> setGeneric("open")

> setMethod(open, "Stream",

+ function(con, ...) con$open()) > setGeneric("close")

> setMethod(close, "Stream",

+ function(con, ...) con$close()) > setGeneric("stream",

+ function(object, ...) standardGeneric("stream")) > setMethod("stream", "ReadLinesStream",

(9)

ReadLinesStream

user interface III

> strm <- ReadLinesStream(fl) > strm

class: ReadLinesStream fileName: s_1_sequence.txt

> open(strm)

> while (length(res <- stream(strm))) + cat("length =", length(res), "\n")

length = 256 length = 256 length = 256 length = 256

(10)

Example:

FastqSampler

in

ShortRead

◮ Import a random sub-sample from a fastqfile. ◮ Can be invoked repeatedly, for different sub-samples. Implementation

◮ Class hierarchy: Sampler,FastqSampler. ◮ GeneratorShortRead:::.FastqSampler ◮ Exposed interface: FastqSampler,yield

◮ Additional features, e.g., finalizer (close stream on garbage collection)

(11)

Challenges

◮ Retaining copy-on-change illusion when appropriate. ◮ User interface: another set of conventions for access, vs.

implementation of additional user interface?

◮ Method documentation – not leveraged or enforced byR documentation system.

(12)

Resources

参照

関連したドキュメント

the trivial automorphism [24], I generated all the rooted maps, or rather Lehman’s code for rooted maps, with e edges and v vertices, eliminated all those whose code-word is

Now it makes sense to ask if the curve x(s) has a tangent at the limit point x 0 ; this is exactly the formulation of the gradient conjecture in the Riemannian case.. By the

Such simple formulas seem not to exist for the Lusztig q-analogues K λ,μ g (q) even in the cases when λ is a single column or a single row partition.. Moreover the duality (3) is

(The Elliott-Halberstam conjecture does allow one to take B = 2 in (1.39), and therefore leads to small improve- ments in Huxley’s results, which for r ≥ 2 are weaker than the result

のようにすべきだと考えていますか。 やっと開通します。長野、太田地区方面  

Although the choice of the state spaces is free in principle, some restrictions appear in Riemann geometry: Because Einstein‘s field equations contain the second derivatives of the

We observe that the elevation of the water waves is in the form of traveling solitary waves; it increases in amplitude as the wave number increases k, as shown in Figures 3a–3d,

One verifies immediately from Theorem 1.5 and Definition 1.8 that the only difference between the data parametrized by the stack H g,d,r and the data parametrized by the stack A g,d,r