How to Avoid Recalculation of Data in R -


when working in r typically create number of intermediate data frames saved off during execution of code. allows me avoid recalculating intermediate data frames if need restart script or crashes. code ends lot of these ugly if/else checks see if intermediate data frame exist.

data <- null pathtodata <- "work/data.rds" if(file.exists(pathtodata)) {    # load calculated data    data <- readrds(pathtodata)  } else {     # calculate data    data <- ...      saverds(data, pathtodata) } 

is there better/simpler way this? ideally done in manner transparent in code.

one option wrap ugly code in function, , wrap intermediate steps in other functions. has advantage of making testing easier, , using functions on scripts considered best practice reproducible data analysis.

calcdata <- function(...) {   #calculate data }  lazycalc <- function(fn, ...) {   if(file.exists(fn)) {     data <- readrds(fn)   } else {      calcdata(...)   return(data) } 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -