[Roxygen-commits] r33 - in pkg: . R sandbox

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Jun 6 13:16:27 CEST 2008


Author: manuel
Date: 2008-06-06 13:16:27 +0200 (Fri, 06 Jun 2008)
New Revision: 33

Added:
   pkg/sandbox/
   pkg/sandbox/example-S3-mcpi.R
   pkg/sandbox/example-S4-person.R
   pkg/sandbox/example-function-mcpi.R
   pkg/sandbox/example.R
   pkg/sandbox/main.R
Removed:
   pkg/R/example.R
   pkg/R/main.R
Modified:
   pkg/R/list.R
Log:
Introduce the sandbox folder and add some examples.

Deleted: pkg/R/example.R
===================================================================
--- pkg/R/example.R	2008-06-04 20:43:55 UTC (rev 32)
+++ pkg/R/example.R	2008-06-06 11:16:27 UTC (rev 33)
@@ -1,26 +0,0 @@
-#' This class represents a person.
-#'
-#' @slot fullname The full name of the person
-#' @slot birthyear The year of birth
-#' @prototype Prototype person is named John Doe
-#'      and born in the year 1971
-setClass('person',
-         representation=
-         representation(fullname='character',
-                        birthyear='numeric'),
-         prototype=
-         prototype(fullname='John Doe',
-                   birthyear=1971))
-
-#' The naming of an object.
-#'
-#' @param object A object which gets a name
-setGeneric('name', function(object) standardGeneric('name'))
-
-#' Name a person, the baptism.
-#'
-#' @param object A Person object
-#' @export
-setMethod('name', 'person', function(object) object at fullname)
-
-## name(new('person', fullname='Jane Roe', birthyear=1776))

