Tabulation function examples

The following examples show how to implement various R applications within Macro Language code tabulation functions DistApply(), DistReduce(), MakeTable(), and Get().

Example: Simple unique counts aggregation

The following example uses the R tabulation function DistApply() to perform a simply dummy unique counts aggregation.

<defop name="dummy_ucnt_tabu">
 <tabu>
  <code language_="r">
  <![CDATA[
col <- 'species' # x is the object args passed to DistApply
f <- function(x){
  # Get retrieves data for the column name supplied.
  # the name supplied to Get must be a name supplied to using in DistApply.
  dat <- Get(x[['col']])
  return(unique(tolower(dat)))}
 
args <- list(col = col)
# DistApply returns a list of results. Each list element is the result of a segment.
results <- DistApply(using = col,fun = f, args = args)
 
unqcnts <- length(unique(unlist(results)))
 
# MakeTable takes a data.frame and optionally a labels argument.
# Note: that the return object of MakeTable is called 'tabu'
tabu <- MakeTable(data.frame(unique_counts = unqcnts),labels='Unique Counts')
# a value called 'tabu' must exist before the R code exits.
  ]]>
  </code>
 </tabu>
</defop>
<base table="examples.iris"/>
<dummy_ucnt_tabu/>

Example: Simple unique counts aggregation

The following example uses the R tabulation function DistReduce() to perform a simply dummy unique counts aggregation.

<defop name="dummy_reduce_ucnt_tabu">
  <tabu>
    <code language_="r">
      <![CDATA[
col <- 'species'
# x is the object args passed to DistApply
mf <- function(x){
  # Get retrieves data for the column name supplied.
  # the name supplied to Get must be a name supplied to using in DistApply.
  dat <- Get(x[['col']])
  return(unique(tolower(dat)))}
 
rf <- function(x,y) unique(unlist(c(x,y))) 
#to lower is not needed as it should have already been made lower case.
 
args <- list(col = col)
# DistReduce returns a list of results. Each list element is the result of a segment.
results <- DistReduce(using = col,mapfun = mf, args = args, reducefun = rf)
 
unqcnts <- length(unique(unlist(results)))
 
# MakeTable takes a data.frame and optionally a labels argument.
# Note: that the return object of MakeTable is called 'tabu'
tabu <- MakeTable(data.frame(unique_counts = unqcnts),labels='Unique Counts')
# a value called 'tabu' must exist before the R code exits.
      ]]>
    </code>
  </tabu>
</defop>
<base table="examples.iris"/>
<dummy_reduce_ucnt_tabu/>