Skip to content

Instantly share code, notes, and snippets.

@hannesdatta
Created December 2, 2020 07:29
Show Gist options
  • Select an option

  • Save hannesdatta/e0ac3d3a940ddf1a0736caaebcc69b38 to your computer and use it in GitHub Desktop.

Select an option

Save hannesdatta/e0ac3d3a940ddf1a0736caaebcc69b38 to your computer and use it in GitHub Desktop.
Save all objects that match a regular expression in .RData file
# Function scans global environment for occurence of regular expression (`regex`),
# and saves all objects in `filename`.
save_by_regex <- function(regex, filename) {
lscall = ls(envir=.GlobalEnv)
stuff_to_save = grep(regex, lscall, value=T)
if (length(stuff_to_save)>0) {
cat('saving...\n')
cat(paste0('(', paste0(stuff_to_save, collapse=', '), ')\n'))
save(list=stuff_to_save , file = filename)
cat('...done.\n') } else {
cat('No objects to save. Verify regular expression.\n')
}
}
## Demo
results_1 = 1:10
results_2 = runif(100)
results_3 = c('a', 'b', 'c')
save_by_regex('^results', 'my_objects.RData')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment