boolean expressions involving sliders in manipulate

hwhite0's Avatar

hwhite0

17 Aug, 2012 09:05 PM

The following seems rather odd, and it is causing problems with a routine I have written using the manipuate fucntion, in which a slider is passed to a function whose action depends on the the resuklt of a boolean evaluation of the slider. Unfortunately it seems that expressions of the form

      slider == a.particular.number

always evalaute to false, no matter what the value of the slider is.

For example, in this code:

manipulate(
a=slider(-3,3,step=0.1,initial=1), {plot(1~1) a==0} )

you find that the response to the console is always FALSE, even when you set the a-slider to 0.

Is there a workaround? I'd like the expression to evaluate as TRUE when the slider does equal the specified value.

  1. Support Staff 2 Posted by Josh Paulson on 19 Aug, 2012 12:25 PM

    Josh Paulson's Avatar

    Homer,

    This becomes more apparent when you use the following function:

    manipulate(
      a=slider(-3,3,step=0.1,initial=1),
      {plot(1~1)
      print(a)
      a==a}
    )
    

    Notice that as you place the slider on "0", it will print the value as something very close to zero such as "1.665335e-16". This is just how R works and you'll need to use a different comparison operator. For example, the following takes this into account:

    manipulate(
      a=slider(-3,3,step=0.1,initial=1),
      {plot(1~1)
      print(a)
      all.equal(a,0)}
    )
    

    For more information, take a look at the docs for functions such as:

    > ?identical
    > ?all.equal
    

    Josh

  2. 3 Posted by hwhite0 on 20 Aug, 2012 12:48 PM

    hwhite0's Avatar

    Thanks, Josh,

     isTRUE(all.equal())
    

    as the condition in an if statement did exactly what I needed.

    Regards,
    Homer

  3. Support Staff 4 Posted by Josh Paulson on 20 Aug, 2012 12:55 PM

    Josh Paulson's Avatar

    Homer,

    Glad to help and all the best!

    Josh

  4. Josh Paulson closed this discussion on 20 Aug, 2012 12:55 PM.

Comments are currently closed for this discussion. You can start a new one.