10
$\begingroup$

I am attempting to use TableView (added in version 12.1) interactively to edit data and save the edited data. The following works.

tv = TableView[{{"Paris", "France", 2200000}, {"New York", "US", 8500000}, {"Beijing", "China", 21700000}}, AllowedDimensions -> {3, 3}]

I evaluate this command to produce the editable TableView object. I make the necessary edits to the cells in this displayed "Out" table and then evaluate this "Out" object (which turns it into an "In" object and produces a new "Out" object). The new "Out" object has the corrected values, which I save using

tv2 = Setting[%]

However, I have the impression that there must be a better way to do this using TableView[Dynamic[tv]]. However when I try this I get an error message "Cannot assign to raw object".

As trivial as this may be, can someone give me an example of how to correctly use TableView to interactively edit and save a data matrix?

$\endgroup$
1
  • 7
    $\begingroup$ You need data = {{"Paris", "France", 2200000}, ...}; TableView[Dynamic[data]] $\endgroup$
    – b3m2a1
    Mar 19, 2020 at 17:44

2 Answers 2

2
$\begingroup$

Code below might help. Asked a similar question here. Code might be a little more complicated than it needs to be. Code let's the user edit points with TableView or with Locator Points.

enter image description here

makeXYplots[xy_] := Module[{}, ListLinePlot[xy
   , PlotRange -> {{-0.05, 1.01}, {-0.05, 1.05}}
   , Frame -> True
   , GridLines -> Automatic
   , ImageSize -> 600
   , Epilog -> {Red, AbsolutePointSize[4], Point[xy] }] ]
makeXYtable[xy_] := Module[{}, TableForm[xy]] 
initPts[n_Integer] := Module[{xVals, yVals },
  xVals = Sort@Round[RandomReal[{0, 1}, n], 0.01];
  yVals = Sort@Round[RandomReal[{0, 1}, n], 0.01];
   Transpose[{xVals, yVals}]
  ]
exportPoints[pts_] := Module[{fileName,  dir},
  dir = If[ Quiet@NotebookDirectory[] =!= $Failed, 
    NotebookDirectory[], $HomeDirectory];
  fileName = SystemDialogInput["FileSave", dir];
  If[fileName =!= $Canceled , Export[fileName, pts ], {} ]
  ]

testManip = DynamicModule[{xyData, iCount, status, lcpBackground},
   Manipulate[
    iCount = iUpdate ;
    lcpBackground = Dynamic[makeXYplots[xyData]] ;
    If[inputType == 0,
     Row[{LocatorPane[ Dynamic[xyData], lcpBackground, 
        LocatorAutoCreate -> True, ContinuousAction -> False]
       , Spacer[10]
       , Dynamic[Framed[ makeXYtable[xyData]]]
        }] ,
     Row[{Dynamic[makeXYplots[xyData]]
       , Spacer[10]
       , TableView[Dynamic[xyData] , Number, 
        AllowedDimensions -> {2, Dynamic[Length@xyData]} ]
        }] 
     ]
    , Delimiter(*=======================*)
    , Style["Calculation Control", Black, Italic,  12]
    , {{inputType, 0, "Input Type"}, {0 -> "Locator Points", 
      1 -> "Table"}, ControlType -> RadioButtonBar}
    , Button["Update"
     , status = Style["updating ...", Darker@Blue, 14, Bold]; 
     FinishDynamic[]; iUpdate = iUpdate + 1;
     , Method -> "Queued"
     ]
    , Delimiter (*=======================*)
    , Style["Export points", Black, Italic,  12]
    , Button["Export"
     , exportPoints[xyData];
     FinishDynamic[];
     iUpdate = iUpdate + 1;
     , Method -> "Queued"
     ]
    , Delimiter (*=======================*)
    , Style["Reset", Black, Italic,  12]
    , Button["Reset"
     , status = Style["updating ...", Darker@Blue, 14, Bold];
     xyData = initPts[npts];
     iCount = 0;
     iUpdate = 0;
     status = Style["Ready to Start", Darker@ Green, Bold];
     FinishDynamic[];
     iUpdate = iUpdate + 1;
     , Method -> "Queued"
     ]
    , {{npts, 7, "N pts"}, 1, 20, 1, Appearance -> "Labeled"}
    (*future use:  track iUpdate to trigger long calculations*)
    , {iUpdate, None}
    , ControlPlacement :> {Left, Left}
    , TrackedSymbols :> {iUpdate, inputType}
    , Initialization :> (
      xyData = initPts[7];
      iCount = 0;
      iUpdate = 0;
      status = Style["Ready to Start", Darker@ Green, Bold];
      )
    , SynchronousUpdating -> False
    ]
   ];

Button["start Test", CellPrint@testManip ]
$\endgroup$
0
$\begingroup$

If you run the command given by -b3m2a1. Add a

Dynamic[data]

If you edit the Number for China to Blank in the TableView, that is backspace over 21 700 000.

The Dynamic[data] on my system running on MacOS shows

{{Paris, France, 2 200 000}, {New York, US, 8 500 000}, {Beijing, China, 2}}

Clearly the 2 is wrong, I have had other issues with this Function where it does not play nicely with Dynamic, unfortunately as it stands I think its unusable if used together with Dynamic.

$\endgroup$

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.