1
$\begingroup$

I have a Manipulate function that has two other functions inside of it, it has two sliders that each take in a value and show a bar on a graph from each input. Each value is shown on the chart as part of the maximum value. So the first bar shows maximum - value1, the second bar shows maximum - (value1 - value2). The output from the second inner function changes if either value changes, but the value from the first inner function only changes if the first value changes.

In my code, when I change the value on the first slider it works as expected (first image), but when I change the value on the second slider, the bar for that value disappears, and the function doesn't seem to be running because if I am outputting the value of the function as a label, it changes to the function name (second image). (In my code example below, sometimes it works as expected, sometimes one bar disappears, sometimes both bars disappear)

Before moving sliders, working as expected

After moving second slider, second bar disappears

Here is a simplified example of the code structure. The differences with my full code is that the inner functions run the number through a trained neural net to give their output, and the there's stylistic design around it. However, removing the code from all style information did not make a difference.

Manipulate[
 (* functions *)
 f1[x_Integer] := x;
 f2[x_Integer] := x - f1val;
 (* display *)
 BarChart[
  {{f1[f1val], 25 - f1[f1val]}, {f2[f2val], 25 - f2[f2val]}},
  ChartLayout -> "Stacked"
  ],
 (* sliders *)
 {{f1val, 7}, 0, 25, 1},
 {{f2val, 12}, 0, 25, 1}
 ]

I used this code last fall (November, I think) and it was working as expected, both bars were updating when I updated the related slider(s). Now when I run the code, I'm having this problem. I've tried on multiple computers, and the code ran on both these computers previously, so I don't think the issue is the computer. I cannot update this particular code to version 12 because some things I used in other parts of the code have changed too much to make it plausible. Any suggestions of what's going wrong and/or how to fix it?

$\endgroup$
0

1 Answer 1

2
$\begingroup$

This is not an answer. It is a comment on the question that far too long to be posted as an ordinary comment.

I am using V11.3 and can not reproduce your problem from the example code you post. I think you have oversimplified the code to the point it can't produce the problem.

However, I can show you a better way to construct your Manipulate expression, so it will be more robust and efficient.

The main problem I see with your code is the the two functions, f1 and f2 are redefined every time the sliders (they are actually animator, but we can ignore that) are moved. In my code, I wrap the Manipulate expression with a DynamicModule expression and define the functions in the initialization option of the Manipulate expression. That insures that the function identifiers are localized as dynamic variables before the Manipulate expression is evaluated. It also insures that the functions are defined once and only once very early in the evaluation, which improves performance.

I also remove the check on the heads of the function arguments because they will be made hundreds or thousands of times as the sliders moved and the check is more expensive than it is worth. After all, you are controlling the values given to the functions with the sliders. You don't need to defend against what some random user is going to give them.

I have also added some options to the BarChart expression. They are cosmetic — they stabilize the y-scale of the chart, resulting in a truer picture of the actual relationship between the two bars. You can ignore these changes if you feel they no relevance to your real problem.

Here is the code for your consideration.

DynamicModule[{f1, f2},
  Manipulate[
    BarChart[
      {{f1[f1val], 25 - f1[f1val]}, {f2[f2val], 25 - f2[f2val]}}, 
      ChartLayout -> "Stacked",
      AxesOrigin -> {0, 0},
      PlotRange -> {Automatic, {-25, 50}}],
    {{f1val, 7}, 0, 25, 1, Appearance -> "Labeled"},
    {{f2val, 12}, 0, 25, 1, Appearance -> "Labeled"},
    Initialization :> (
      f1[x_] := x;
      f2[x_] := x - f1val;)],
  TrackedSymbols :> None]

manip

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.