Last active
August 29, 2015 14:17
-
-
Save zkamvar/50c4a98e67aa2a697339 to your computer and use it in GitHub Desktop.
Replace data within an R folder when updating a package.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The only thing I would change is switch
cat
formessage
. 😄