Eines der Dinge, die ich häufig tue, ist das Verfolgen der Befehle, die zum Generieren von Daten und Objekten verwendet werden, und der Kommentar hat sich als nützliches Werkzeug dafür erwiesen.
Die 'matched.call.data' und 'generate.command.string' machen den Trick. Nicht perfekt, aber hilfreich und eine Verwendung für 'comment ()'. :)
# Comments only accept strings...
# Substituting the escaped quotes ('\"') makes it prettier.
generate.command.string <- function( matched.call.data )
{
command.string <- as.character( bquote( .( list( matched.call.data ) ) ) )
sapply( bquote( .(command.string) ),
USE.NAMES=FALSE,
function( x )
gsub( "\\\"", "\'", as.list( match.call() )$x )[[2]] )
}
# Some generating function...
generate.matrix <- function( nrows, ncols, data=NA ) {
# Some generated object
mat <- matrix( data= data, nrow= nrows, ncol= ncols )
matched.call.data <- do.call( "call",
c( list( as.character( match.call()[[1]] ) ),
lapply( as.list( match.call() )[-1], eval ) ) )
comment( mat ) <- c( Generated= date(),
Command = generate.command.string( matched.call.data ) )
mat
}
# Generate an object with a missing argument.
emptyMat <- generate.matrix( nrows=2, ncols=2 )
comment( emptyMat )
# Generate without formally stating arguments.
dataMat <- generate.matrix( 2, 2, sample(1:4, 4, replace= TRUE ) )
comment( dataMat )
# And with a longer command.
charMat <- generate.matrix( 3, 3,
c( 'This', 'is', 'a', 'much', 'longer',
'argument', 'section', 'that', 'wraps') )
comment( charMat )
# And with a variable.
myData <- c( 'An', 'expanded', 'command', 'argument')
charMat2 <- generate.matrix( 2, 2, myData )
comment( charMat2 )
# Create a new object from an original command.
Sys.sleep(1)
emptyMat2 <- eval( parse( text= comment( emptyMat )[['Command']] ) )
dataMat2 <- eval( parse( text= comment( emptyMat )[['Command']] ) )
# Check equality of the static matrices.
identical( emptyMat, emptyMat2 )
# The generation dates are different.
all.equal( emptyMat, emptyMat2, check.attributes= FALSE )
comment( emptyMat )['Generated'] <- NA
comment( emptyMat2 )['Generated'] <- NA
identical( emptyMat, emptyMat2 )
# Command argument structure still works too.
str( as.list( match.call(
generate.matrix, parse( text=comment( charMat2 )[[ 'Command' ]] ) ) )[-1] )