Best Way to Initialize Empty DataSet with Column Types

What's the best way to create an empty DataSet with the column types set?

Possible ideas

myDataset = system.dataset.toDataSet(['intCol', 'strCol'], [[1, 'a']])
myDataset = system.dataset.clearDataset(myDataset)
myDataset = system.dataset.toDataSet([], [])
myDataset = system.dataset.addColumn(myDataset, 0, [], 'intCol', int)
myDataset = system.dataset.addColumn(myDataset, 1, [], 'strCol', str)

I know DataSets are designed to be immutable, which is why I don't like either of these methods.

  • Is there a better way to do this?
  • Have I already found the best way to do this?
  • Might this be an indication that I've done something else wrong along the way?
    • Maybe I just shouldn't be trying to initialize DataSets at all?

If you truly need an empty dataset, then use a DatasetBuilder.

Honestly though, without knowing exactly what you're trying to accomplish, I would say that I have never had a need in 7 years to "initialize" an empty dataset.

2 Likes

The project I'm working with has some session properties that are sometimes set to a dataset, sometimes set to a string. On loading a relevant page, I want that session prop to be an empty dataset with the appropriate columns and types preset. I thought it'd be easier to just create one from scratch instead of ensuring that prop isn't set to a string from anywhere else in the project.

Writing this down explicitly is making me second guess that decision lol. But that was the motivation for this post.

For future reference:

I found an even simpler way to create a dataset the way I want to. There's a constructor on the BasicDataset class that has exactly what I need:

from com.inductiveautomation.ignition.common import BasicDataset

myDataset = BasicDataset(['intCol', 'strCol'], [int, str])

From the BasicDataset documentation.

1 Like

If you want to use a binding, my unionAll() expression function can do this without jython.