Modified: pkg/R/list.R
===================================================================
--- pkg/R/list.R	2008-06-04 20:43:55 UTC (rev 32)
+++ pkg/R/list.R	2008-06-06 11:16:27 UTC (rev 33)
@@ -36,7 +36,8 @@
 }
 
 is.odd <- function(a) {
-  Negate(is.even)(a)
+  #Negate(is.even)(a)
+  !is.even(a)
 }
 
 zip <- function(zipper, ...) {

Deleted: pkg/R/main.R
===================================================================
--- pkg/R/main.R	2008-06-04 20:43:55 UTC (rev 32)
+++ pkg/R/main.R	2008-06-06 11:16:27 UTC (rev 33)
@@ -1,10 +0,0 @@
-source('list.R')
-source('parse.R')
-
-FILE <- 'example.R'
-
-argv <- commandArgs(trailingOnly=T)
-argc <- length(argv)
-file <- ifelse(argc > 0, car(argv), FILE)
-
-parse.file(file)

Added: pkg/sandbox/example-S3-mcpi.R
===================================================================
--- pkg/sandbox/example-S3-mcpi.R	                        (rev 0)
+++ pkg/sandbox/example-S3-mcpi.R	2008-06-06 11:16:27 UTC (rev 33)
@@ -0,0 +1,71 @@
+# An example for S3 documentation using Roxygen.
+
+
+
+#' A monte carlo algorithm to calculate PI. Extended
+#' version which returns the throws.
+#'
+#' @param trials The number of trials to throw
+#'
+#' @return mcpi a list containing the approximated
+#'              PI value and the throws
+mcpiX <- function(trials) {
+
+  throws <- matrix(NA, nrow=trials,
+                   ncol=3,
+                   dimnames=list(NULL, c('x','y','hit')))
+  hits <- 0
+  
+  for ( i in 1:trials ) {   
+    xy <- runif(2, min=, max=1)
+
+    hit <- (xy[1]*xy[1] + xy[2]*xy[2] <= 1)
+    hits <- hits + hit
+    throws[i,] <- c(xy, hit)
+  }
+
+  pi <- 4 * hits / trials
+
+  res <- list(pi=pi,
+              throws=throws)
+  class(res) <- 'mcpi'
+              
+  return(res)
+}
+
+#' Print function for 'mcpi' objects.
+#' @param x The 'mcpi' object
+#' @param ... Ignored
+#' @return NULL
+print.mcpi <- function(x, ...) {
+  cat('Approximated PI value (using', nrow(x$throws), 'throws) =', x$pi, '\n')
+}
+
+#' Plot function for 'mcpi' objects.
+#' @param x The 'mcpi' object
+#' @return NULL
+#'
+#' @import pgirmess polycircle 
+plot.mcpi <- function(x, ...) {
+  plot(x$throws[,1:2], col=x$throws[,3]+1,
+       xlim=c(0,1), ylim=c(0,1), pch=19,
+       main=paste('pi =', x$pi), ...)
+
+  # require(pgirmess)
+  lines(polycirc(1, pts=c(0, 0), nbr=100), lwd=2)
+}
+
+#' Return base data of a monte carlo algorithm.
+#' @param x A object created with a monte carlo algorithm
+mcbase <- function(x, ...) {
+  UseMethod('mcbase')
+}
+
+#' Base of PI approximation with motne carlo algorithm.
+#' @param x The 'mcpi' object
+#' @return The throws
+mcbase.mcpi <- function(x, ...) {
+  return(x$throws)
+}
+
+

Added: pkg/sandbox/example-S4-person.R
===================================================================
--- pkg/sandbox/example-S4-person.R	                        (rev 0)
+++ pkg/sandbox/example-S4-person.R	2008-06-06 11:16:27 UTC (rev 33)
@@ -0,0 +1,41 @@
+# An example for S4 documentation using Roxygen.
+
+
+
+#' This class represents a person.
+#'
+#' @slot fullname The full name of the person
+#' @slot birthyear The year of birth
+#' @prototype Prototype person is named John Doe
+#'      and born in the year 1971
+setClass('Person',
+         representation=
+         representation(fullname='character',
+                        birthyear='numeric'),
+         prototype=
+         prototype(fullname='John Doe',
+                   birthyear=1971))
+
+#' Constructor function for Person object.
+#' @param fullname The name of the person.
+#' @param birthyear The year of birth.
+#' @return Person The Person object
+#' @export
+Person <- function(fullname, birthyear) {
+  return(new('Person', fullname=fullname, birthyear=birthyear))
+}
+
+#' The naming of an object.
+#'
+#' @param object A object which gets a name
+setGeneric('name', function(object) standardGeneric('name'))
+
+#' Name a person, the baptism.
+#'
+#' @param object A Person object
+#' @export
+setMethod('name', 'Person',
+function(object) {
+  return(object at fullname)
+})
+

Added: pkg/sandbox/example-function-mcpi.R
===================================================================
--- pkg/sandbox/example-function-mcpi.R	                        (rev 0)
+++ pkg/sandbox/example-function-mcpi.R	2008-06-06 11:16:27 UTC (rev 33)
@@ -0,0 +1,35 @@
+# An example for a 'normal' function documentation using
+# Roxygen.
+
+
+
+#' A monte carlo algorithm to calculate PI.
+#'
+#' @param trials The number of trials to throw
+#' @param verbose Show some information during the tossing
+#'
+#' @return The approximation of PI
+#'
+#' @reference http://www.datastructures.info/the-monte-carlo-algorithmmethod/
+#' @author Manuel J. A. Eugster
+mcpi <- function(trials, verbose=FALSE) {
+  hits <- 0                             #' Number of successfull trials
+
+  for ( i in 1:trials ) {
+    if ( verbose )
+      cat('.')
+    
+    xy <- runif(2, min=, max=1)         #' Simulate the throwing, sample (x,y)
+
+    if ( xy[1]*xy[1] + xy[2]*xy[2] <= 1 )
+      hits <- hits + 1
+  }
+
+  pi <- 4 * hits / trials
+
+  if ( verbose )
+    cat('done\n')
+  
+  return(pi)
+}
+

Copied: pkg/sandbox/example.R (from rev 32, pkg/R/example.R)
===================================================================
--- pkg/sandbox/example.R	                        (rev 0)
+++ pkg/sandbox/example.R	2008-06-06 11:16:27 UTC (rev 33)
@@ -0,0 +1,26 @@
+#' This class represents a person.
+#'
+#' @slot fullname The full name of the person
+#' @slot birthyear The year of birth
+#' @prototype Prototype person is named John Doe
+#'      and born in the year 1971
+setClass('person',
+         representation=
+         representation(fullname='character',
+                        birthyear='numeric'),
+         prototype=
+         prototype(fullname='John Doe',
+                   birthyear=1971))
+
+#' The naming of an object.
+#'
+#' @param object A object which gets a name
+setGeneric('name', function(object) standardGeneric('name'))
+
+#' Name a person, the baptism.
+#'
+#' @param object A Person object
+#' @export
+setMethod('name', 'person', function(object) object at fullname)
+
+## name(new('person', fullname='Jane Roe', birthyear=1776))

Copied: pkg/sandbox/main.R (from rev 32, pkg/R/main.R)
===================================================================
--- pkg/sandbox/main.R	                        (rev 0)
+++ pkg/sandbox/main.R	2008-06-06 11:16:27 UTC (rev 33)
@@ -0,0 +1,14 @@
+source('../R/list.R')
+source('../R/parse.R')
+
+FILE <- 'example.R'
+
+argv <- commandArgs(trailingOnly=T)
+argc <- length(argv)
+file <- ifelse(argc > 0, car(argv), FILE)
+
+l <- parse.file(file)
+str(l)
+
+
+l1 <- parse.file('example-S4-person.R')



More information about the Roxygen-commits mailing list