Problem with scoping a variable value

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem with scoping a variable value

Tolga Uzuner
Hi there,

I have a function which has a variable called show as an input:

richardson.grad <- function(func, x, d=0.01, eps=1e-4, r=6, show=F){
# do some things
   if(show) {

        cat("\n","first order approximations", "\n")      
        print(a.mtr, 12)


    }
#do more things and return
}

The show variable is being used as a flag to show intermediate results.

Interestingly enough, I have downloaded a package recently which defines
the show variable as a function:

 > show
standardGeneric for "show" defined from package "methods"

function (object)
standardGeneric("show")
<environment: 01676F7C>
Methods may be defined for arguments: object

 >

Now, all of a sudden, the function I had defined earlier is scoping up
to this new value, and is thus not working:

 > richardson.grad(function(x) x^2,2)
Error in if (show) { : argument is not interpretable as logical
 >

I could always redefine show in richardson.grad to be something else but
something seems wrong: why is richardson.grad not looking up show's
value in the  function ? How would I fix this ?

Thanks in advance,
Tolga

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Problem with scoping a variable value

Gabor Grothendieck
Without a reproducible example one can only guess but
perhaps the problem is not show but that you have a variable
F.  Try writing out F as FALSE.


On 2/16/06, Tolga Uzuner <[hidden email]> wrote:

> Hi there,
>
> I have a function which has a variable called show as an input:
>
> richardson.grad <- function(func, x, d=0.01, eps=1e-4, r=6, show=F){
> # do some things
>   if(show) {
>
>        cat("\n","first order approximations", "\n")
>        print(a.mtr, 12)
>
>
>    }
> #do more things and return
> }
>
> The show variable is being used as a flag to show intermediate results.
>
> Interestingly enough, I have downloaded a package recently which defines
> the show variable as a function:
>
>  > show
> standardGeneric for "show" defined from package "methods"
>
> function (object)
> standardGeneric("show")
> <environment: 01676F7C>
> Methods may be defined for arguments: object
>
>  >
>
> Now, all of a sudden, the function I had defined earlier is scoping up
> to this new value, and is thus not working:
>
>  > richardson.grad(function(x) x^2,2)
> Error in if (show) { : argument is not interpretable as logical
>  >
>
> I could always redefine show in richardson.grad to be something else but
> something seems wrong: why is richardson.grad not looking up show's
> value in the  function ? How would I fix this ?
>
> Thanks in advance,
> Tolga
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Problem with scoping a variable value

Paul Roebuck
In reply to this post by Tolga Uzuner
On Thu, 16 Feb 2006, Tolga Uzuner wrote:

> I have a function which has a variable called show as an input:
>
> richardson.grad <- function(func, x, d=0.01, eps=1e-4, r=6, show=F){
> # do some things
>    if(show) {
>
>         cat("\n","first order approximations", "\n")
>         print(a.mtr, 12)
>
>
>     }
> #do more things and return
> }
>
> The show variable is being used as a flag to show intermediate results.
>
> Interestingly enough, I have downloaded a package recently which defines
> the show variable as a function:
>
>  > show
> standardGeneric for "show" defined from package "methods"
>
> function (object)
> standardGeneric("show")
> <environment: 01676F7C>
> Methods may be defined for arguments: object
>
>  >
>
> Now, all of a sudden, the function I had defined earlier is scoping up
> to this new value, and is thus not working:
>
>  > richardson.grad(function(x) x^2,2)
> Error in if (show) { : argument is not interpretable as logical
>  >
>
> I could always redefine show in richardson.grad to be something else but
> something seems wrong: why is richardson.grad not looking up show's
> value in the  function ? How would I fix this ?

You didn't spell out the logical value 'FALSE' which may
be causing your problem. Consider this alternative also...

richardson.grad <- function(func,
                            x,
                            d = 0.01,
                            eps = 1e-4,
                            r = 6,
                            verbose = getOption("verbose")) {
    ## do some things
    if (verbose) {
         cat("\n", "first order approximations:", "\n")
         print(a.mtr, 12)
    }
    ## do more things and return
}

----------------------------------------------------------
SIGSIG -- signature too long (core dumped)

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Problem with scoping a variable value

Tolga Uzuner
In reply to this post by Gabor Grothendieck
You are a star... that was it. Many thanks,
Tolga

-----Original Message-----
From: Gabor Grothendieck [mailto:[hidden email]]
Sent: 16 February 2006 20:24
To: Tolga Uzuner
Cc: [hidden email]
Subject: Re: [R] Problem with scoping a variable value

Without a reproducible example one can only guess but
perhaps the problem is not show but that you have a variable
F.  Try writing out F as FALSE.


On 2/16/06, Tolga Uzuner <[hidden email]> wrote:

> Hi there,
>
> I have a function which has a variable called show as an input:
>
> richardson.grad <- function(func, x, d=0.01, eps=1e-4, r=6, show=F){
> # do some things
>   if(show) {
>
>        cat("\n","first order approximations", "\n")
>        print(a.mtr, 12)
>
>
>    }
> #do more things and return
> }
>
> The show variable is being used as a flag to show intermediate results.
>
> Interestingly enough, I have downloaded a package recently which defines
> the show variable as a function:
>
>  > show
> standardGeneric for "show" defined from package "methods"
>
> function (object)
> standardGeneric("show")
> <environment: 01676F7C>
> Methods may be defined for arguments: object
>
>  >
>
> Now, all of a sudden, the function I had defined earlier is scoping up
> to this new value, and is thus not working:
>
>  > richardson.grad(function(x) x^2,2)
> Error in if (show) { : argument is not interpretable as logical
>  >
>
> I could always redefine show in richardson.grad to be something else but
> something seems wrong: why is richardson.grad not looking up show's
> value in the  function ? How would I fix this ?
>
> Thanks in advance,
> Tolga
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Problem with scoping a variable value

Philippe Grosjean
In reply to this post by Paul Roebuck
Paul Roebuck wrote:

> On Thu, 16 Feb 2006, Tolga Uzuner wrote:
>
>
>>I have a function which has a variable called show as an input:
>>
>>richardson.grad <- function(func, x, d=0.01, eps=1e-4, r=6, show=F){
>># do some things
>>   if(show) {
>>
>>        cat("\n","first order approximations", "\n")
>>        print(a.mtr, 12)
>>
>>
>>    }
>>#do more things and return
>>}
>>
>>The show variable is being used as a flag to show intermediate results.
>>
>>Interestingly enough, I have downloaded a package recently which defines
>>the show variable as a function:
>>
>> > show
>>standardGeneric for "show" defined from package "methods"
>>
>>function (object)
>>standardGeneric("show")
>><environment: 01676F7C>
>>Methods may be defined for arguments: object
>>
>> >
>>
>>Now, all of a sudden, the function I had defined earlier is scoping up
>>to this new value, and is thus not working:
>>
>> > richardson.grad(function(x) x^2,2)
>>Error in if (show) { : argument is not interpretable as logical
>> >
>>
>>I could always redefine show in richardson.grad to be something else but
>>something seems wrong: why is richardson.grad not looking up show's
>>value in the  function ? How would I fix this ?
>
>
> You didn't spell out the logical value 'FALSE' which may
> be causing your problem. Consider this alternative also...
>
> richardson.grad <- function(func,
>                             x,
>                             d = 0.01,
>                             eps = 1e-4,
>                             r = 6,
>                             verbose = getOption("verbose")) {
>     ## do some things
>     if (verbose) {
>          cat("\n", "first order approximations:", "\n")
>          print(a.mtr, 12)
>     }
>     ## do more things and return
> }

This is definitely the best solution, and Paul is right: you have to use
  FALSE in preference to F (FALSE is a reserved word, F is not in R).

Now, you should know that show() *is* a generic function (it is the
default method for S4 objects), and it is a very bad idea to use it as
named argument for functions. I would suggest to use Paul's suggestion
instead, or to change it as 'show.it = FALSE', as a minimum.
Best,

Philippe Grosjean

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html