Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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
message("devtools::use_data(", appendLF = FALSE)
for (i in datasets){"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)
Copy link

romunov commented Apr 28, 2015

The only thing I would change is switch cat for message. 😄

Copy link

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