2

I'm developing a workflow where user-submitted data are validated and some preliminary statistical tests are run, and then the resulting workspace is sent to a statistician for further analysis.

In some scenarios, the RData file will be sent to a statistician who was, until then, not familiar with the project. I would like for them to be able to open the RData file and be shown a message summarizing the project, telling them what is contained in the workspace they opened, and a URL where they can look for more information.

The catch is, I am trying to rely as little as possible on the statistician running commands or loading packages. The best I've been able to come up with is this (during creating of the RData file):

library(stringr);
## the message, wrap it to 40 char because we're being cautious
message <- strwrap("Blah blah this is a message, look at this url http://foo.bar, here is a file listing. To see this information again, please type 'README()'",40);

## override ls 
ls<-README<-function(...) {
  ## prints neatly wrapped message with no line numbers
  cat(paste0(message,collapse='\n'),'\n\n');
  ## capture the execution environment
  pf<-parent.frame(); 
  ## print the requested ls output
  print(base:::ls(...));
  ## if README was invoked as ls, clean it up
  if("ls" %in% base:::ls(pf)) pf$ls<-NULL;}

## generate the RData file, where FOO, BAR, BAZ are, e.g. 
## fitted models or data frames
save(ls,README,message,FOO,BAR,BAZ,file='your_output.RData');

Then your_output.RData is the file the statistician will open from their R session.

This is sub-optimal because it assumes that the statistician will type "ls" at console rather than having some kind of interface (e.g. ESS) do it for them (and possibly error out). Also, it feels sketchy to mess with their base functions even if I do it in the cause of user-friendliness.

Another thing I thought of is having README be a custom class and saving an S3 print method along with it in the RData file, but not everyone will immediately print a full object to their console. I'd probably try class() and head() on it and who knows what else other people do first.

The documentation for the .First() command says (emphasis mine)...

A function ‘.First’ (and ‘.Last’) can be defined in appropriate ‘.Rprofile’ or ‘Rprofile.site’ files or have been saved in ‘.RData’.

...but if I open an RData file from an already running session, .First() does not execute.

Does anybody know of a way to display a one-time message when someone opens an RData file with minimal user input, preferably none?

bokov
  • 3,444
  • 2
  • 31
  • 49
  • I would instead overwrite `load` to check for a message-object that matches the name of the loaded object and cat()s the contents. – IRTFM Oct 14 '13 at 19:12
  • Ah, but by that time they may have already invoked the base version of `load` to load the workspace in the first place, and there's no reason for them to run load again... – bokov Oct 14 '13 at 19:36
  • 1
    @DWin - but doesn't that depend on distributing the overloaded `load` function to all possible recipients and making sure it's part of their default environment? – Carl Witthoft Oct 14 '13 at 19:37
  • It certainly does. I assumed that was part of the plan, given that the original plan was to overwrite `ls` or do `` to an ESS setup. – IRTFM Oct 14 '13 at 19:52
  • Except `ls` can be distributed inside `your_output.RData`, while `load` would have to be distributed separately for it to get called when `your_output.RData` is loaded. – bokov Oct 14 '13 at 20:13

0 Answers0