1
$\begingroup$

I am trying to plot two smooth histograms, which I'm generating separately so I can chose their PlotStyle separately. When I run the code, it says I cannot combine the two graphic objects. What am I doing wrong?

Thanks, Sid

CloudDeploy[
 FormPage[{"k1" -> "Number", "n1" -> "Number", "n2" -> "Number", 
   "k2" -> "Number", "n3" -> "Number", "n4" -> "Number"},
  
  fig1 = SmoothHistogram[
     Table[Table[
       If[RandomReal[] > #k1, RandomChoice[{100, 90}], 
        RandomChoice[{80, 70, 60, 50, 40}]], #n1], #n2], 
     PlotStyle -> Red] &;
  fig2 = SmoothHistogram[
     Table[Table[
       If[RandomReal[] > #k2, RandomChoice[{100, 90}], 
        RandomChoice[{40, 30, 20, 10, 0}]], #n3], #n4] , 
     PlotStyle -> Gray] &;
  
  Show[{fig1, fig2}]]]
$\endgroup$
1
$\begingroup$

The second argument to FormPage should be a single function of the inputs in the first argument. So your code should be structured like:

CloudDeploy[
 FormPage[{"k1" -> "Number", "n1" -> "Number", "n2" -> "Number", 
   "k2" -> "Number", "n3" -> "Number", "n4" -> "Number"}, 
   (fig1 = SmoothHistogram[ <<stuff>> ];
    fig2 = SmoothHistogram[ <<stuff>> ];
    Show[{fig1, fig2}]) &
 ]
]

Note that there's only one &, after all the code in the second argument of FormPage.

I used () to make sure the & scoped all the code when constructing the function, but you could also use Block or Module:

Block[{fig1, fig2},
    fig1 = SmoothHistogram[ <<stuff>> ];
    fig2 = SmoothHistogram[ <<stuff>> ];
    Show[{fig1, fig2}]
]&

which might be nicer, especially for longer functions.

$\endgroup$
1
  • $\begingroup$ Thank you so much! This was very helpful. $\endgroup$ May 20 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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