Most portable way to embed a README message in an RData (R workspace) file

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'),'nn');
  ## 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 (eg 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?

链接地址: http://www.djcxy.com/p/38300.html

上一篇: 将Rdata的列表转换为R中的数据帧

下一篇: 大多数可移植的方式将README消息嵌入到RData(R workspace)文件中