[R] Lazy Evaluation?
Hello, I've stumbled upon following problem, when trying to overload the methods for group Math for an S4-class which contains functions as slots. setClass(NumFunction, representation = list(fun = function)) NumFunction - function(f) new(NumFunction, fun = f) square - function(x) x^2 NF - NumFunction(square) setMethod(Math, NumFunction, function(x){ nfun - function(n) callGeneric([EMAIL PROTECTED](n)) tmp - function(n) nfun(n) NumFunction(tmp) }) sinNF - sin(NF) [EMAIL PROTECTED](sqrt(pi/2)) # works as expected, returns 1 # now a slightly different version of setMethod(Math, NumFunction, # ...), which dispenses the unnecessary wrapper function tmp() setMethod(Math, NumFunction, function(x){ nfun - function(n) callGeneric([EMAIL PROTECTED](n)) return(NumFunction(nfun)) }) sinNF - sin(NF) [EMAIL PROTECTED](sqrt(pi/2)) # produces an error, namely: # Error in typeof(fdef) : evaluation nested too deeply: infinite # recursion / options(expression=)? Replacing the generating function NumFunction() with corresponding new(..) calls doesn't change the outcome. When I call the newly defined functions nfun resp. tmp from within the function body of setMethod(), e.g. setMethod(Math, NumFunction, function(x){ nfun - function(n) callGeneric([EMAIL PROTECTED](n)) cat(nfun(1)) NumFunction(nfun) }) by tmp(1) resp. nfun(1), both versions cat correct output when called by sin(NF). If I don't return NumFunction(tmp) resp. NumFunction(nfun) but tmp resp. nfun, both versions work just fine, i.e. sin(NF)(sqrt(pi/2)) returns 1. Would someone explain me this behavior? (R Version 1.9.0) Best regards, Thomas Stabla Sourcecode can be found at: http://www.uni-bayreuth.de/departments/math/org/mathe7/DISTR/NumFun.R __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] R CMD check errors
On Fri, 12 Mar 2004, Uwe Ligges wrote: Thomas Stabla wrote: Hello again, I tried to isolate the source code, which causes the error messages: * checking S3 generic/method consistency ... WARNING Error in .tryQuietly({ : Error in library(package, lib.loc = lib.loc, character.only = TRUE, verbose = FALSE) : package/namespace load failed * checking for replacement functions with final arg not named 'value' ... WARNING Error in .tryQuietly({ : Error in library(package, lib.loc = lib.loc, character.only = TRUE, verbose = FALSE) : package/namespace load failed I created a new package skeleton by calling package.skeleton(name = error) from R 1.8.1. I deleted the subdirectories 'data', 'src' and 'man'. The NAMESPACE file contains: exportPattern(^[^\\.]) and lacks: import(methods) and for sure you want as well: exportClass(Parameter) Thus, the complete file is: import(methods) exportClass(Parameter) exportPattern(^[^\\.]) sounds logical. BTW, I read Writing R Extensions in HMTL form under Windows R 1.8.1, and it doesn't mention exportClass. Also it doesn't describe how to use S4 classes (or package methods) and namepaces together. Hence, I think it is not up to date. The Error.R file in the subdirectory 'R' contains x - 1 setClass(Parameter, setClass(Parameter, representation(name = character)) Well, above, you mean: setClass(Parameter, representation(name = character)) copy paste error, sorry Now R CMD check distr runs smoothly, thanks for your help. Thomas Stabla __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] R CMD check errors
Hello again, I tried to isolate the source code, which causes the error messages: * checking S3 generic/method consistency ... WARNING Error in .tryQuietly({ : Error in library(package, lib.loc = lib.loc, character.only = TRUE, verbose = FALSE) : package/namespace load failed * checking for replacement functions with final arg not named 'value' ... WARNING Error in .tryQuietly({ : Error in library(package, lib.loc = lib.loc, character.only = TRUE, verbose = FALSE) : package/namespace load failed I created a new package skeleton by calling package.skeleton(name = error) from R 1.8.1. I deleted the subdirectories 'data', 'src' and 'man'. The NAMESPACE file contains: exportPattern(^[^\\.]) The Error.R file in the subdirectory 'R' contains x - 1 setClass(Parameter, setClass(Parameter, representation(name = character)) This small package produces already the errors mentioned above, if R CMD check Error is called (R 1.8.1 and also R 1.9.x). The point is, as soon as I remove the line with the setClass-call, then R CMD check Error runs without errors (there is a warning about an empty 'INDEX' file). If I remove the NAMESPACE file and call R CMD check (after build), I get these errors: * checking S3 generic/method consistency ... WARNING Error in .tryQuietly({ : Error in eval(expr, envir, enclos) : couldn't find function setClass Execution halted * checking for replacement functions with final arg not named 'value' ... WARNING Error in .tryQuietly({ : Error in eval(expr, envir, enclos) : couldn't find function setClass Execution halted Thanks for your help, Thomas Stabla Error directory available at: http://www.uni-bayreuth.de/departments/math/org/mathe7/DISTR/Error.tar.gz __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] R CMD check errors
Hello, I'm getting some error messages from R CMD check I can't deal with. I'm working under Linux with R 1.8.1 The package working directory can be found at: http://www.uni-bayreuth.de/departments/math/org/mathe7/DISTR/distr.tar.gz Here's the 00check.log produced by R CMD check distr * using log directory '/home/tom/studium/R/swp/swp/package/distr.Rcheck' * checking for file 'distr/DESCRIPTION' ... OK * checking if this is a source package ... OK * checking package directory ... OK * checking for portable file names ... OK * checking for sufficient/correct file permissions ... OK * checking DESCRIPTION meta-information ... OK * checking index information ... OK * checking package subdirectories ... WARNING Subdirectory 'data' contains no data sets. Subdirectory 'src' contains no source files. * checking R files for syntax errors ... OK * checking R files for library.dynam ... OK * checking S3 generic/method consistency ... WARNING Error in .tryQuietly({ : Error in library(package, lib.loc = lib.loc, character.only = TRUE, verbose = FALSE) : package/namespace load failed Execution halted * checking for replacement functions with final arg not named 'value' ... WARNING Error in .tryQuietly({ : Error in library(package, lib.loc = lib.loc, character.only = TRUE, verbose = FALSE) : package/namespace load failed Execution halted * checking Rd files ... OK * checking for missing documentation entries ... ERROR Error in .tryQuietly({ : Error in library(package, lib.loc = lib.loc, character.only = TRUE, verbose = FALSE) : Thanks for your help, Thomas Stabla __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] Set values in namespaces
Hello, I want to use global variables in a package which is using a namespace. But I don`t know how to change the values of the global variables. I know how to get the value of the variables, e.g. base::pi [1] 3.141593 but following code doesn`t work base::pi - 3.14 Error: Object base not found Thanks for your help, Thomas Stabla -- version _ platform i686-pc-linux-gnu arch i686 os linux-gnu system i686, linux-gnu status major1 minor8.1 year 2003 month11 day 21 language R __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Set values in namespaces
On Mon, 5 Jan 2004, Prof Brian Ripley wrote: On Mon, 5 Jan 2004, Thomas Stabla wrote: I want to use global variables in a package which is using a namespace. But I don`t know how to change the values of the global variables. I know how to get the value of the variables, e.g. base::pi [1] 3.141593 but following code doesn`t work base::pi - 3.14 Error: Object base not found Base is a special case, and assign(pi, 3.14, envir=NULL) will do this (although please don't). All other namespaces are sealed, so you will get something like library(MASS) assign(lda, pi, pos=2) Error in assign(lda, pi, pos = 2) : can't change value of a locked binding Now, that attempts to change the export, and not the value in the namespace, so there is a question of which you want and why you want to change it. (Changing the value in the namespace does not change the export, which is a copy, but as _both_ the namespace and exports environments are sealed, this would not matter much.) In which manuals do I have to look to learn more about namespaces, exports and sealed environments? I`m not yet very familiar with this concepts. I want to be able to change the values, so that the user of the package can control in some way how some of the functions in the package will work. useless example: mypackage::username = Thomas mypackage::printusername = function() print(username) There are ways around this and if you peruse the R sources you will find them. For example, grid sets up an environment in its namespace for its global variables, and in R-devel there is assignInNamespace(). Well, I looked at grid sources, and found the piece of code you mentioned, but wasn`t able to understand it fully. As a work-around, I now use the function assignInNamespace() from R-devel, which also works in R 1.8.1 if it`s pasted in the running R process. Thanks, Thomas Stabla __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] setMethod(min, myclass, ...)
Hello, I have defined a new class setClass(myclass, representation(min = numeric, max = numeric)) and want to write accessor functions, so that for foo = new(myclass, min = 0, max = 1) min(foo) # prints 0 max(foo) # prints 1 At first i created a generic function for min setGeneric(min, function(..., na.rm = FALSE) standardGeneric(min)) and then tried to use setMethod. And there's my problem, I don't know the name of the first argument which is to be passed to min. I can't just write: setMethod(min, myclass, function(..., na.rm = FALSE) [EMAIL PROTECTED]) The same problem occurs with max. Thanks for your help. Greetings, Thomas Stabla __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
[R] tcltk - tkcreate question
Hello, i'm trying to translate following tcltk source code, which I found in newsgroup comp.lang.tcl, written by Tom Wilkason, into R Code. proc scrolled_Canvas {base} { frame $base.fm -borderwidth 2 -relief sunken canvas $base.fm.cv -yscrollcommand $base.fm.cv_vertscrollbar set scrollbar $base.fm.cv_vertscrollbar -orient vertical \ -command $base.fm.cv yview pack $base.fm.cv -side left -fill both -expand true pack $base.fm.cv_vertscrollbar -side right -fill y pack $base.fm -side top -fill both -expand true set hull [frame $base.fm.cv.hull -borderwidth 2 -relief ridge] set wid [winfo width $base.fm] $base.fm.cv create window 0 0 -anchor nw -window $hull -width 10 -height 500 -tag window bind $base.fm.cv Configure ResizeCanvas %W %w %h return $hull } I have successfully translated the code until the line $base.fm.cv create window 0 0 -anchor nw -window $hull -width 10 -height 500 -tag window which i don't fully understand because i started with tcltk just this week. I tried to translate this line using the R function tkcreate, but i didn't get very far. Thanks for your help. Greetings, Thomas Stabla __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] tcltk - tkcreate question
On 27 Nov 2003, Peter Dalgaard wrote: Thomas Stabla [EMAIL PROTECTED] writes: I have successfully translated the code until the line $base.fm.cv create window 0 0 -anchor nw -window $hull -width 10 -height 500 -tag window which i don't fully understand because i started with tcltk just this week. I tried to translate this line using the R function tkcreate, but i didn't get very far. I assume you got the canvas ($base.fm.cv) stored in a variable, cv, say, and hull similarly. Then my first guess would be tkcreate(cv, window, 0, 0, anchor=nw, window=hull, width=10, height=5, tag= window) Works fine, thank you for your fast help. Best regards, Thomas Stabla __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
[R] setValidity and initialize method conflict ?
Hello, i am using classes and want to force an validity check when an object is created. platform i686-pc-linux-gnu arch i686 os linux-gnu system i686, linux-gnu status major1 minor7.1 year 2003 month06 day 16 language R Following piece of code works fine, just like I expected it to -- setClass(Foo, representation(woo = numeric)) [1] Foo validFooObject - function(object) { + if([EMAIL PROTECTED] 0) return(TRUE) + else return(warning: negative value for woo)} setValidity(Foo, validFooObject) [1] Foo new(Foo, woo = 1) # all is fine An object of class Foo Slot woo: [1] 1 new(Foo, woo = -1) # ok, negative value Error in validObject(.Object) : Invalid Foo object: warning: negative value for woo - Now, if i define a initialize method for Foo, things change (R had been restarted) - setClass(Foo, representation(woo = numeric)) [1] Foo validFooObject - function(object) { + if([EMAIL PROTECTED] 0) return(TRUE) + else return(warning: negative value for woo)} setValidity(Foo, validFooObject) [1] Foo setMethod(initialize, Foo, function(.Object, woo){ + [EMAIL PROTECTED] = woo + .Object}) [1] initialize new(Foo, woo = 1) # all is fine An object of class Foo Slot woo: [1] 1 new(Foo, woo = -1) # ! no warning, object created! An object of class Foo Slot woo: [1] -1 - How do i force an validity check, when an initalize method has been set? Thanks for your help. Greetings, Thomas Stabla setClass(Foo, representation(woo = numeric)) validFooObject - function(object) { if([EMAIL PROTECTED] 0) return(TRUE) else return(warning: negative value for woo)} setValidity(Foo, validFooObject) new(Foo, woo = 1) # all is fine new(Foo, woo = -1) # ok, negative value ### setClass(Foo, representation(woo = numeric)) validFooObject - function(object) { if([EMAIL PROTECTED] 0) return(TRUE) else return(warning: negative value for woo)} setValidity(Foo, validFooObject) setMethod(initialize, Foo, function(.Object, woo){ [EMAIL PROTECTED] = woo .Object}) new(Foo, woo = 1) # all is fine new(Foo, woo = -1) # ! no warning message is produced ! __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help