Last active
December 21, 2022 23:17
-
-
Save kmasiello/2d47c88617339fa69c85fa3ed8905e86 to your computer and use it in GitHub Desktop.
Only pin a data frame to Connect if it has changed
This file contains hidden or 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
library(pins) | |
board <- board_rsconnect() | |
# Start with a reference pin | |
ref_pin <- mtcars | |
pin_write(board, ref_pin, description = "A pin that might change.") | |
# Make a changed data frame | |
mtcars2 <- mtcars %>% dplyr::mutate(cyl = cyl*2) | |
# Parameters for `pin_unique` | |
# board = a rsconnect board object defined by board_rsconnect() | |
# pin_name = the name of the pinned object on Connect | |
# new_pin = a data frame to be pinned as a new, changed version of the pin | |
# n_prune = optional number to pass to pin_versions_prune | |
pin_unique <- function(board, pin_name, new_pin, n_prune=NA, ...){ | |
# read most recent pin | |
reference <- pins::pin_read(board, {{pin_name}}) | |
# Use `diffdf` to determine if pin is unique | |
diff <- diffdf::diffdf(reference, new_pin, suppress_warnings = TRUE) | |
if(diffdf_has_issues(diff)){ | |
pins::pin_write(board = board, x = new_pin, name = pin_name, ...) | |
message(paste( | |
"Pin has changed. Wrote updated pin to", pin_name) | |
) | |
if(is.numeric(n_prune)){ | |
pins::pin_versions_prune(board = board, name = pin_name, n = n_prune) | |
num_versions <- nrow(pins::pin_versions(board = board, name = pin_name)) | |
message(paste("There are ", num_versions, "pinned versions of", pin_name)) | |
} | |
}else{ | |
message("No changes detected from previously-pinned version. Did not pin or prune anything.") | |
} | |
} | |
# Test it out | |
board %>% | |
pin_unique("katie.masiello/ref_pin", mtcars2, 6, description = "A pin that changed from the last version.") | |
# Also see pin comparison function at https://gist.github.com/kmasiello/0b2ca8bb89bcf0692a074da5d9557236 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment