[Zooimage-commits] r104 - pkg/zooimage/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Apr 21 14:42:48 CEST 2009


Author: romain
Date: 2009-04-21 14:42:48 +0200 (Tue, 21 Apr 2009)
New Revision: 104

Modified:
   pkg/zooimage/R/ZIClass.r
   pkg/zooimage/R/ZIRes.r
   pkg/zooimage/R/zie.r
Log:
less eval( parse( ) ) constructs (using match.fun)

Modified: pkg/zooimage/R/ZIClass.r
===================================================================
--- pkg/zooimage/R/ZIClass.r	2009-04-21 12:29:43 UTC (rev 103)
+++ pkg/zooimage/R/ZIClass.r	2009-04-21 12:42:48 UTC (rev 104)
@@ -39,25 +39,23 @@
 	# check calc.vars
 	calc.vars <- calc.vars[1]
 	if (!is.null(calc.vars)) {
-		#eval(parse(text = paste("df <- ", calc.vars, "(df)", sep = "")))
-		if (!exists(calc.vars, mode = "function"))
-		   stop("Function ", calc.vars, "() not found!")
-		CV <- get(calc.vars, mode = "function")
+		CV <- match.fun( calc.vars )
 		df <- CV(df)
 	}
 	
 	# algorithm
 	algorithm <- algorithm[1]
-	eval(parse(text = paste("ZI.class <- ", algorithm, "(Formula, data = df, ...)", sep = "")))
-	#	if (!exists(ZI.class))
-	#		stop("Error while training the '", algorithm, "' algorithm!")
+	algo.fun  <- match.fun( algorithm )
+	ZI.class <- algo.fun(Formula, data = df, ...)
+	
 	# Return a ZIClass object
 	class(ZI.class) <- c("ZIClass", class(ZI.class))
 	attr(ZI.class, "algorithm") <- algorithm
 	attr(ZI.class, "package") <- package
-	attr(ZI.class, "calc.vars") <- get(calc.vars, envir = parent.frame())
+	attr(ZI.class, "calc.vars") <- CV
 	Classes <- df[[as.character(Formula)[2]]]
 	attr(ZI.class, "classes") <- Classes
+	
 	# Calculate predictions with full training set
     attr(ZI.class, "predict") <- predict(ZI.class, df, calc.vars = FALSE, class.only = TRUE)
 
@@ -70,10 +68,10 @@
   
 	# Possibly make a k-fold cross-validation and check results
 	if (!is.null(k.xval)) {
-		if (algorithm == "lda") {
-			mypredict <- function(object, newdata) predict(object, newdata = newdata)$class
+		mypredict <- if (algorithm == "lda") {
+			function(object, newdata) predict(object, newdata = newdata)$class
 		} else {
-			mypredict <- function(object, newdata) predict(object, newdata = newdata, type = "class")
+			function(object, newdata) predict(object, newdata = newdata, type = "class")
 		}
     	res <- cv(Classes, Formula, data = df, model = get(algorithm),
 			predict = mypredict, k = k.xval, predictions = TRUE, ...)$predictions

Modified: pkg/zooimage/R/ZIRes.r
===================================================================
--- pkg/zooimage/R/ZIRes.r	2009-04-21 12:29:43 UTC (rev 103)
+++ pkg/zooimage/R/ZIRes.r	2009-04-21 12:42:48 UTC (rev 104)
@@ -1,4 +1,4 @@
-# Copyright (c) 2004, Ph. Grosjean <phgrosjean at sciviews.org>
+# {{{ Copyright (c) 2004, Ph. Grosjean <phgrosjean at sciviews.org>
 #
 # This file is part of ZooImage .
 # 
@@ -14,6 +14,7 @@
 # 
 # You should have received a copy of the GNU General Public License
 # along with ZooImage.  If not, see <http://www.gnu.org/licenses/>.
+# }}}
 
 # {{{ process.sample
 "process.sample" <- function(ZidFile, ZIClass, ZIDesc,

Modified: pkg/zooimage/R/zie.r
===================================================================
--- pkg/zooimage/R/zie.r	2009-04-21 12:29:43 UTC (rev 103)
+++ pkg/zooimage/R/zie.r	2009-04-21 12:42:48 UTC (rev 104)
@@ -760,8 +760,9 @@
         num <- eval(parse(text = num))
         # Check if the number is correct
 		### TODO: add this in the template file!
-		if (length(num) < Nmin || length(num) > Nmax)
+		if (length(num) < Nmin || length(num) > Nmax){
            stop("Wrong number of images in 'Image' field for ", Data$Sample[i], "!")
+		}
 		
 		# Update several fileds according to what is deefined in the samples table
 		###TODO: add the other fields + define this option



More information about the Zooimage-commits mailing list