Examples for <code language_="r">

Usage examples to leverage the new language_="r" functionality in the <code> tag.

Description

The <code> tag now supports language_="r", allowing 1010data to interface with this popular statistics package from within your macro code. See <code> in the 1010data Reference Manual for more information.

When using the <code> tag, it is necessary to put the R functions and related operations inside of a CDATA tag.

Note: Your account must be specifically enabled to make use of the R language.

Examples

The following example creates a new worksheet with the columns 'state' and 'elev'. The new worksheet is a subset of rows and columns.

<base table="pub.demo.weather.stations">
<code language_="r">
  <![CDATA[
    #Create a new worksheet with the columns 'state' and 'elev'.
    #The new worksheet is a subset of rows and columns.
    #The R objects 'table' and 'ops' automatically exist in the R session.
    n <- Rows(ops,table)                          #get number of rows
    cmd <- ColsMetaData(ops,table)                #get meta-data
    cols <- c('state','elev')
    cols.ind <- which(ColNames(cmd) %in% cols)
    cmd2 <- SliceMetaData(cmd,cols.ind)           #subset meta-data
    dat <- Data(ops,table,10:n,cols)              #get data
    ops <- ReBase(dat,cmd2)                       #re-base and assign to new value to ops.
  ]]>
</code>

The following example tests 4 columns for normality:

<base table="mdb_regression_test_folder.stations"/>
<code language_="r">
<![CDATA[
a <- function(ops,table){
  if((N <- Rows(ops,table)) > 1000000) stop('too much data')
  d <- Data(ops,table)
  test.cols <- c('lat','lon','elev','anht')
  #run shapiro tests and rbind together data.frames
  shapiro.results <- do.call("rbind",
                             lapply(d[,test.cols],function(x){
                               r <- shapiro.test(x)
                               data.frame(stat=r$statistic,
                                          p_val=r$p.value,
                                          method=r$method)}))
  shapiro.results$name <-row.names(shapiro.results)
  #run KS tests and rbind results
  ks.results <- do.call("rbind",
                        lapply(d[,test.cols],function(x){
                          r <- ks.test(scale(x),"pnorm")
                          data.frame(stat=r$statistic,
                                     p_val=r$p.value,
                                     method=r$method)}))
  ks.results$name <- row.names(ks.results)
  #rbind shapiro and KS results
  res <- rbind(shapiro.results,ks.results)
  ops <- ReBase(res)}
ops <- a(ops,table)
]]>
</code>
<willbe name="is_normal" value="p_val>0.05"/>