Local variables in DynamicModule affected by outside evaluationBasic Dynamic Interface Programming that returns variables to nb functionsRefresh dynamic variable when used with GetDynamicModule issues with Refresh, double evaluation, and CDF Player previewHow to make a notebook take action whenever a particular symbol is defined or redefined?How can I change a Dynamic object's state when the kernel quits?$CellContext issue while trying to export the current output of a ManipulatePreventing the content of a CDF report from being overwritten when multiple reports are open at onceDynamic Plot of non-linear function within DynamicModule yields dynamically changing constant line instead of expected behaviorDynamicModule not working when additional code is includedRunning a DynamicModule outside the Notebook containg the DynamicModule

Ribbon Cable Cross Talk - Is there a fix after the fact?

Alexandrov's generalization of Cauchy's rigidity theorem

Who wrote “A writer only begins a book. A reader finishes it.”?

What is to the west of Westeros?

Why is this integration method not valid?

Moons and messages

Complications of displaced core material?

Are there historical examples of audiences drawn to a work that was "so bad it's good"?

How to escape dependency hell?

How does Dreadhorde Arcanist interact with split cards?

switching alignment

Is a world with one country feeding everyone possible?

Comparison of bool data types in C++

Was this scene in S8E06 added because of fan reactions to S8E04?

Did significant numbers of Japanese officers escape prosecution during the Tokyo Trials?

What did the 'turbo' button actually do?

How do you earn the reader's trust?

How to write numbers and percentage?

How does the Earth's center produce heat?

I want to ask company flying me out for office tour if I can bring my fiance

Is keeping the forking link on a true fork necessary (Github/GPL)?

Why does Bran want to find Drogon?

Are there any German nonsense poems (Jabberwocky)?

Are runways booked by airlines to land their planes?



Local variables in DynamicModule affected by outside evaluation


Basic Dynamic Interface Programming that returns variables to nb functionsRefresh dynamic variable when used with GetDynamicModule issues with Refresh, double evaluation, and CDF Player previewHow to make a notebook take action whenever a particular symbol is defined or redefined?How can I change a Dynamic object's state when the kernel quits?$CellContext issue while trying to export the current output of a ManipulatePreventing the content of a CDF report from being overwritten when multiple reports are open at onceDynamic Plot of non-linear function within DynamicModule yields dynamically changing constant line instead of expected behaviorDynamicModule not working when additional code is includedRunning a DynamicModule outside the Notebook containg the DynamicModule













1












$begingroup$


I was trying to use DynamicModule to create a Input cell that contains a variable assignment, where the variable name is given by a InputField. A minimal example is:



createExample[] := DynamicModule[name = Null,
Column[
InputField[Dynamic[name]],
Button["OK",
With[name1 = name,
CellPrint[ExpressionCell[
Defer[name1 = 99999]
, "Input"]]]
]
]
]


Running createExample[] will give



1



Enter a variable name called var1 and click OK button
2



However, when I evaluate this assignment, the InputField also changes
3



My question is, how to avoid the change of the variable inside the DynamicModule.










share|improve this question









