lambda - Is there a better clean approach to single-use functions in R? -


in lot of cases, need write code makes logical bloc , feels right place in function. however, being used once, makes more cumbersome move code away applied , give single-use name polluting namespace.

today, experimenting , came across question lambda expressions in r. implemented logic following:

x <- (function(charsbase, n, m) {   z <- apply(     matrix(         sample(unique(charsbase), n*m*3, replace = true)         , nrow = n*3, ncol = m     )     , 1     , paste, collapse="")   head(unique(z), n) }) (letters, 1000, 3) 

questions:

  1. is there better way of creating lambda in r?
  2. while namespace apparently kept clean, how memory? in experience, r leaks when create / remove object in global environment. if extensive allocation / freeing done within function, keep memory under control?

thanks lot in advance!

you can use with list or data-frame first argument. example:

result <- with(list(a=3, b=4), {     foo <- + b     foo^2 }) 

this keeps global environment clean, because part enclosed in brackets evaluated in separate environment destroyed after evaluation takes place.

however, in experience can become cumbersome program in style. find more practical clean unwanted objects rm() when they're no longer needed. it's not elegant, agree.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -