Skip to content

Instantly share code, notes, and snippets.

@zkamvar
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zkamvar/50c4a98e67aa2a697339 to your computer and use it in GitHub Desktop.
Save zkamvar/50c4a98e67aa2a697339 to your computer and use it in GitHub Desktop.
Replace data within an R folder when updating a package.
datasets <- vapply(strsplit(dir("data"), "\\."), "[", character(1), 1)
data_replacer <- function(x){
# manipulate your data here
y <- old2new_genind(x, new(class(x)))
if ("genclone" %in% class(x)){
y@mlg <- x@mlg
}
return(y)
}
message("devtools::use_data(", appendLF = FALSE)
for (i in datasets){
do.call("data", list(i)) # load the data into the global environment.
dat <- get(i) # reassign the data set into the variable "dat"
if (length(dat) > 1){ # check if it's actually a list
for (j in names(dat)){
if (any(c("genind", "genclone") %in% class(dat))){
dat[[j]] <- data_replacer(dat[[j]])
}
}
message(paste0(i, ", "), appendLF = FALSE) # print the name of the data set
assign(i, dat) # reassign the modified data to the original
} else {
if (any(c("genind", "genclone") %in% class(dat))){
dat <- data_replacer(dat)
message(paste0(i, ", "), appendLF = FALSE)
assign(i, dat)
}
}
}
message("overwrite = TRUE)")
# Once finished, copy and paste the printed output into your console.
#
# e.g. devtools::use_data(myData, myOtherData, overwrite = TRUE)
@zkamvar
Copy link
Author

zkamvar commented Jun 22, 2015

Agreed! :bowtie:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment