File and directory structure of the r project

In ordinary programming languages ​​such as java, each file usually corresponds to a class.

I just started with R. I would like to create a small program, and I would like to create a specific file and directory structure like this

Main.R # the main control script 
MyClass.R # A class that is referenced from within Main.R 
ProcessData.R # Another class that uses an object of MyClass.R as input

So, I would like to do something like this (pseudocode):

Main.R

myc <- new MyClass # create a new instance of MyClass from within Main.R
pd <- new ProcessData 
pd$processMyClass( myc ) # call a method in ProcessData that processes the myc object in some way

So, this is pretty abstract, but I just wanted to know if this is possible in principle in R.

UPDATE: I need to clarify. So the question is: how would you translate the following java program into the R program, supporting the same number of files and structures of the next toy program?

Main.java:

public static void main( String[] args ) {
    MyClass myc = new MyClass("SampleWord");
    ProcessData pd = new ProcessData();
    pd.processData( myc );
}

Myclass.java

class MyClass {

    public String word;

    public MyClass( String word ) {
        this.word = word;
    }
}

Processdata.java

class ProcessData.java {

    public void processData( MyClass myc ) {
        System.out.println( "pd.processData = " + myc.word );
    }

}
+5
source share
2 answers

Java- , R . R (S3, S4, Reference Classes) . Reference Classes - , ​​ R, , Java- R, .

( , .)

Main.R:

source("MyClass.R")
source("ProcessData.R")

main <- function() {
    myc <- new("MyClass", word = "SampleWord")
    pd <- new("ProcessData")
    cat("pd$processData =", pd$processData(myc), "\n")
}

MyClass.R:

setRefClass("MyClass", 
    fields = list(word = "character")
)

ProcessData.R:

setRefClass("ProcessData",
    fields = list(myc = "MyClass"),
    methods = list(
        processData = function(myc) myc$word
    )
)

:

source("Main.R")
main()

proto package - , Self javascript, Lua , , io language. proto ( "" -):

Main.R:

source("MyClass.R")
source("ProcessData.R")  

library(proto)

main <- function() {
    myc <- MyClass$new("SampleWord")
    pd <- ProcessData$new()
    cat("pd$processData =", pd$processData(myc), "\n")
}

MyClass.R:

MyClass <- proto(
    new = function(., word) proto(word = word)
)

ProcessData.R:

ProcessData <- proto(
    new = function(.) proto(.), 
    processData = function(., myc) myc$word
)

:

source("Main.R")
main()

UPDATE: -.

2: main MyClass .

+7

R, S3, S4 Reference.

## S3 methods, Section 5 of
RShowDoc("R-lang")

## S4 classes
?Classes
?Methods

## Reference classes
?ReferenceClasses

Java , " " ( , ), R , . S3, , , S4. S4 , , lisp, java.

.

, `ProcessData '; , generic, MyClass.

## definition and 'low-level' constructor
.MyClass <- setClass("MyClass", representation(word="character"))

## definition of a generic
setGeneric("processData", function(x, ...) standardGeneric("processData"))

setMethod("processData", "MyClass", function(x, ...) {
    cat("processData(MyClass) =", x@word, "\n")
})

> myClass <- .MyClass(word="hello world")
> processData(myClass)
processData(MyClass) = hello world 

: "AllGenerics.R" "MyClass.R" ( ) "AllGenerics.R", "AllClasses.R", "processData-methods". R "( , ).

, ,

MyClass <- function(word=character(), ...)
{
    .MyClass(word=word, ...)
}

, . ( ) .

word <- function(x, ...) x@word

, . , , . +

setGeneric("word<-", function(x, ..., value) standardGeneric("word<-"))

setReplaceMethod("word", c("MyClass", "character"), function(x, ..., value) {
    ## note double dispatch on x=MyClass, value=character
    x@word <- value
    x
})

-

setReplaceMethod("word", c("MyClass", "character"), function(x, ..., value) {
    initialize(x, word=value)
})

initialize generic default ; .

, , "show", (getGeneric("show"))

setMethod("show", "MyClass", function(object) {
    cat("class:", class(object), "\n")
    cat("word:", word(object), "\n")
})

> myClass
class: MyClass 
word: hello world 
> word(myClass)
[1] "hello world"
> word(myClass) <- "goodbye world"
> processData(myClass)
processData(MyClass) = goodbye world

R ; S4 . , , , , . , "" , 1, . , , , show

setMethod("show", "MyClass", function(object) {
    cat("class:", class(object), "\n")
    cat("word() length:", length(word(object)), "\n")
})

( Linux)

> amer <- MyClass(readLines("/usr/share/dict/american-english"))
> brit <- MyClass(readLines("/usr/share/dict/british-english"))
> amer
class: MyClass 
word() length: 99171 
> brit
class: MyClass 
word() length: 99156 
> sum(word(amer) %in% word(brit))
[1] 97423
> amer_uc <- amer  ## no copy, but marked to be copied if either changed
> word(amer_uc) <- toupper(word(amer_uc))  ## two distinct objects

.

" "

S4, . ,

.MyClass <- setClass("MyClass", representation(word="character"))
amer <- .MyClass(word=readLines("/usr/share/dict/american-english"))
amer_uc <- amer
amer_uc@word <- toupper(amer_uc@word)

, amer_uc, amer:

> amer@word[99 + 1:10]
 [1] "Adana"      "Adar"       "Adar's"     "Addams"     "Adderley"  
 [6] "Adderley's" "Addie"      "Addie's"    "Addison"    "Adela"     
> amer_uc@word[99 + 1:10]
 [1] "ADANA"      "ADAR"       "ADAR'S"     "ADDAMS"     "ADDERLEY"  
 [6] "ADDERLEY'S" "ADDIE"      "ADDIE'S"    "ADDISON"    "ADELA"     

, R - ; . ; , , R. , R , , , R, integer() data.frame lm().

,

.MyRefClass <- setRefClass("MyRefClass", fields = list(word="character"))
amer <- .MyRefClass(word=readLines("/usr/share/dict/american-english"))
amer_uc <- amer
amer_uc$word <- toupper(amer_uc$word)

amer amer_uc! C Java, R.

> amer$word[99 + 1:10]
 [1] "ADANA"      "ADAR"       "ADAR'S"     "ADDAMS"     "ADDERLEY"  
 [6] "ADDERLEY'S" "ADDIE"      "ADDIE'S"    "ADDISON"    "ADELA"     
> amer_uc$word[99 + 1:10]
 [1] "ADANA"      "ADAR"       "ADAR'S"     "ADDAMS"     "ADDERLEY"  
 [6] "ADDERLEY'S" "ADDIE"      "ADDIE'S"    "ADDISON"    "ADELA"     
+10

All Articles