# Is there a way to embed a data set in a notebook?

I have a data set (currently a table in a .csv file). I would like to share this notebook with colleagues either as a notebook, or (even better) as a CDF document. Is there a way to embed the data (on the order of a few megabytes) directly into the notebook?

• David Reiss Scientific Arts had a blog some years ago in which he outlined this very thing. However, he seems to have taken this part of his website down. – dwa Apr 11 '15 at 4:37
• @dwa I agree with belisarius, therefore I converted your post to a comment. – Mr.Wizard Apr 11 '15 at 6:50
• Possible duplicate?: (11891) – Mr.Wizard Apr 11 '15 at 6:51

One possibility would be to create a notebook cell containing the imported data in Compressed form:

testData = RandomReal[{0, 1}, {100, 100}];
Export["file.csv", testData];

f = Compress[Import["file.csv"]];
NotebookWrite[EvaluationNotebook[],
Cell[BoxData[RowBox[{"mydata", "=", ToBoxes[f], ";"}]], "Input"]]


Here, I created a test file and imported it. I store a compressed version in f. The NotebookWrite command inserts the string contained in f as a new cell in your current notebook. Now you can delete the previous lines and keep only the newly generated cell. It starts with myData =, and assigns the compressed file contents to that symbol. When you send a notebook containing this cell to someone else, they can then just evaluate that cell and get the data by doing Uncompress on the result. In the next two lines, I verify that this works by comparing to the original data:

reconstituted = Uncompress[mydata];
reconstituted == testData

(* ==> True *)

• This does what I had asked originally. Thank you very much for your time in answering. – parselmouth Apr 13 '15 at 21:23