Skip to content

Example 5

Jonathon Love edited this page Nov 15, 2016 · 2 revisions

Having setup the results for this analysis in earlier examples, we can now turn to it's implementation in the .b.R file. As the results objects are now specified in the .r.yaml file, we can do away with the .init() function.

Example05Class <- R6::R6Class(
    "Example05Class",
    inherit = Example05Base,
    private = list(
        .run = function() {

            if (length(self$options$deps) == 0 ||
                is.null(self$options$group))
                    return()

            ttestTable <- self$results$ttest

            for (rowKey in ttestTable$rowKeys) {

                data <- data.frame(
                    x = self$data[[self$options$group]],
                    y = self$data[[rowKey]])

                ttest <- t.test(y ~ x, data, var.equal=self$options$equVar)
                ttestTable$setRow(rowKey=rowKey, values=list(
                    stat=unname(ttest$statistic),
                    df=unname(ttest$parameter),
                    p=unname(ttest$p.value)
                ))
            }

        })
)

First up we need to accommodate the changes we made to the dependent option. It is now called deps, and is of type Variables. Rather than checking to see if it is NULL, we now want to see if it's length is zero. If zero, we do not want to perform the analysis:

...
            if (length(self$options$deps) == 0 ||
                is.null(self$options$group))
                    return()
...

Next we want to retrieve the ttest table from the results, and iterate over its keys.

            ttestTable <- self$results$ttest

            for (rowKey in ttestTable$rowKeys) {

                ...
            }

For each row key, we want to perform a t-test:

...
                data <- data.frame(
                    x = self$data[[self$options$group]],
                    y = self$data[[rowKey]])

                ttest <- t.test(y ~ x, data, var.equal=self$options$equVar)
...

And for each ttest result, we want to populate a row in the table:

...
                ttestTable$setRow(rowKey=rowKey, values=list(
                    stat=unname(ttest$statistic),
                    df=unname(ttest$parameter),
                    p=unname(ttest$p.value)
                ))
...

the $setRow() function can be called with either rowKey, or rowNo. values is a list containing the values to be placed into the row. The names (in this case stat, df and p) correspond to the column names specified in the .r.yaml file. The values retrieved from the t-test result object are named, and require being 'unnamed', as can be seen.

Tutorial

  • [Getting Started](Getting Started)
  • [Example 0](Example 0)
  • [Example 1](Example 1)
  • [Example 2](Example 2)
  • [Example 3](Example 3)
  • [Example 4](Example 4)
  • [Example 5](Example 5)

API

Clone this wiki locally