Kendimi çok bulurken bulduğum şeylerden biri, veri ve nesneler üretmek için kullanılan komutları izlemektir ve yorumu bunun için yararlı bir araç olarak bulduk.
'Matched.call.data' ve 'construc.command.string' bu numarayı yapıyor. Mükemmel değil, ama yardımcı ve 'comment ()' için bir kullanım. :)
# 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] )