$endgroup$
















    1












    $begingroup$


    I was trying to use DynamicModule to create a Input cell that contains a variable assignment, where the variable name is given by a InputField. A minimal example is:



    createExample[] := DynamicModule[name = Null,
    Column[
    InputField[Dynamic[name]],
    Button["OK",
    With[name1 = name,
    CellPrint[ExpressionCell[
    Defer[name1 = 99999]
    , "Input"]]]
    ]
    ]
    ]


    Running createExample[] will give



    1



    Enter a variable name called var1 and click OK button
    2



    However, when I evaluate this assignment, the InputField also changes
    3



    My question is, how to avoid the change of the variable inside the DynamicModule.










    share|improve this question









    $endgroup$














      1












      1








      1





      $begingroup$


      I was trying to use DynamicModule to create a Input cell that contains a variable assignment, where the variable name is given by a InputField. A minimal example is:



      createExample[] := DynamicModule[name = Null,
      Column[
      InputField[Dynamic[name]],
      Button["OK",
      With[name1 = name,
      CellPrint[ExpressionCell[
      Defer[name1 = 99999]
      , "Input"]]]
      ]
      ]
      ]


      Running createExample[] will give



      1



      Enter a variable name called var1 and click OK button
      2



      However, when I evaluate this assignment, the InputField also changes
      3



      My question is, how to avoid the change of the variable inside the DynamicModule.










      share|improve this question









      $endgroup$




      I was trying to use DynamicModule to create a Input cell that contains a variable assignment, where the variable name is given by a InputField. A minimal example is:



      createExample[] := DynamicModule[name = Null,
      Column[
      InputField[Dynamic[name]],
      Button["OK",
      With[name1 = name,
      CellPrint[ExpressionCell[
      Defer[name1 = 99999]
      , "Input"]]]
      ]
      ]
      ]


      Running createExample[] will give



      1



      Enter a variable name called var1 and click OK button
      2



      However, when I evaluate this assignment, the InputField also changes
      3



      My question is, how to avoid the change of the variable inside the DynamicModule.







      dynamic






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 9 hours ago









      L.YuL.Yu

      1029




      1029




















          3 Answers
          3






          active

          oldest

          votes


















          3












          $begingroup$

          If you work with Boxes you don't have to worry about evaluations:



          createExample[] := DynamicModule[name="",
          Column[
          InputField[Dynamic[name],Boxes, ContinuousAction->True],
          Button[
          "OK",
          Dynamic @ CellPrint[Cell[BoxData[RowBox[name, "=", "99999"]], "Input"]]
          ]
          ]
          ]


          A short animation:



          enter image description here






          share|improve this answer









          $endgroup$












          • $begingroup$
            Wow thanks! this is exactly what I want. But I'm still curious how could global variable get into the DynamicModule in my version or if there is any caveat of using DynamicModule.
            $endgroup$
            – L.Yu
            8 hours ago



















          1












          $begingroup$

          In your code name1 is not localised within the scope of the DynamicModule. Try your experiments on these two variations.



           createExample2[] := 
          DynamicModule[name = Null,
          Column[Dynamic@name1, InputField[Dynamic[name]],
          Button["OK", With[name1 = name,
          CellPrint[ExpressionCell[Defer[name1 = 99999], "Input"]]]]]];

          createExample3[]:=DynamicModule[name = Null, name1,
          Column[Dynamic@name1, InputField[Dynamic[name]],
          Button["OK", With[name1 = name,
          CellPrint[ExpressionCell[Defer[name1 = 99999], "Input"]]]]]];





          share|improve this answer









          $endgroup$




















            1












            $begingroup$

            Here's a different way to inject the value of name into the cell:



            createExample[] := DynamicModule[name = Null,
            Column[
            InputField[Dynamic[name], Hold[Expression]],
            Button["OK",
            name /. Hold[v_] :> CellPrint[ExpressionCell[Defer[v = 99999], "Input"]]]
            ]
            ]


            The reason var1 becomes 99999 in the OP's code, is that the displayed contents of the InputField is the dynamically updated value of name. When name is set to var1, it displays the value of var1. Initially, it has no value, so we set var1. But when a value for var1 is set, the value is displayed. The above prevents that by specifying the type of the contents to be a held expression. "Held" means that it won't be evaluated. It also means that the value of name will be Hold[var1] when var1 is entered into the field. To handle this, I opted for the alternative method of injecting the user's input into the Set command in the cell.






            share|improve this answer









            $endgroup$













              Your Answer








              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "387"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f198727%2flocal-variables-in-dynamicmodule-affected-by-outside-evaluation%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              3












              $begingroup$

              If you work with Boxes you don't have to worry about evaluations:



              createExample[] := DynamicModule[name="",
              Column[
              InputField[Dynamic[name],Boxes, ContinuousAction->True],
              Button[
              "OK",
              Dynamic @ CellPrint[Cell[BoxData[RowBox[name, "=", "99999"]], "Input"]]
              ]
              ]
              ]


              A short animation:



              enter image description here






              share|improve this answer









              $endgroup$












              • $begingroup$
                Wow thanks! this is exactly what I want. But I'm still curious how could global variable get into the DynamicModule in my version or if there is any caveat of using DynamicModule.
                $endgroup$
                – L.Yu
                8 hours ago
















              3












              $begingroup$

              If you work with Boxes you don't have to worry about evaluations:



              createExample[] := DynamicModule[name="",
              Column[
              InputField[Dynamic[name],Boxes, ContinuousAction->True],
              Button[
              "OK",
              Dynamic @ CellPrint[Cell[BoxData[RowBox[name, "=", "99999"]], "Input"]]
              ]
              ]
              ]


              A short animation:



              enter image description here






              share|improve this answer









              $endgroup$












              • $begingroup$
                Wow thanks! this is exactly what I want. But I'm still curious how could global variable get into the DynamicModule in my version or if there is any caveat of using DynamicModule.
                $endgroup$
                – L.Yu
                8 hours ago














              3












              3








              3





              $begingroup$

              If you work with Boxes you don't have to worry about evaluations:



              createExample[] := DynamicModule[name="",
              Column[
              InputField[Dynamic[name],Boxes, ContinuousAction->True],
              Button[
              "OK",
              Dynamic @ CellPrint[Cell[BoxData[RowBox[name, "=", "99999"]], "Input"]]
              ]
              ]
              ]


              A short animation:



              enter image description here






              share|improve this answer









              $endgroup$



              If you work with Boxes you don't have to worry about evaluations:



              createExample[] := DynamicModule[name="",
              Column[
              InputField[Dynamic[name],Boxes, ContinuousAction->True],
              Button[
              "OK",
              Dynamic @ CellPrint[Cell[BoxData[RowBox[name, "=", "99999"]], "Input"]]
              ]
              ]
              ]


              A short animation:



              enter image description here







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 8 hours ago









              Carl WollCarl Woll

              79.7k3102207




              79.7k3102207











              • $begingroup$
                Wow thanks! this is exactly what I want. But I'm still curious how could global variable get into the DynamicModule in my version or if there is any caveat of using DynamicModule.
                $endgroup$
                – L.Yu
                8 hours ago

















              • $begingroup$
                Wow thanks! this is exactly what I want. But I'm still curious how could global variable get into the DynamicModule in my version or if there is any caveat of using DynamicModule.
                $endgroup$
                – L.Yu
                8 hours ago
















              $begingroup$
              Wow thanks! this is exactly what I want. But I'm still curious how could global variable get into the DynamicModule in my version or if there is any caveat of using DynamicModule.
              $endgroup$
              – L.Yu
              8 hours ago





              $begingroup$
              Wow thanks! this is exactly what I want. But I'm still curious how could global variable get into the DynamicModule in my version or if there is any caveat of using DynamicModule.
              $endgroup$
              – L.Yu
              8 hours ago












              1












              $begingroup$

              In your code name1 is not localised within the scope of the DynamicModule. Try your experiments on these two variations.



               createExample2[] := 
              DynamicModule[name = Null,
              Column[Dynamic@name1, InputField[Dynamic[name]],
              Button["OK", With[name1 = name,
              CellPrint[ExpressionCell[Defer[name1 = 99999], "Input"]]]]]];

              createExample3[]:=DynamicModule[name = Null, name1,
              Column[Dynamic@name1, InputField[Dynamic[name]],
              Button["OK", With[name1 = name,
              CellPrint[ExpressionCell[Defer[name1 = 99999], "Input"]]]]]];





              share|improve this answer









              $endgroup$

















                1












                $begingroup$

                In your code name1 is not localised within the scope of the DynamicModule. Try your experiments on these two variations.



                 createExample2[] := 
                DynamicModule[name = Null,
                Column[Dynamic@name1, InputField[Dynamic[name]],
                Button["OK", With[name1 = name,
                CellPrint[ExpressionCell[Defer[name1 = 99999], "Input"]]]]]];

                createExample3[]:=DynamicModule[name = Null, name1,
                Column[Dynamic@name1, InputField[Dynamic[name]],
                Button["OK", With[name1 = name,
                CellPrint[ExpressionCell[Defer[name1 = 99999], "Input"]]]]]];





                share|improve this answer









                $endgroup$















                  1












                  1








                  1





                  $begingroup$

                  In your code name1 is not localised within the scope of the DynamicModule. Try your experiments on these two variations.



                   createExample2[] := 
                  DynamicModule[name = Null,
                  Column[Dynamic@name1, InputField[Dynamic[name]],
                  Button["OK", With[name1 = name,
                  CellPrint[ExpressionCell[Defer[name1 = 99999], "Input"]]]]]];

                  createExample3[]:=DynamicModule[name = Null, name1,
                  Column[Dynamic@name1, InputField[Dynamic[name]],
                  Button["OK", With[name1 = name,
                  CellPrint[ExpressionCell[Defer[name1 = 99999], "Input"]]]]]];





                  share|improve this answer









                  $endgroup$



                  In your code name1 is not localised within the scope of the DynamicModule. Try your experiments on these two variations.



                   createExample2[] := 
                  DynamicModule[name = Null,
                  Column[Dynamic@name1, InputField[Dynamic[name]],
                  Button["OK", With[name1 = name,
                  CellPrint[ExpressionCell[Defer[name1 = 99999], "Input"]]]]]];

                  createExample3[]:=DynamicModule[name = Null, name1,
                  Column[Dynamic@name1, InputField[Dynamic[name]],
                  Button["OK", With[name1 = name,
                  CellPrint[ExpressionCell[Defer[name1 = 99999], "Input"]]]]]];






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 4 hours ago









                  Emerson WillardEmerson Willard

                  663




                  663





















                      1












                      $begingroup$

                      Here's a different way to inject the value of name into the cell:



                      createExample[] := DynamicModule[name = Null,
                      Column[
                      InputField[Dynamic[name], Hold[Expression]],
                      Button["OK",
                      name /. Hold[v_] :> CellPrint[ExpressionCell[Defer[v = 99999], "Input"]]]
                      ]
                      ]


                      The reason var1 becomes 99999 in the OP's code, is that the displayed contents of the InputField is the dynamically updated value of name. When name is set to var1, it displays the value of var1. Initially, it has no value, so we set var1. But when a value for var1 is set, the value is displayed. The above prevents that by specifying the type of the contents to be a held expression. "Held" means that it won't be evaluated. It also means that the value of name will be Hold[var1] when var1 is entered into the field. To handle this, I opted for the alternative method of injecting the user's input into the Set command in the cell.






                      share|improve this answer









                      $endgroup$

















                        1












                        $begingroup$

                        Here's a different way to inject the value of name into the cell:



                        createExample[] := DynamicModule[name = Null,
                        Column[
                        InputField[Dynamic[name], Hold[Expression]],
                        Button["OK",
                        name /. Hold[v_] :> CellPrint[ExpressionCell[Defer[v = 99999], "Input"]]]
                        ]
                        ]


                        The reason var1 becomes 99999 in the OP's code, is that the displayed contents of the InputField is the dynamically updated value of name. When name is set to var1, it displays the value of var1. Initially, it has no value, so we set var1. But when a value for var1 is set, the value is displayed. The above prevents that by specifying the type of the contents to be a held expression. "Held" means that it won't be evaluated. It also means that the value of name will be Hold[var1] when var1 is entered into the field. To handle this, I opted for the alternative method of injecting the user's input into the Set command in the cell.






                        share|improve this answer









                        $endgroup$















                          1












                          1








                          1





                          $begingroup$

                          Here's a different way to inject the value of name into the cell:



                          createExample[] := DynamicModule[name = Null,
                          Column[
                          InputField[Dynamic[name], Hold[Expression]],
                          Button["OK",
                          name /. Hold[v_] :> CellPrint[ExpressionCell[Defer[v = 99999], "Input"]]]
                          ]
                          ]


                          The reason var1 becomes 99999 in the OP's code, is that the displayed contents of the InputField is the dynamically updated value of name. When name is set to var1, it displays the value of var1. Initially, it has no value, so we set var1. But when a value for var1 is set, the value is displayed. The above prevents that by specifying the type of the contents to be a held expression. "Held" means that it won't be evaluated. It also means that the value of name will be Hold[var1] when var1 is entered into the field. To handle this, I opted for the alternative method of injecting the user's input into the Set command in the cell.






                          share|improve this answer









                          $endgroup$



                          Here's a different way to inject the value of name into the cell:



                          createExample[] := DynamicModule[name = Null,
                          Column[
                          InputField[Dynamic[name], Hold[Expression]],
                          Button["OK",
                          name /. Hold[v_] :> CellPrint[ExpressionCell[Defer[v = 99999], "Input"]]]
                          ]
                          ]


                          The reason var1 becomes 99999 in the OP's code, is that the displayed contents of the InputField is the dynamically updated value of name. When name is set to var1, it displays the value of var1. Initially, it has no value, so we set var1. But when a value for var1 is set, the value is displayed. The above prevents that by specifying the type of the contents to be a held expression. "Held" means that it won't be evaluated. It also means that the value of name will be Hold[var1] when var1 is entered into the field. To handle this, I opted for the alternative method of injecting the user's input into the Set command in the cell.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 4 hours ago









                          Michael E2Michael E2

                          152k12208492




                          152k12208492



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Mathematica Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              Use MathJax to format equations. MathJax reference.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f198727%2flocal-variables-in-dynamicmodule-affected-by-outside-evaluation%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Log på Navigationsmenu

                              Wonderful Copenhagen (sang) Eksterne henvisninger | NavigationsmenurSide på frankloesser.comWonderful Copenhagen

                              Detroit Tigers Spis treści Historia | Skład zespołu | Sukcesy | Członkowie Baseball Hall of Fame | Zastrzeżone numery | Przypisy | Menu nawigacyjneEncyclopedia of Detroit - Detroit TigersTigers Stadium, Detroit, MITigers Timeline 1900sDetroit Tigers Team History & EncyclopediaTigers Timeline 1910s1935 World Series1945 World Series1945 World Series1984 World SeriesComerica Park, Detroit, MI2006 World Series2012 World SeriesDetroit Tigers 40-Man RosterDetroit Tigers Coaching StaffTigers Hall of FamersTigers Retired Numberse