9
$\begingroup$

A nice feature of the mathematica documentation is the copious use of ExampleData in illustrating functionality. I would like to provide some example data for a package I have written, but there is apparently no guidance in the docs or on the net on how to do this. Consequently, I have two related questions:

  1. Is there a hook into the system's ExampleData function for providing your own example data? I'm thinking along the lines of the ImportExport`RegisterImport[] function that provides seamless integration of a custom import converter to the Import[] command.

  2. If not, what is a good way to provide example data from a custom package? Ideally, the data would be available from the documentation browser when someone tries one of the examples.

I would prefer to package the data together with the package code, rather than store it on a remote server and access it with Import["http://myserver.com/exampledata.zip"].

$\endgroup$
5
  • $\begingroup$ question 1: no. There is no such way. Concerning 2 note that one point of ExmapleData is that it is distributed withouth getting a new copy of Mathematica. If you'd like to store the data with your package why not make a myData[stuff], add a Format rule and some selectors? $\endgroup$
    – user21
    Commented Aug 16, 2012 at 18:04
  • 1
    $\begingroup$ I'm not sure if I fully understand this. 1) You want to deliver test data for your package 2) You want to package it with your package code. So, why don't you put this data in the same directory and Import it from there? Import[FileNameJoin[{NotebookDirectory[], "testfile.xls"}]] or so. What would an ExampleData version do more than that? $\endgroup$ Commented Aug 16, 2012 at 21:12
  • $\begingroup$ @SjoerdC.deVries Well, with ExampleData, one does not need to know the location of the data, as in jVincent's answer below. With Import, you need to know the location of the data file... $\endgroup$
    – JxB
    Commented Aug 17, 2012 at 1:52
  • $\begingroup$ ...and that can be inconvenient when you want to access a canonical or standard example of data associated with your package. When I'm learning a new function from the docs, I often open a new (unsaved) notebook, get some data with ExampleData, and start playing. Is there a standard directory for storing data in a package, where Mma does not need to know the path? (I see $UserBaseDirectory<>"/ApplicationData", perhaps there?) $\endgroup$
    – JxB
    Commented Aug 17, 2012 at 2:02
  • 1
    $\begingroup$ As long as you put your data in the same folder as your notebook the above code would work wherever you would place that directory. jVincent's solution stores all the examples in your package code and they will all be in main memory after loading the package. Doesn't sound ideal to me, particularly if it would involve large data sets such as 3D objects. $\endgroup$ Commented Aug 17, 2012 at 6:01

1 Answer 1

11
$\begingroup$

You could simply add new definitions for ExampleData in your package:

Unprotect[ExampleData];
ExampleData[{"JxBs Package", "RandomNumberSequence"}] := {7, 5, 8, 2, 1, 5, 3, 7, 9, 1};
Protect[ExampleData];

Which would make it available after loading your package

ExampleData[{"JxBs Package", "RandomNumberSequence"}]

{7, 5, 8, 2, 1, 5, 3, 7, 9, 1}

This is sort of cheating, but It will work for your code, just not with with ExampleData's listing of types, and properties.

$\endgroup$
3
  • 2
    $\begingroup$ why is it cheating? I think this is perfectly fine way to do it, except that some commands like ExampleData[] won't list your custom data, because it gets the list directly from DataPaclets`ExampleDataDump`Collections. Of course, one could easily modify it to handle these too. If the OP doesn't want to use a custom example data function, then this is a reasonable way out (although, standard warnings about modifying built-ins in a package apply and should probably be mentioned) $\endgroup$
    – rm -rf
    Commented Aug 16, 2012 at 18:24
  • $\begingroup$ @R.M I called it cheating because you end up with functionality which appears like ExampleData fully handles the new data, which it of cause doesn't, maybe a different term would be more appropriate. $\endgroup$
    – jVincent
    Commented Aug 16, 2012 at 19:16
  • $\begingroup$ Well, it depends on to what extents the OP is willing to go to mimic the built-in feature. For example, the no argument definition could be modified to, say, ExampleData[] := DataPaclets`ExampleDataDump`Collections ~Join~ {"JxBs Package"} // Sort and so on... Anyway, I think we both agree that this is a reasonable way to go for the OP :) $\endgroup$
    – rm -rf
    Commented Aug 16, 2012 at 19:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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