21

I'm working on a project right now where I have been slowly accumulating a bunch of different variables from a bunch of different sources. Being a somewhat clever person, I created a different sub-directory for each under a main "original_data" directory, and included a .txt file with the URL and other descriptors of where I got the data from. Being an insufficiently clever person, these .txt files have no structure.

Now I am faced with the task of compiling a methods section which documents all the different data sources. I am willing to go through and add structure to the data, but then I would need to find or build a reporting tool to scan through the directories and extract the information.

This seems like something that ProjectTemplate would have already, but I can't seem to find that functionality there.

Does such a tool exist?

If it does not, what considerations should be taken into account to provide maximum flexibility? Some preliminary thoughts:

  1. A markup language should be used (YAML?)
  2. All sub-directories should be scanned
  3. To facilitate (2), a standard extension for a dataset descriptor should be used
  4. Critically, to make this most useful there needs to be some way to match variable descriptors with the name that they ultimately take on. Therefore either all renaming of variables has to be done in the source files rather than in a cleaning step (less than ideal), some code-parsing has to be done by the documentation engine to track variable name changes (ugh!), or some simpler hybrid such as allowing the variable renames to be specified in the markup file should be used.
  5. Ideally the report would be templated as well (e.g. "We pulled the [var] variable from [dset] dataset on [date]."), and possibly linked to Sweave.
  6. The tool should be flexible enough to not be overly burdensome. This means that minimal documentation would simply be a dataset name.
4
  • 1
    couple of thoughts. one, you should look at roxygen2 for documenting data-sets. two, it is quite easy to generalize ProjectTemplate to do what you seek, and I am working on a proof of concept, which I will post on github shortly.
    – Ramnath
    Nov 2, 2011 at 12:14
  • @Ramnath Thanks. Sounds awesome. I'd forgotten roxygen2 works for datasets, so I'll give that a look. Do you have a documentation format written up somewhere, even if the parser isn't done, so that I can avoid rewriting when your proof of concept comes out? Nov 2, 2011 at 23:50
  • I am not working on a new documentation format. roxygen2 is sufficient for most documentation requirements. The idea I am working on is to allow roxygen to be run on arbitrary directories, so that it can be used with a project that is not an R package.
    – Ramnath
    Nov 3, 2011 at 0:18
  • Ah, cool. I'll just do something similar to this then for now. stackoverflow.com/questions/2310409/…. Nov 3, 2011 at 1:26

1 Answer 1

16
+50

This is a very good question: people should be very concerned about all of the sequences of data collection, aggregation, transformation, etc., that form the basis for statistical results. Unfortunately, this is not widely practiced.

Before addressing your questions, I want to emphasize that this appears quite related to the general aim of managing data provenance. I might as well give you a Google link to read more. :) There are a bunch of resources that you'll find, such as the surveys, software tools (e.g. some listed in the Wikipedia entry), various research projects (e.g. the Provenance Challenge), and more.

That's a conceptual start, now to address practical issues:

I'm working on a project right now where I have been slowly accumulating a bunch of different variables from a bunch of different sources. Being a somewhat clever person, I created a different sub-directory for each under a main "original_data" directory, and included a .txt file with the URL and other descriptors of where I got the data from. Being an insufficiently clever person, these .txt files have no structure.

Welcome to everyone's nightmare. :)

Now I am faced with the task of compiling a methods section which documents all the different data sources. I am willing to go through and add structure to the data, but then I would need to find or build a reporting tool to scan through the directories and extract the information.

No problem. list.files(...,recursive = TRUE) might become a good friend; see also listDirectory() in R.utils.

It's worth noting that filling in a methods section on data sources is a narrow application within data provenance. In fact, it's rather unfortunate that the CRAN Task View on Reproducible Research focuses only on documentation. The aims of data provenance are, in my experience, a subset of reproducible research, and documentation of data manipulation and results are a subset of data provenance. Thus, this task view is still in its infancy regarding reproducible research. It might be useful for your aims, but you'll eventually outgrow it. :)

Does such a tool exist?

Yes. What are such tools? Mon dieu... it is very application-centric in general. Within R, I think that these tools are not given much attention (* see below). That's rather unfortunate - either I'm missing something, or else the R community is missing something that we should be using.

