I have a table with column headers and data. (The original table is much longer than the example is use to explain my problem here).

tableFull = {{"XXX_ID", "XXX_main", "XXX_post", "YYY_post", 
   "YYY_ID"}, {1, 2, 3, 4, 5}, {95, 294, 589, 2, 103}}

Table header:

tableHeaderShort = tableFull[[1]]

I want to dynamically select only a subset of columns based on column header names using "Dynamic", like so:

CheckboxBar[Dynamic[x], tableHeaderShort, Appearance -> "Vertical"]

The selected columns I check with:


which works fine. I look for the positions of the desired columns, which are also updated dynamically:

columnsToTake = 
 Dynamic[Position[tableHeaderShort, #] & /@ x // Flatten[#, 1] &]

I generate the subset of columns that I want to export. Again this is updated dynamically and works.

export = Dynamic[
   Extract[tableFull // Transpose, 
     Position[tableHeaderShort, #] & /@ x // Flatten[#, 1] &] // 

Finally, I want to export the desired table to a Microsoft Excel file.

Button["Export", Export["insert desired file path", export]]

The problem is the following: An Excel file is generated as desired, but the content of the file is the unevaluated output instead of the current dynamic state of the "export" variable:

screenshot of excel file containing the command above rather than the desired output

  • 2
    $\begingroup$ Use: Button["Export", Export["insert desired file path", Setting @ export]]` instead. $\endgroup$
    – Carl Woll
    Oct 3 '21 at 17:23
  • 2
    $\begingroup$ Don't set a variable like export equal to Dynamic[..]. It should be defined export = Extract[tableFull // Transpose, Position[tableHeaderShort, #] & /@ x // Flatten[#, 1] &] // Transpose; and then display (wherever you're displaying it) Dynamic[TableForm[export]]. You'll have to figure out any tweaks to make it work, since there's not enough information given for me to figure out if any are needed. The basic principle is to separate variables/data/computation from formatting. It makes it easier to compute with variables and data. $\endgroup$
    – Michael E2
    Oct 3 '21 at 18:33
  • $\begingroup$ Thank you Carl! "Setting[expr]" is the solution. According to the documentation (reference.wolfram.com/language/ref/Setting.html) it "replaces dynamic objects in expr by their current values. $\endgroup$
    – Niki
    Oct 4 '21 at 5:57

Your Answer

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

Browse other questions tagged or ask your own question.