For the basic process that you've described, I typically use JSON (see this answer and this answer for comments on what I'm up to). For much of my work, I represent this as a "data flow model" (that term can be ambiguous, by the way, especially in the context of computing, but I mean it from a statistical analyses perspective). In many cases, this flow is described via JSON, so it is not hard to extract the sequence from JSON to address how particular results arose.

For more complex or regulated projects, JSON is not enough, and I use databases to define how data was collected, transformed, etc. For regulated projects, the database may have lots of authentication, logging, and more in it, to ensure that data provenance is well documented. I suspect that that kind of DB is well beyond your interest, so let's move on...

1. A markup language should be used (YAML?)

Frankly, whatever you need to describe your data flow will be adequate. Most of the time, I find it adequate to have good JSON, good data directory layouts, and good sequencing of scripts.

2. All sub-directories should be scanned

Done: listDirectory()

3. To facilitate (2), a standard extension for a dataset descriptor should be used

Trivial: ".json". ;-) Or ".SecretSauce" works, too.

4. Critically, to make this most useful there needs to be some way to match variable descriptors with the name that they ultimately take on. Therefore either all renaming of variables has to be done in the source files rather than in a cleaning step (less than ideal), some code-parsing has to be done by the documentation engine to track variable name changes (ugh!), or some simpler hybrid such as allowing the variable renames to be specified in the markup file should be used.

As stated, this doesn't quite make sense. Suppose that I take var1 and var2, and create var3 and var4. Perhaps var4 is just a mapping of var2 to its quantiles and var3 is the observation-wise maximum of var1 and var2; or I might create var4 from var2 by truncating extreme values. If I do so, do I retain the name of var2? On the other hand, if you're referring to simply matching "long names" with "simple names" (i.e. text descriptors to R variables), then this is something only you can do. If you have very structured data, it's not hard to create a list of text names matching variable names; alternatively, you could create tokens upon which string substitution could be performed. I don't think it's hard to create a CSV (or, better yet, JSON ;-)) that matches variable name to descriptor. Simply keep checking that all variables have matching descriptor strings, and stop once that's done.

5. Ideally the report would be templated as well (e.g. "We pulled the [var] variable from [dset] dataset on [date]."), and possibly linked to Sweave.

That's where others' suggestions of roxygen and roxygen2 can apply.

6. The tool should be flexible enough to not be overly burdensome. This means that minimal documentation would simply be a dataset name.

Hmm, I'm stumped here. :)

(*) By the way, if you want one FOSS project that relates to this, check out Taverna. It has been integrated with R as documented in several places. This may be overkill for your needs at this time, but it's worth investigating as an example of a decently mature workflow system.


Note 1: Because I frequently use bigmemory for large data sets, I have to name the columns of each matrix. These are stored in a descriptor file for each binary file. That process encourages the creation of descriptors matching variable names (and matrices) to descriptors. If you store your data in a database or other external files supporting random access and multiple R/W access (e.g. memory mapped files, HDF5 files, anything but .rdat files), you will likely find that adding descriptors becomes second nature.

4
  • +1 Nice answer. I shall add JSON on my list of things to learn about.
    – Andrie
    Nov 10, 2011 at 8:52
  • @Andrie What's very exciting is that you don't have much to learn! You already know R's lists, which, I believe, offer a superset of JSON capabilities (one might argue they can be isomorphic, I'm not sure). Check out my little demo in this answer. It will take about 3-5 minutes to learn everything you need to know by just trying out fromJSON and toJSON, which is the I/O part, since you already know lists & nesting of lists. Everything gets converted to a list.
    – Iterator
    Nov 10, 2011 at 13:33
  • (Continued) Looking at the options for fromJSON is useful because you can learn how to create named vectors, a la c(Tom = "cat", Jerry = "mouse"). Only gotcha is that TRUE and FALSE are stored in JSON in their lowercase forms.
    – Iterator
    Nov 10, 2011 at 13:34
  • Also of note is the DDI standard, which seems comprehensive (that could be a downside!). Not sure about R support. Feb 15, 2013 at 16:37

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.